Building an Android app

I’d like to build the Android app ‘Alarmio’.

Following https://nixos.org/nixpkgs/manual/#building-an-android-application, I created a shell.nix:

{ pkgs ? import <nixpkgs> {} }:

pkgs.androidenv..buildApp {
  name = "Alarmio";
  platformVersions = [ "28" ];
}

This gave me a

$  nix-shell
error: You must accept the Android Software Development Kit License Agreement at
https://developer.android.com/studio/terms
by setting nixpkgs config option 'android_sdk.accept_license = true;'

(use '--show-trace' to show detailed location information)

even though I do have nixpkgs.config.android_sdk.accept_license = true in my /etc/nixos/configuration.nix. I guess licenseAccepted isn’t passed on correctly somewhere? Anyway I ‘hacked around’ it like:

{ pkgs ? import <nixpkgs> {} }:

(pkgs.androidenv.override {
  licenseAccepted = true;
})
.buildApp {
  name = "Alarmio";
  platformVersions = [ "28" ];
}

This allows me to enter the nix-shell, but then when I try to ./gradlew install I get:

FAILURE: Build completed with 4 failures.

1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':app:mergeOssDebugResources'.
> Multiple task action failures occurred:
   > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
      > AAPT2 aapt2-3.5.0-5435860-linux Daemon #20: Daemon startup failed
        This should not happen under normal circumstances, please file an issue if it does.
   > A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade

Adding --stacktrace reveals /home/aengelen/.gradle/caches/transforms-2/files-2.1/8848e96d59858a36a941eba86940e0ae/aapt2-3.5.0-5435860-linux/aapt2 cannot be executed. Indeed, running that executable directly, it doesn’t have the interpreter set. I can patchelf the correct interpreter, but it seems gradle overwrites it every time.

Any ideas on how to get over this hurdle?

I guess buildFHSUserEnv could work to sidestep the problem of aapt2 not finding its interpreter, but I’m not sure if you can somehow combine buildFHSUserEnv and the buildApp for android?

I think I did manage a successful dummy Android app a while back. I will try to find instructions if you can remind me in 4 days (hopefully you won’t need to … But do remind if you do)

1 Like

(I cheated by creating a /lib64/ld-linux-x86-64.so.2… but that’s dirty so I’d still be interested in a proper solution)

ummm maybe I’m not very knowledgeable but aren’t you supposed to put android_sdk.accept_license = true; in ~/.config/nixpkgs/config.nix
That seemed to work for me

This is my ~/.config/nixpkgs/config.nix:

{
  allowUnfree = true;
  android_sdk.accept_license = true;
}

nix-shell has nothing to do with NixOS, it’s part of Nix rather so it doesn’t need configuration.nix I think

1 Like

Aah, somehow I assumed settings in /etc/nixos/configuration.nix would be ‘inherited’ by the local/nix-shell config - indeed it looks like those are separate, got it.

I like to accept the license explicitly on a per-project basis, and my nix-fu is slowly improving, so I now got:

{ pkgs ? import <nixpkgs> { config.android_sdk.accept_license = true; } }:

pkgs.androidenv.buildApp {
  name = "Alarmio";
  platformVersions = [ "28" ];
}

No (good) solution for the aapt2 interpreter, though.