summary refs log tree commit diff
path: root/pkgs/development/mobile/androidenv/build-app.nix
blob: 25af902679dbb444b919a9c089a4cfae81684af7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{ stdenv, androidsdk, jdk, ant, androidndk, gnumake, gawk, file, which }:
args@{ name, src, platformVersions ? [ "8" ], useGoogleAPIs ? false, antFlags ? ""
, release ? false, keyStore ? null, keyAlias ? null, keyStorePassword ? null, keyAliasPassword ? null
, useNDK ? false, ...
}:

assert release -> keyStore != null && keyAlias != null && keyStorePassword != null && keyAliasPassword != null;

let
  platformName = if stdenv.system == "i686-linux" || stdenv.system == "x86_64-linux" then "linux"
    else if stdenv.system == "x86_64-darwin" then "macosx"
    else throw "Platform: ${stdenv.system} is not supported!";

  androidsdkComposition = androidsdk {
    inherit platformVersions useGoogleAPIs;
    abiVersions = [];
  };
in
stdenv.mkDerivation ({
  name = stdenv.lib.replaceChars [" "] [""] name;

  ANDROID_HOME = "${androidsdkComposition}/libexec/android-sdk-${platformName}";

  buildInputs = [ jdk ant ] ++
    stdenv.lib.optional useNDK [ androidndk gnumake gawk file which ];

  buildPhase = ''
    ${stdenv.lib.optionalString release ''
    
      # Provide key singing attributes
      ( echo "key.store=${keyStore}"
        echo "key.alias=${keyAlias}"
        echo "key.store.password=${keyStorePassword}"
        echo "key.alias.password=${keyAliasPassword}"
      ) >> ant.properties
    ''}

    export ANDROID_SDK_HOME=`pwd` # Key files cannot be stored in the user's home directory. This overrides it.
    ${if useNDK then ''
        export GNUMAKE=${gnumake}/bin/make
        export NDK_HOST_AWK=${gawk}/bin/gawk
        ${androidndk}/bin/ndk-build
      '' else ""}
    ant ${antFlags} ${if release then "release" else "debug"}
  '';

  installPhase = ''
    mkdir -p $out
    mv bin/*-${if release then "release" else "debug"}.apk $out
    
    mkdir -p $out/nix-support
    echo "file binary-dist \"$(echo $out/*.apk)\"" > $out/nix-support/hydra-build-products
  '';
} //
builtins.removeAttrs args ["name"])