Home Explore Blog Models CI



nixpkgs

3rd chunk of `doc/languages-frameworks/android.section.md`
772b806f63228204ba183854b82e15745c265d0d80fdc2730000000100000fea
* `repoXmls` is an attribute set containing paths to repo XML files. If specified,
  it takes priority over `repoJson`, and will trigger a local build writing out a
  repo.json to the Nix store based on the given repository XMLs. Note that this uses
  import-from-derivation.

```nix
{
  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 ];
  };
}
```

When building the above expression with:

```bash
$ nix-build
```

The Android SDK gets deployed with all desired plugin versions.

We can also deploy subsets of the Android SDK. For example, to only the
`platform-tools` package, you can evaluate the following expression:

```nix
with import <nixpkgs> { };

let
  androidComposition = androidenv.composeAndroidPackages {
    # ...
  };
in
androidComposition.platform-tools
```

## Using predefined Android package compositions {#using-predefined-android-package-compositions}

In addition to composing an Android package set manually, it is also possible
to use a predefined composition that contains a fairly complete set of Android packages:

The following Nix expression can be used to deploy the entire SDK:

```nix
with import <nixpkgs> { };

androidenv.androidPkgs.androidsdk
```

It is also possible to use one plugin only:

```nix
with import <nixpkgs> { };

androidenv.androidPkgs.platform-tools
```

## Spawning emulator instances {#spawning-emulator-instances}

For testing purposes, it can also be quite convenient to automatically generate
scripts that spawn emulator instances with all desired configuration settings.

An emulator spawn script can be configured by invoking the `emulateApp {}`
function:

```nix
with import <nixpkgs> { };

androidenv.emulateApp {
  name = "emulate-MyAndroidApp";
  platformVersion = "28";
  abiVersion = "x86"; # armeabi-v7a, mips, x86_64
  systemImageType = "google_apis_playstore";
}
```

Additional flags may be applied to the Android SDK's emulator through the runtime environment variable `$NIX_ANDROID_EMULATOR_FLAGS`.

It is also possible to specify an APK to deploy inside the emulator
and the package and activity names to launch it:

```nix
with import <nixpkgs> { };

androidenv.emulateApp {
  name = "emulate-MyAndroidApp";
  platformVersion = "24";
  abiVersion = "armeabi-v7a"; # mips, x86, x86_64
  systemImageType = "default";
  app = ./MyApp.apk;
  package = "MyApp";
  activity = "MainActivity";
}
```

In addition to prebuilt APKs, you can also bind the APK parameter to a
`buildApp {}` function invocation shown in the previous example.

## Notes on environment variables in Android projects {#notes-on-environment-variables-in-android-projects}

* `ANDROID_HOME` should point to the Android SDK. In your Nix expressions, this should be
  `${androidComposition.androidsdk}/libexec/android-sdk`. Note that `ANDROID_SDK_ROOT` is deprecated,
  but if you rely on tools that need it, you can export it too.
* `ANDROID_NDK_ROOT` should point to the Android NDK, if you're doing NDK development.
  In your Nix expressions, this should be `${ANDROID_HOME}/ndk-bundle`.

If you are running the Android Gradle plugin, you need to export GRADLE_OPTS to override aapt2
to point to the aapt2 binary in the Nix store as well, or use a FHS environment so the packaged
aapt2 can run. If you don't want to use a FHS environment, something like this should work:

```nix
let
  buildToolsVersion = "30.0.3";

  # Use buildToolsVersion when you define androidComposition
  androidComposition = <...>;
in
pkgs.mkShell rec {
  ANDROID_HOME = "${androidComposition.androidsdk}/libexec/android-sdk";
  ANDROID_NDK_ROOT = "${ANDROID_HOME}/ndk-bundle";

  # Use the same buildToolsVersion here
  GRADLE_OPTS = "-Dorg.gradle.project.android.aapt2FromMavenOverride=${ANDROID_HOME}/build-tools/${buildToolsVersion}/aapt2";

Title: Nix-Managed Android SDK Deployment, Predefined Compositions, Emulators, and Environment Variables
Summary
This section outlines how to build and deploy Android SDK components using Nix, demonstrating the use of `repoXmls` for custom repository definitions and showing how to deploy specific SDK subsets like `platform-tools`. It introduces predefined Android package compositions (e.g., `androidenv.androidPkgs.androidsdk`) for convenience. The text then details configuring and spawning Android emulator instances using `androidenv.emulateApp`, including options for platform, ABI, system image, and deploying prebuilt APKs. Finally, it provides crucial guidance on setting environment variables (`ANDROID_HOME`, `ANDROID_NDK_ROOT`) within Nix Android projects and addresses how to override `aapt2` for the Android Gradle plugin using `GRADLE_OPTS`.