Home Explore Blog CI



nixpkgs

4th chunk of `doc/languages-frameworks/android.section.md`
589385fa73d3f29ab38c342cfd86c23f46d4f7d23169f1b70000000100000a84
* `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";
}
```

If you are using cmake, you need to add it to PATH in a shell hook or FHS env profile.
The path is suffixed with a build number, but properly prefixed with the version.
So, something like this should suffice:

```nix
let
  cmakeVersion = "3.10.2";

  # Use cmakeVersion 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 cmakeVersion here
  shellHook = ''
    export PATH="$(echo "$ANDROID_HOME/cmake/${cmakeVersion}".*/bin):$PATH"
  '';
}
```

Note that running Android Studio with ANDROID_HOME set will automatically write a
`local.properties` file with `sdk.dir` set to $ANDROID_HOME if one does not already
exist. If you are using the NDK as well, you may have to add `ndk.dir` to this file.

An example shell.nix that does all this for you is provided in examples/shell.nix.
This shell.nix includes a shell hook that overwrites local.properties with the correct
sdk.dir and ndk.dir values. This will ensure that the SDK and NDK directories will
both be correct when you run Android Studio inside nix-shell.

## Notes on improving build.gradle compatibility {#notes-on-improving-build.gradle-compatibility}

Ensure that your buildToolsVersion and ndkVersion match what is declared in androidenv.
If you are using cmake, make sure its declared version is correct too.

Otherwise, you may get cryptic errors from aapt2 and the Android Gradle plugin warning

Title: Configuring Environment Variables and Improving Gradle/CMake Compatibility for Android Projects in Nix
Summary
This section details the necessary environment variable configurations for Android projects using Nix, focusing on `ANDROID_HOME`, `ANDROID_NDK_ROOT`, and `GRADLE_OPTS` for the Android Gradle plugin, along with including cmake in PATH. It emphasizes ensuring that `buildToolsVersion`, `ndkVersion`, and cmake version match what's declared in `androidenv` to avoid compatibility issues. Additionally, it discusses the automatic generation of `local.properties` by Android Studio and provides guidance on using a `shell.nix` file to manage these configurations for seamless integration within nix-shell.