3 The Android build environment provides three major features and a number of
6 ## Using androidenv with Android Studio {#using-androidenv-with-android-studio}
8 Use the `android-studio-full` attribute for a very complete Android SDK, including system images:
11 buildInputs = [ android-studio-full ];
17 buildInputs = [ androidStudioPackages.stable.full ];
20 Alternatively, you can pass composeAndroidPackages to the `withSdk` passthru:
24 (android-studio.withSdk (androidenv.composeAndroidPackages {
30 These will export ANDROID_SDK_ROOT and ANDROID_NDK_ROOT to the SDK and NDK directories
31 in the specified Android build environment.
33 ## Deploying an Android SDK installation with plugins {#deploying-an-android-sdk-installation-with-plugins}
35 Alternatively, you can deploy the SDK separately with a desired set of plugins, or subsets of an SDK.
38 with import <nixpkgs> {};
41 androidComposition = androidenv.composeAndroidPackages {
42 cmdLineToolsVersion = "8.0";
43 toolsVersion = "26.1.1";
44 platformToolsVersion = "30.0.5";
45 buildToolsVersions = [ "30.0.3" ];
46 includeEmulator = false;
47 emulatorVersion = "30.3.4";
48 platformVersions = [ "28" "29" "30" ];
49 includeSources = false;
50 includeSystemImages = false;
51 systemImageTypes = [ "google_apis_playstore" ];
52 abiVersions = [ "armeabi-v7a" "arm64-v8a" ];
53 cmakeVersions = [ "3.10.2" ];
55 ndkVersions = ["22.0.7026061"];
56 useGoogleAPIs = false;
57 useGoogleTVAddOns = false;
63 androidComposition.androidsdk
66 The above function invocation states that we want an Android SDK with the above
67 specified plugin versions. By default, most plugins are disabled. Notable
68 exceptions are the tools, platform-tools and build-tools sub packages.
70 The following parameters are supported:
72 * `cmdLineToolsVersion `, specifies the version of the `cmdline-tools` package to use
73 * `toolsVersion`, specifies the version of the `tools` package. Notice `tools` is
74 obsolete, and currently only `26.1.1` is available, so there's not a lot of
75 options here, however, you can set it as `null` if you don't want it.
76 * `platformsToolsVersion` specifies the version of the `platform-tools` plugin
77 * `buildToolsVersions` specifies the versions of the `build-tools` plugins to
79 * `includeEmulator` specifies whether to deploy the emulator package (`false`
80 by default). When enabled, the version of the emulator to deploy can be
81 specified by setting the `emulatorVersion` parameter.
82 * `cmakeVersions` specifies which CMake versions should be deployed.
83 * `includeNDK` specifies that the Android NDK bundle should be included.
85 * `ndkVersions` specifies the NDK versions that we want to use. These are linked
86 under the `ndk` directory of the SDK root, and the first is linked under the
87 `ndk-bundle` directory.
88 * `ndkVersion` is equivalent to specifying one entry in `ndkVersions`, and
89 `ndkVersions` overrides this parameter if provided.
90 * `includeExtras` is an array of identifier strings referring to arbitrary
91 add-on packages that should be installed.
92 * `platformVersions` specifies which platform SDK versions should be included.
94 For each platform version that has been specified, we can apply the following
97 * `includeSystemImages` specifies whether a system image for each platform SDK
99 * `includeSources` specifies whether the sources for each SDK version should be
101 * `useGoogleAPIs` specifies that for each selected platform version the
102 Google API should be included.
103 * `useGoogleTVAddOns` specifies that for each selected platform version the
104 Google TV add-on should be included.
106 For each requested system image we can specify the following options:
108 * `systemImageTypes` specifies what kind of system images should be included.
109 Defaults to: `default`.
110 * `abiVersions` specifies what kind of ABI version of each system image should
111 be included. Defaults to: `armeabi-v7a`.
113 Most of the function arguments have reasonable default settings.
115 You can specify license names:
117 * `extraLicenses` is a list of license names.
118 You can get these names from repo.json or `querypackages.sh licenses`. The SDK
119 license (`android-sdk-license`) is accepted for you if you set accept_license
120 to true. If you are doing something like working with preview SDKs, you will
121 want to add `android-sdk-preview-license` or whichever license applies here.
123 Additionally, you can override the repositories that composeAndroidPackages will
126 * `repoJson` specifies a path to a generated repo.json file. You can generate this
127 by running `generate.sh`, which in turn will call into `mkrepo.rb`.
128 * `repoXmls` is an attribute set containing paths to repo XML files. If specified,
129 it takes priority over `repoJson`, and will trigger a local build writing out a
130 repo.json to the Nix store based on the given repository XMLs.
135 packages = [ ./xml/repository2-1.xml ];
137 ./xml/android-sys-img2-1.xml
138 ./xml/android-tv-sys-img2-1.xml
139 ./xml/android-wear-sys-img2-1.xml
140 ./xml/android-wear-cn-sys-img2-1.xml
141 ./xml/google_apis-sys-img2-1.xml
142 ./xml/google_apis_playstore-sys-img2-1.xml
144 addons = [ ./xml/addon2-1.xml ];
149 When building the above expression with:
155 The Android SDK gets deployed with all desired plugin versions.
157 We can also deploy subsets of the Android SDK. For example, to only the
158 `platform-tools` package, you can evaluate the following expression:
161 with import <nixpkgs> {};
164 androidComposition = androidenv.composeAndroidPackages {
168 androidComposition.platform-tools
171 ## Using predefined Android package compositions {#using-predefined-android-package-compositions}
173 In addition to composing an Android package set manually, it is also possible
174 to use a predefined composition that contains a fairly complete set of Android packages:
176 The following Nix expression can be used to deploy the entire SDK:
179 with import <nixpkgs> {};
181 androidenv.androidPkgs.androidsdk
184 It is also possible to use one plugin only:
187 with import <nixpkgs> {};
189 androidenv.androidPkgs.platform-tools
192 ## Spawning emulator instances {#spawning-emulator-instances}
194 For testing purposes, it can also be quite convenient to automatically generate
195 scripts that spawn emulator instances with all desired configuration settings.
197 An emulator spawn script can be configured by invoking the `emulateApp {}`
201 with import <nixpkgs> {};
203 androidenv.emulateApp {
204 name = "emulate-MyAndroidApp";
205 platformVersion = "28";
206 abiVersion = "x86"; # armeabi-v7a, mips, x86_64
207 systemImageType = "google_apis_playstore";
211 Additional flags may be applied to the Android SDK's emulator through the runtime environment variable `$NIX_ANDROID_EMULATOR_FLAGS`.
213 It is also possible to specify an APK to deploy inside the emulator
214 and the package and activity names to launch it:
217 with import <nixpkgs> {};
219 androidenv.emulateApp {
220 name = "emulate-MyAndroidApp";
221 platformVersion = "24";
222 abiVersion = "armeabi-v7a"; # mips, x86, x86_64
223 systemImageType = "default";
226 activity = "MainActivity";
230 In addition to prebuilt APKs, you can also bind the APK parameter to a
231 `buildApp {}` function invocation shown in the previous example.
233 ## Notes on environment variables in Android projects {#notes-on-environment-variables-in-android-projects}
235 * `ANDROID_HOME` should point to the Android SDK. In your Nix expressions, this should be
236 `${androidComposition.androidsdk}/libexec/android-sdk`. Note that `ANDROID_SDK_ROOT` is deprecated,
237 but if you rely on tools that need it, you can export it too.
238 * `ANDROID_NDK_ROOT` should point to the Android NDK, if you're doing NDK development.
239 In your Nix expressions, this should be `${ANDROID_HOME}/ndk-bundle`.
241 If you are running the Android Gradle plugin, you need to export GRADLE_OPTS to override aapt2
242 to point to the aapt2 binary in the Nix store as well, or use a FHS environment so the packaged
243 aapt2 can run. If you don't want to use a FHS environment, something like this should work:
247 buildToolsVersion = "30.0.3";
249 # Use buildToolsVersion when you define androidComposition
250 androidComposition = <...>;
253 ANDROID_HOME = "${androidComposition.androidsdk}/libexec/android-sdk";
254 ANDROID_NDK_ROOT = "${ANDROID_HOME}/ndk-bundle";
256 # Use the same buildToolsVersion here
257 GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${ANDROID_HOME}/build-tools/${buildToolsVersion}/aapt2";
261 If you are using cmake, you need to add it to PATH in a shell hook or FHS env profile.
262 The path is suffixed with a build number, but properly prefixed with the version.
263 So, something like this should suffice:
267 cmakeVersion = "3.10.2";
269 # Use cmakeVersion when you define androidComposition
270 androidComposition = <...>;
273 ANDROID_HOME = "${androidComposition.androidsdk}/libexec/android-sdk";
274 ANDROID_NDK_ROOT = "${ANDROID_HOME}/ndk-bundle";
276 # Use the same cmakeVersion here
278 export PATH="$(echo "$ANDROID_HOME/cmake/${cmakeVersion}".*/bin):$PATH"
283 Note that running Android Studio with ANDROID_HOME set will automatically write a
284 `local.properties` file with `sdk.dir` set to $ANDROID_HOME if one does not already
285 exist. If you are using the NDK as well, you may have to add `ndk.dir` to this file.
287 An example shell.nix that does all this for you is provided in examples/shell.nix.
288 This shell.nix includes a shell hook that overwrites local.properties with the correct
289 sdk.dir and ndk.dir values. This will ensure that the SDK and NDK directories will
290 both be correct when you run Android Studio inside nix-shell.
292 ## Notes on improving build.gradle compatibility {#notes-on-improving-build.gradle-compatibility}
294 Ensure that your buildToolsVersion and ndkVersion match what is declared in androidenv.
295 If you are using cmake, make sure its declared version is correct too.
297 Otherwise, you may get cryptic errors from aapt2 and the Android Gradle plugin warning
298 that it cannot install the build tools because the SDK directory is not writeable.
302 buildToolsVersion "30.0.3"
303 ndkVersion = "22.0.7026061"
304 externalNativeBuild {
313 ## Querying the available versions of each plugin {#querying-the-available-versions-of-each-plugin}
315 repo.json provides all the options in one file now.
317 A shell script in the `pkgs/development/mobile/androidenv/` subdirectory can be used to retrieve all
321 ./querypackages.sh packages
324 The above command-line instruction queries all package versions in repo.json.
326 ## Updating the generated expressions {#updating-the-generated-expressions}
328 repo.json is generated from XML files that the Android Studio package manager uses.
329 To update the expressions run the `generate.sh` script that is stored in the
330 `pkgs/development/mobile/androidenv/` subdirectory:
336 ## Building an Android application with Ant {#building-an-android-application-with-ant}
338 In addition to the SDK, it is also possible to build an Ant-based Android
339 project and automatically deploy all the Android plugins that a project
340 requires. Most newer Android projects use Gradle, and this is included for historical
344 with import <nixpkgs> {};
346 androidenv.buildApp {
347 name = "MyAndroidApp";
348 src = ./myappsources;
351 # If release is set to true, you need to specify the following parameters
352 keyStore = ./keystore;
353 keyAlias = "myfirstapp";
354 keyStorePassword = "mykeystore";
355 keyAliasPassword = "myfirstapp";
357 # Any Android SDK parameters that install all the relevant plugins that a
359 platformVersions = [ "24" ];
361 # When we include the NDK, then ndk-build is invoked before Ant gets invoked
366 Aside from the app-specific build parameters (`name`, `src`, `release` and
367 keystore parameters), the `buildApp {}` function supports all the function
368 parameters that the SDK composition function (the function shown in the
369 previous section) supports.
371 This build function is particularly useful when it is desired to use
372 [Hydra](https://nixos.org/hydra): the Nix-based continuous integration solution
373 to build Android apps. An Android APK gets exposed as a build product and can be
374 installed on any Android device with a web browser by navigating to the build