summaryrefslogtreecommitdiff
path: root/pkgs/development/mobile/androidenv/examples/shell.nix
blob: 036a7df3655e213570e2d165b2768f9f39c8f70a (plain)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
{
  # If you copy this example out of nixpkgs, use these lines instead of the next.
  # This example pins nixpkgs: https://nix.dev/tutorials/first-steps/towards-reproducibility-pinning-nixpkgs.html
  /*
    nixpkgsSource ? (fetchTarball {
      name = "nixpkgs-20.09";
      url = "https://github.com/NixOS/nixpkgs/archive/20.09.tar.gz";
      sha256 = "1wg61h4gndm3vcprdcg7rc4s1v3jkm5xd7lw8r2f67w502y94gcy";
    }),
    pkgs ? import nixpkgsSource {
      config.allowUnfree = true;
    },
  */

  # If you want to use the in-tree version of nixpkgs:
  pkgs ? import ../../../../.. {
    config.allowUnfree = true;
  },

  # You probably need to set it to true to express consent.
  licenseAccepted ? pkgs.callPackage ../license.nix { },
}:

# Copy this file to your Android project.
let
  # If you copy this example out of nixpkgs, something like this will work:
  /*
    androidEnvNixpkgs = fetchTarball {
      name = "androidenv";
      url = "https://github.com/NixOS/nixpkgs/archive/<fill me in from Git>.tar.gz";
      sha256 = "<fill me in with nix-prefetch-url --unpack>";
    };

    androidEnv = pkgs.callPackage "${androidEnvNixpkgs}/pkgs/development/mobile/androidenv" {
      inherit pkgs;
      licenseAccepted = true;
    };
  */

  inherit (pkgs) lib;

  # Otherwise, just use the in-tree androidenv:
  androidEnv = pkgs.callPackage ./.. {
    inherit pkgs licenseAccepted;
  };

  # The head unit only works on these platforms
  includeAuto = pkgs.stdenv.hostPlatform.isx86_64 || pkgs.stdenv.hostPlatform.isDarwin;

  ndkVersions = [
    "23.1.7779620"
    "25.1.8937393"
    "26.1.10909125"
    "latest"
  ];

  androidComposition = androidEnv.composeAndroidPackages {
    includeSources = true;
    includeSystemImages = false;
    includeEmulator = "if-supported";
    includeNDK = "if-supported";
    inherit ndkVersions;
    useGoogleAPIs = true;
    useGoogleTVAddOns = true;

    # Make sure everything from the last decade works since we are not using system images.
    numLatestPlatformVersions = 10;

    # If you want to use a custom repo JSON:
    # repoJson = ../repo.json;

    # If you want to use custom repo XMLs:
    /*
      repoXmls = {
        packages = [ ../xml/repository2-1.xml ];
        images = [
          ../xml/android-sys-img2-1.xml
          ../xml/android-tv-sys-img2-1.xml
          ../xml/android-wear-sys-img2-1.xml
          ../xml/android-wear-cn-sys-img2-1.xml
          ../xml/google_apis-sys-img2-1.xml
          ../xml/google_apis_playstore-sys-img2-1.xml
        ];
        addons = [ ../xml/addon2-1.xml ];
      };
    */

    includeExtras = [
      "extras;google;gcm"
    ]
    ++ pkgs.lib.optionals includeAuto [
      "extras;google;auto"
    ];

    # Accepting more licenses declaratively:
    extraLicenses = [
      # Already accepted for you with the global accept_license = true or
      # licenseAccepted = true on androidenv.
      # "android-sdk-license"

      # These aren't, but are useful for more uncommon setups.
      "android-sdk-preview-license"
      "android-googletv-license"
      "android-sdk-arm-dbt-license"
      "google-gdk-license"
      "intel-android-extra-license"
      "intel-android-sysimage-license"
      "mips-android-sysimage-license"
    ];
  };

  androidSdk = androidComposition.androidsdk;
  platformTools = androidComposition.platform-tools;
  firstSdkVersion = lib.foldl' (
    s: x: if lib.strings.compareVersions (toString x) s < 0 then toString x else s
  ) "100" androidComposition.platformVersions;
  latestSdkVersion = lib.foldl' (
    s: x: if lib.strings.compareVersions (toString x) s > 0 then toString x else s
  ) "0" androidComposition.platformVersions;
  jdk = pkgs.jdk;
in
pkgs.mkShell rec {
  name = "androidenv-demo";
  packages = [
    androidSdk
    platformTools
    jdk
  ];

  LANG = "C.UTF-8";
  LC_ALL = "C.UTF-8";
  JAVA_HOME = jdk.home;

  ANDROID_HOME = "${androidSdk}/libexec/android-sdk";
  ANDROID_NDK_ROOT = "${ANDROID_HOME}/ndk-bundle";

  shellHook = ''
    # Ensures that we don't have to use a FHS env by using the nix store's aapt2.
    export GRADLE_OPTS="-Dorg.gradle.project.android.aapt2FromMavenOverride=$(echo "$ANDROID_HOME/build-tools/"*"/aapt2")"

    # Add cmake to the path.
    cmake_root="$(echo "$ANDROID_HOME/cmake/"*/)"
    export PATH="$cmake_root/bin:$PATH"

    # Write out local.properties for Android Studio.
    cat <<EOF > local.properties
    # This file was automatically generated by nix-shell.
    sdk.dir=$ANDROID_HOME
    ndk.dir=$ANDROID_NDK_ROOT
    cmake.dir=$cmake_root
    EOF
  '';

  passthru.tests = {

    shell-sdkmanager-licenses-test =
      pkgs.runCommand "shell-sdkmanager-licenses-test"
        {
          nativeBuildInputs = [
            androidSdk
            jdk
          ];
        }
        ''
          if [[ ! "$(sdkmanager --licenses)" =~ "All SDK package licenses accepted." ]]; then
            echo "At least one of SDK package licenses are not accepted."
            exit 1
          fi
          touch $out
        '';

    shell-sdkmanager-packages-test =
      pkgs.runCommand "shell-sdkmanager-packages-test"
        {
          nativeBuildInputs = [
            androidSdk
            jdk
          ];
        }
        ''
          output="$(sdkmanager --list)"
          installed_packages_section=$(echo "''${output%%Available Packages*}" | awk 'NR>4 {print $1}')

          packages=(
            "build-tools" "platform-tools" \
            "extras;google;gcm"
          )

          ${lib.optionalString includeAuto ''packages+=("extras;google;auto")''}

          for package in "''${packages[@]}"; do
            if [[ ! $installed_packages_section =~ "$package" ]]; then
              echo "$package package was not installed."
              exit 1
            fi
          done

          num_ndk_packages="$(echo "$installed_packages_section" | grep '^ndk;' | wc -l)"
          if [ $num_ndk_packages -ne ${toString (pkgs.lib.length ndkVersions)} ]; then
            echo "Invalid NDK package count: $num_ndk_packages"
            exit 1
          fi

          touch "$out"
        '';
  };
}