nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
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 ? (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 inherit (pkgs) lib;
41
42 # Otherwise, just use the in-tree androidenv:
43 androidEnv = pkgs.callPackage ./.. {
44 inherit pkgs licenseAccepted;
45 };
46
47 # The head unit only works on these platforms
48 includeAuto = pkgs.stdenv.hostPlatform.isx86_64 || pkgs.stdenv.hostPlatform.isDarwin;
49
50 ndkVersions = [
51 "23.1.7779620"
52 "25.1.8937393"
53 "26.1.10909125"
54 "latest"
55 ];
56
57 androidComposition = androidEnv.composeAndroidPackages {
58 includeSources = true;
59 includeSystemImages = false;
60 includeEmulator = "if-supported";
61 includeNDK = "if-supported";
62 inherit ndkVersions;
63 useGoogleAPIs = true;
64 useGoogleTVAddOns = true;
65
66 # Make sure everything from the last decade works since we are not using system images.
67 numLatestPlatformVersions = 10;
68
69 # If you want to use a custom repo JSON:
70 # repoJson = ../repo.json;
71
72 # If you want to use custom repo XMLs:
73 /*
74 repoXmls = {
75 packages = [ ../xml/repository2-1.xml ];
76 images = [
77 ../xml/android-sys-img2-1.xml
78 ../xml/android-tv-sys-img2-1.xml
79 ../xml/android-wear-sys-img2-1.xml
80 ../xml/android-wear-cn-sys-img2-1.xml
81 ../xml/google_apis-sys-img2-1.xml
82 ../xml/google_apis_playstore-sys-img2-1.xml
83 ];
84 addons = [ ../xml/addon2-1.xml ];
85 };
86 */
87
88 includeExtras = [
89 "extras;google;gcm"
90 ]
91 ++ pkgs.lib.optionals includeAuto [
92 "extras;google;auto"
93 ];
94
95 # Accepting more licenses declaratively:
96 extraLicenses = [
97 # Already accepted for you with the global accept_license = true or
98 # licenseAccepted = true on androidenv.
99 # "android-sdk-license"
100
101 # These aren't, but are useful for more uncommon setups.
102 "android-sdk-preview-license"
103 "android-googletv-license"
104 "android-sdk-arm-dbt-license"
105 "google-gdk-license"
106 "intel-android-extra-license"
107 "intel-android-sysimage-license"
108 "mips-android-sysimage-license"
109 ];
110 };
111
112 androidSdk = androidComposition.androidsdk;
113 platformTools = androidComposition.platform-tools;
114 firstSdkVersion = lib.foldl' (
115 s: x: if lib.strings.compareVersions (toString x) s < 0 then toString x else s
116 ) "100" androidComposition.platformVersions;
117 latestSdkVersion = lib.foldl' (
118 s: x: if lib.strings.compareVersions (toString x) s > 0 then toString x else s
119 ) "0" androidComposition.platformVersions;
120 jdk = pkgs.jdk;
121in
122pkgs.mkShell rec {
123 name = "androidenv-demo";
124 packages = [
125 androidSdk
126 platformTools
127 jdk
128 ];
129
130 LANG = "C.UTF-8";
131 LC_ALL = "C.UTF-8";
132 JAVA_HOME = jdk.home;
133
134 # Note: ANDROID_HOME is deprecated. Use ANDROID_SDK_ROOT.
135 ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
136 ANDROID_NDK_ROOT = "${ANDROID_SDK_ROOT}/ndk-bundle";
137
138 shellHook = ''
139 # Ensures that we don't have to use a FHS env by using the nix store's aapt2.
140 export GRADLE_OPTS="-Dorg.gradle.project.android.aapt2FromMavenOverride=$(echo "$ANDROID_SDK_ROOT/build-tools/"*"/aapt2")"
141
142 # Add cmake to the path.
143 cmake_root="$(echo "$ANDROID_SDK_ROOT/cmake/"*/)"
144 export PATH="$cmake_root/bin:$PATH"
145
146 # Write out local.properties for Android Studio.
147 cat <<EOF > local.properties
148 # This file was automatically generated by nix-shell.
149 sdk.dir=$ANDROID_SDK_ROOT
150 ndk.dir=$ANDROID_NDK_ROOT
151 cmake.dir=$cmake_root
152 EOF
153 '';
154
155 passthru.tests = {
156
157 shell-sdkmanager-licenses-test =
158 pkgs.runCommand "shell-sdkmanager-licenses-test"
159 {
160 nativeBuildInputs = [
161 androidSdk
162 jdk
163 ];
164 }
165 ''
166 if [[ ! "$(sdkmanager --licenses)" =~ "All SDK package licenses accepted." ]]; then
167 echo "At least one of SDK package licenses are not accepted."
168 exit 1
169 fi
170 touch $out
171 '';
172
173 shell-sdkmanager-packages-test =
174 pkgs.runCommand "shell-sdkmanager-packages-test"
175 {
176 nativeBuildInputs = [
177 androidSdk
178 jdk
179 ];
180 }
181 ''
182 output="$(sdkmanager --list)"
183 installed_packages_section=$(echo "''${output%%Available Packages*}" | awk 'NR>4 {print $1}')
184
185 packages=(
186 "build-tools" "platform-tools" \
187 "extras;google;gcm"
188 )
189
190 for x in $(seq ${toString firstSdkVersion} ${toString latestSdkVersion}); do
191 packages+=("sources;android-$x")
192 done
193
194 ${lib.optionalString includeAuto ''packages+=("extras;google;auto")''}
195
196 for package in "''${packages[@]}"; do
197 if [[ ! $installed_packages_section =~ "$package" ]]; then
198 echo "$package package was not installed."
199 exit 1
200 fi
201 done
202
203 num_ndk_packages="$(echo "$installed_packages_section" | grep '^ndk;' | wc -l)"
204 if [ $num_ndk_packages -ne ${toString (pkgs.lib.length ndkVersions)} ]; then
205 echo "Invalid NDK package count: $num_ndk_packages"
206 exit 1
207 fi
208
209 touch "$out"
210 '';
211 };
212}