1{
2 # To test your changes in androidEnv run `nix-shell android-sdk-with-emulator-shell.nix`
3
4 # If you copy this example out of nixpkgs, use these lines instead of the next.
5 # This example pins nixpkgs: https://nix.dev/tutorials/first-steps/towards-reproducibility-pinning-nixpkgs.html
6 /*
7 nixpkgsSource ? (builtins.fetchTarball {
8 name = "nixpkgs-20.09";
9 url = "https://github.com/NixOS/nixpkgs/archive/20.09.tar.gz";
10 sha256 = "1wg61h4gndm3vcprdcg7rc4s1v3jkm5xd7lw8r2f67w502y94gcy";
11 }),
12 pkgs ? import nixpkgsSource {
13 config.allowUnfree = true;
14 },
15 */
16
17 # If you want to use the in-tree version of nixpkgs:
18 pkgs ? import ../../../../.. {
19 config.allowUnfree = true;
20 },
21
22 licenseAccepted ? pkgs.callPackage ../license.nix { },
23}:
24
25# Copy this file to your Android project.
26let
27 # If you copy this example out of nixpkgs, something like this will work:
28 /*
29 androidEnvNixpkgs = fetchTarball {
30 name = "androidenv";
31 url = "https://github.com/NixOS/nixpkgs/archive/<fill me in from Git>.tar.gz";
32 sha256 = "<fill me in with nix-prefetch-url --unpack>";
33 };
34
35 androidEnv = pkgs.callPackage "${androidEnvNixpkgs}/pkgs/development/mobile/androidenv" {
36 inherit pkgs;
37 licenseAccepted = true;
38 };
39 */
40
41 # Otherwise, just use the in-tree androidenv:
42 androidEnv = pkgs.callPackage ./.. {
43 inherit pkgs licenseAccepted;
44 };
45
46 sdkArgs = {
47 includeNDK = false;
48 includeSystemImages = false;
49 includeEmulator = false;
50
51 # Accepting more licenses declaratively:
52 extraLicenses = [
53 # Already accepted for you with the global accept_license = true or
54 # licenseAccepted = true on androidenv.
55 # "android-sdk-license"
56
57 # These aren't, but are useful for more uncommon setups.
58 "android-sdk-preview-license"
59 "android-googletv-license"
60 "android-sdk-arm-dbt-license"
61 "google-gdk-license"
62 "intel-android-extra-license"
63 "intel-android-sysimage-license"
64 "mips-android-sysimage-license"
65 ];
66 };
67
68 androidComposition = androidEnv.composeAndroidPackages sdkArgs;
69 androidSdk = androidComposition.androidsdk;
70 platformTools = androidComposition.platform-tools;
71 latestSdk = pkgs.lib.foldl' pkgs.lib.max 0 androidComposition.platformVersions;
72 jdk = pkgs.jdk;
73in
74pkgs.mkShell {
75 name = "androidenv-example-without-emulator-demo";
76 packages = [
77 androidSdk
78 platformTools
79 jdk
80 ];
81
82 LANG = "C.UTF-8";
83 LC_ALL = "C.UTF-8";
84 JAVA_HOME = jdk.home;
85
86 # Note: ANDROID_HOME is deprecated. Use ANDROID_SDK_ROOT.
87 ANDROID_SDK_ROOT = "${androidSdk}/libexec/android-sdk";
88
89 shellHook = ''
90 # Write out local.properties for Android Studio.
91 cat <<EOF > local.properties
92 # This file was automatically generated by nix-shell.
93 sdk.dir=$ANDROID_SDK_ROOT
94 EOF
95 '';
96
97 passthru.tests = {
98
99 shell-without-emulator-sdkmanager-packages-test =
100 pkgs.runCommand "shell-without-emulator-sdkmanager-packages-test"
101 {
102 nativeBuildInputs = [
103 androidSdk
104 jdk
105 ];
106 }
107 ''
108 output="$(sdkmanager --list)"
109 installed_packages_section=$(echo "''${output%%Available Packages*}" | awk 'NR>4 {print $1}')
110 echo "installed_packages_section: ''${installed_packages_section}"
111
112 packages=(
113 "build-tools" "cmdline-tools" \
114 "platform-tools" "platforms;android-${toString latestSdk}"
115 )
116
117 for package in "''${packages[@]}"; do
118 if [[ ! $installed_packages_section =~ "$package" ]]; then
119 echo "$package package was not installed."
120 exit 1
121 fi
122 done
123
124 touch "$out"
125 '';
126
127 shell-without-emulator-sdkmanager-excluded-packages-test =
128 pkgs.runCommand "shell-without-emulator-sdkmanager-excluded-packages-test"
129 {
130 nativeBuildInputs = [
131 androidSdk
132 jdk
133 ];
134 }
135 ''
136 output="$(sdkmanager --list)"
137 installed_packages_section=$(echo "''${output%%Available Packages*}" | awk 'NR>4 {print $1}')
138
139 excluded_packages=(
140 "emulator" "ndk"
141 )
142
143 for package in "''${excluded_packages[@]}"; do
144 if [[ $installed_packages_section =~ "$package" ]]; then
145 echo "$package package was installed, while it was excluded!"
146 exit 1
147 fi
148 done
149
150 touch "$out"
151 '';
152 };
153}