1{
2 # If you copy this example out of nixpkgs, use these lines instead of the next.
3 # This example pins nixpkgs: https://nix.dev/tutorials/first-steps/towards-reproducibility-pinning-nixpkgs.html
4 /*
5 nixpkgsSource ? (builtins.fetchTarball {
6 name = "nixpkgs-20.09";
7 url = "https://github.com/NixOS/nixpkgs/archive/20.09.tar.gz";
8 sha256 = "1wg61h4gndm3vcprdcg7rc4s1v3jkm5xd7lw8r2f67w502y94gcy";
9 }),
10 pkgs ? import nixpkgsSource {
11 config.allowUnfree = true;
12 },
13 */
14
15 # If you want to use the in-tree version of nixpkgs:
16 pkgs ? import ../../../../.. {
17 config.allowUnfree = true;
18 },
19
20 # You probably need to set it to true to express consent.
21 licenseAccepted ? pkgs.callPackage ../license.nix { },
22}:
23
24# Copy this file to your Android project.
25let
26 # If you copy this example out of nixpkgs, something like this will work:
27 /*
28 androidEnvNixpkgs = fetchTarball {
29 name = "androidenv";
30 url = "https://github.com/NixOS/nixpkgs/archive/<fill me in from Git>.tar.gz";
31 sha256 = "<fill me in with nix-prefetch-url --unpack>";
32 };
33
34 androidEnv = pkgs.callPackage "${androidEnvNixpkgs}/pkgs/development/mobile/androidenv" {
35 inherit pkgs;
36 licenseAccepted = true;
37 };
38 */
39
40 # Otherwise, just use the in-tree androidenv:
41 androidEnv = pkgs.callPackage ./.. {
42 inherit pkgs licenseAccepted;
43 };
44
45 # The head unit only works on these platforms
46 includeAuto = pkgs.stdenv.hostPlatform.isx86_64 || pkgs.stdenv.hostPlatform.isDarwin;
47
48 ndkVersions = [
49 "23.1.7779620"
50 "25.1.8937393"
51 "26.1.10909125"
52 "latest"
53 ];
54
55 androidComposition = androidEnv.composeAndroidPackages {
56 includeSources = true;
57 includeSystemImages = false;
58 includeEmulator = "if-supported";
59 includeNDK = "if-supported";
60 inherit ndkVersions;
61 useGoogleAPIs = true;
62 useGoogleTVAddOns = true;
63
64 # Make sure everything from the last decade works since we are not using system images.
65 numLatestPlatformVersions = 10;
66
67 # If you want to use a custom repo JSON:
68 # repoJson = ../repo.json;
69
70 # If you want to use custom repo XMLs:
71 /*
72 repoXmls = {
73 packages = [ ../xml/repository2-1.xml ];
74 images = [
75 ../xml/android-sys-img2-1.xml
76 ../xml/android-tv-sys-img2-1.xml
77 ../xml/android-wear-sys-img2-1.xml
78 ../xml/android-wear-cn-sys-img2-1.xml
79 ../xml/google_apis-sys-img2-1.xml
80 ../xml/google_apis_playstore-sys-img2-1.xml
81 ];
82 addons = [ ../xml/addon2-1.xml ];
83 };
84 */
85
86 includeExtras =
87 [
88 "extras;google;gcm"
89 ]
90 ++ pkgs.lib.optionals includeAuto [
91 "extras;google;auto"
92 ];
93
94 # Accepting more licenses declaratively:
95 extraLicenses = [
96 # Already accepted for you with the global accept_license = true or
97 # licenseAccepted = true on androidenv.
98 # "android-sdk-license"
99
100 # These aren't, but are useful for more uncommon setups.
101 "android-sdk-preview-license"
102 "android-googletv-license"
103 "android-sdk-arm-dbt-license"
104 "google-gdk-license"
105 "intel-android-extra-license"
106 "intel-android-sysimage-license"
107 "mips-android-sysimage-license"
108 ];
109 };
110
111 androidSdk = androidComposition.androidsdk;
112 platformTools = androidComposition.platform-tools;
113 firstSdk = pkgs.lib.foldl' pkgs.lib.min 100 androidComposition.platformVersions;
114 latestSdk = pkgs.lib.foldl' pkgs.lib.max 0 androidComposition.platformVersions;
115 jdk = pkgs.jdk;
116in
117pkgs.mkShell rec {
118 name = "androidenv-demo";
119 packages = [
120 androidSdk
121 platformTools
122 jdk
123 ];
124
125 LANG = "C.UTF-8";
126 LC_ALL = "C.UTF-8";
127 JAVA_HOME = jdk.home;
128
129 # Note: ANDROID_HOME is deprecated. Use ANDROID_SDK_ROOT.
130 ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
131 ANDROID_NDK_ROOT = "${ANDROID_SDK_ROOT}/ndk-bundle";
132
133 shellHook = ''
134 # Ensures that we don't have to use a FHS env by using the nix store's aapt2.
135 export GRADLE_OPTS="-Dorg.gradle.project.android.aapt2FromMavenOverride=$(echo "$ANDROID_SDK_ROOT/build-tools/"*"/aapt2")"
136
137 # Add cmake to the path.
138 cmake_root="$(echo "$ANDROID_SDK_ROOT/cmake/"*/)"
139 export PATH="$cmake_root/bin:$PATH"
140
141 # Write out local.properties for Android Studio.
142 cat <<EOF > local.properties
143 # This file was automatically generated by nix-shell.
144 sdk.dir=$ANDROID_SDK_ROOT
145 ndk.dir=$ANDROID_NDK_ROOT
146 cmake.dir=$cmake_root
147 EOF
148 '';
149
150 passthru.tests = {
151
152 shell-sdkmanager-licenses-test =
153 pkgs.runCommand "shell-sdkmanager-licenses-test"
154 {
155 nativeBuildInputs = [
156 androidSdk
157 jdk
158 ];
159 }
160 ''
161 if [[ ! "$(sdkmanager --licenses)" =~ "All SDK package licenses accepted." ]]; then
162 echo "At least one of SDK package licenses are not accepted."
163 exit 1
164 fi
165 touch $out
166 '';
167
168 shell-sdkmanager-packages-test =
169 pkgs.runCommand "shell-sdkmanager-packages-test"
170 {
171 nativeBuildInputs = [
172 androidSdk
173 jdk
174 ];
175 }
176 ''
177 output="$(sdkmanager --list)"
178 installed_packages_section=$(echo "''${output%%Available Packages*}" | awk 'NR>4 {print $1}')
179
180 packages=(
181 "build-tools" "platform-tools" \
182 "extras;google;gcm"
183 )
184
185 for x in $(seq ${toString firstSdk} ${toString latestSdk}); do
186 if [ $x -ne 34 ]; then
187 # FIXME couldn't find platforms;android-34, even though it's in the correct directory!! sdkmanager's bug?!
188 packages+=("platforms;android-$x")
189 fi
190 packages+=("sources;android-$x")
191 done
192
193 ${pkgs.lib.optionalString includeAuto ''packages+=("extras;google;auto")''}
194
195 for package in "''${packages[@]}"; do
196 if [[ ! $installed_packages_section =~ "$package" ]]; then
197 echo "$package package was not installed."
198 exit 1
199 fi
200 done
201
202 num_ndk_packages="$(echo "$installed_packages_section" | grep '^ndk;' | wc -l)"
203 if [ $num_ndk_packages -ne ${toString (pkgs.lib.length ndkVersions)} ]; then
204 echo "Invalid NDK package count: $num_ndk_packages"
205 exit 1
206 fi
207
208 touch "$out"
209 '';
210 };
211}