Home Explore Blog Models CI



nixpkgs

5th chunk of `doc/languages-frameworks/android.section.md`
ec7b055d66952ccb83ffa449450a75775d407964f4e981e30000000100000de2
    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
that it cannot install the build tools because the SDK directory is not writeable.

```gradle
android {
    buildToolsVersion "30.0.3"
    ndkVersion = "22.0.7026061"
    externalNativeBuild {
        cmake {
            version "3.10.2"
        }
    }
}

```

## Querying the available versions of each plugin {#querying-the-available-versions-of-each-plugin}

All androidenv packages are available on [search.nixos.org](https://search.nixos.org).
Note that `aarch64-linux` compatibility is currently spotty, though `x86_64-linux` and `aarch64-darwin`
are well supported. This is because Google's repository definitions mark some packages for "all" architectures
that are really only for `x86_64` or `aarch64`.

## Updating the generated expressions {#updating-the-generated-expressions}

repo.json is generated from XML files that the Android Studio package manager uses.
To update the expressions run the `update.sh` script that is stored in the
`pkgs/development/mobile/androidenv/` subdirectory:

```bash
./update.sh
```

This is run automatically by the nixpkgs update script.

## Building an Android application with Ant {#building-an-android-application-with-ant}

In addition to the SDK, it is also possible to build an Ant-based Android
project and automatically deploy all the Android plugins that a project
requires. Most newer Android projects use Gradle, and this is included for historical
purposes.

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

androidenv.buildApp {
  name = "MyAndroidApp";
  src = ./myappsources;
  release = true;

  # If release is set to true, you need to specify the following parameters
  keyStore = ./keystore;
  keyAlias = "myfirstapp";
  keyStorePassword = "mykeystore";
  keyAliasPassword = "myfirstapp";

  # Any Android SDK parameters that install all the relevant plugins that a
  # build requires
  platformVersions = [ "24" ];

  # When we include the NDK, then ndk-build is invoked before Ant gets invoked
  includeNDK = true;
}
```

Aside from the app-specific build parameters (`name`, `src`, `release` and
keystore parameters), the `buildApp {}` function supports all the function
parameters that the SDK composition function (the function shown in the
previous section) supports.

This build function is particularly useful when it is desired to use
[Hydra](https://nixos.org/hydra): the Nix-based continuous integration solution
to build Android apps. An Android APK gets exposed as a build product and can be
installed on any Android device with a web browser by navigating to the build
result page.

Title: Android Development with Nix: Compatibility, Updates, and Ant Builds
Summary
This section covers several aspects of Android development within the Nix ecosystem. It explains how Android Studio automatically populates `local.properties` based on `ANDROID_HOME`, and how an example `shell.nix` can automate setting both `sdk.dir` and `ndk.dir`. The document emphasizes the importance of matching `buildToolsVersion`, `ndkVersion`, and `cmakeVersion` in `build.gradle` with `androidenv` declarations to ensure compatibility and prevent errors. It also mentions that `androidenv` package versions are discoverable on `search.nixos.org` (with notes on `aarch64-linux` compatibility) and details how to update generated expressions by running a specific `update.sh` script. Finally, it describes `androidenv.buildApp` for building Ant-based Android projects, providing a Nix example with parameters for release builds, keystores, platform versions, and NDK inclusion, highlighting its utility for CI/CD solutions like Hydra.