145 lines
5.7 KiB
Nix
145 lines
5.7 KiB
Nix
# `vmTests.<system>.agent-android-dev`: the `android-dev` archetype
|
|
# (../archetypes/default.nix) builds a real Android app. The module-eval suite
|
|
# only reads the archetype's settings; this boots a VM with that module and runs
|
|
# `gradle assembleDebug` on a small sample project (./android-dev-sample), which
|
|
# is the one thing that shows the pieces work together: the SDK under
|
|
# `ANDROID_HOME`, the JDK, Gradle, and `nix-ld`, without which the `aapt2` that
|
|
# Android Gradle Plugin downloads cannot start on NixOS.
|
|
#
|
|
# The VM has no network, so the build is offline. `deps` is a fixed-output
|
|
# derivation that builds the sample once with network access, in the sandbox, and
|
|
# keeps what Gradle downloaded as a Maven repository. The VM builds against only
|
|
# that repository (`offline.init.gradle`). The downloaded `aapt2` is the real
|
|
# one, started by the VM's loader.
|
|
#
|
|
# The fetch cannot start that `aapt2` itself (the build sandbox has no loader; the
|
|
# agent container has `nix-ld`), so it runs twice: the first run downloads `aapt2`
|
|
# and fails when it starts it, the second points Gradle at the SDK's own `aapt2`
|
|
# through `android.aapt2FromMavenOverride` and gets the rest of the dependencies.
|
|
#
|
|
# When `deps` changes (a new Gradle plugin version in the sample, or a new
|
|
# nixpkgs Gradle), its hash changes; the build prints the new one.
|
|
{ pkgs }:
|
|
let
|
|
sample = ./android-dev-sample;
|
|
|
|
# The SDK the fetch builds against: the archetype's own arguments, so the
|
|
# platform and build tools cannot drift apart.
|
|
unfreePkgs = import pkgs.path {
|
|
inherit (pkgs.stdenv.hostPlatform) system;
|
|
config = {
|
|
allowUnfree = true;
|
|
android_sdk.accept_license = true;
|
|
};
|
|
};
|
|
sdkArgs = import ../archetypes/android-sdk-args.nix;
|
|
sdk = unfreePkgs.androidenv.composeAndroidPackages sdkArgs;
|
|
sdkRoot = "${sdk.androidsdk}/libexec/android-sdk";
|
|
|
|
deps =
|
|
pkgs.runCommand "android-dev-sample-deps-sdk-aapt2"
|
|
{
|
|
nativeBuildInputs = [
|
|
pkgs.gradle
|
|
pkgs.jdk
|
|
];
|
|
ANDROID_HOME = sdkRoot;
|
|
JAVA_HOME = "${pkgs.jdk.home}";
|
|
SSL_CERT_FILE = "${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt";
|
|
outputHashMode = "recursive";
|
|
outputHashAlgo = "sha256";
|
|
outputHash = "sha256-IAf9/EeF4dAFuwiQhojXbVJAHkSQNmcmVDAx71MhgoI=";
|
|
}
|
|
''
|
|
export HOME=$TMPDIR GRADLE_USER_HOME=$TMPDIR/gradle-home
|
|
cp -r ${sample} project
|
|
chmod -R u+w project
|
|
cd project
|
|
build() {
|
|
gradle --no-daemon --console=plain -I online.init.gradle "$@" assembleDebug
|
|
}
|
|
|
|
# First run: downloads aapt2, then fails to start it.
|
|
build || true
|
|
cache=$GRADLE_USER_HOME/caches/modules-2/files-2.1
|
|
jar=$(find "$cache/com.android.tools.build/aapt2" -name 'aapt2-*-linux.jar' | head -n 1)
|
|
test -n "$jar" || { echo "the first run did not download aapt2" >&2; exit 1; }
|
|
|
|
# Second run: the rest of the build.
|
|
build -Pandroid.aapt2FromMavenOverride=${sdkRoot}/build-tools/${builtins.head sdkArgs.buildToolsVersions}/aapt2
|
|
|
|
# Gradle's cache is `group/artifact/version/<sha1>/<file>`; a Maven
|
|
# repository has no sha1 directory.
|
|
mkdir -p $out
|
|
cd "$cache"
|
|
find . -type f | while read -r f; do
|
|
IFS=/ read -r _ group artifact version _ name <<< "$f"
|
|
dest=$out/''${group//.//}/$artifact/$version
|
|
mkdir -p "$dest"
|
|
cp "$f" "$dest/$name"
|
|
done
|
|
'';
|
|
|
|
scriptWith =
|
|
{ sample, deps }:
|
|
''
|
|
machine.wait_for_unit("multi-user.target")
|
|
|
|
# A login shell, so the build reads the archetype's variables and PATH
|
|
# the way an agent's shell does.
|
|
machine.succeed("cp -r ${sample} /root/sample && chmod -R u+w /root/sample")
|
|
machine.succeed("bash -lc 'echo ANDROID_HOME=$ANDROID_HOME JAVA_HOME=$JAVA_HOME; command -v gradle adb'")
|
|
machine.succeed(
|
|
"cd /root/sample && ANDROID_DEPS_REPO=${deps} bash -lc "
|
|
"'gradle --offline --no-daemon --console=plain -I offline.init.gradle assembleDebug' 2>&1"
|
|
)
|
|
|
|
# The APK holds what aapt2 produced: a compiled manifest and resource table.
|
|
apk = "/root/sample/build/outputs/apk/debug/sample-debug.apk"
|
|
listing = machine.succeed(f"unzip -l {apk}")
|
|
for entry in ("AndroidManifest.xml", "resources.arsc", "classes.dex"):
|
|
assert entry in listing, f"{entry} missing from the APK:\n{listing}"
|
|
'';
|
|
in
|
|
pkgs.testers.runNixOSTest {
|
|
name = "agent-android-dev";
|
|
|
|
# The build takes a few minutes on the runner; the limit catches a hang.
|
|
globalTimeout = 1800;
|
|
|
|
# The VM's package set, with the SDK's unfree licence accepted. Left to its own
|
|
# `nixpkgs.config`, the node evaluates the archetype's allowance, which reads
|
|
# the SDK out of the package set the allowance builds, and recurses.
|
|
node.pkgs = unfreePkgs;
|
|
node.pkgsReadOnly = false;
|
|
|
|
nodes.machine = {
|
|
imports = [
|
|
(import ../archetypes/default.nix).android-dev.module
|
|
# The agent option the archetype sets a default for, declared here so the
|
|
# VM needs no hive module.
|
|
(
|
|
{ lib, ... }:
|
|
{
|
|
options.services.hyperhive.agent.icon = lib.mkOption {
|
|
type = lib.types.nullOr lib.types.path;
|
|
default = null;
|
|
};
|
|
}
|
|
)
|
|
];
|
|
virtualisation.memorySize = 4096;
|
|
virtualisation.cores = 2;
|
|
virtualisation.diskSize = 4096;
|
|
environment.systemPackages = [ pkgs.unzip ];
|
|
};
|
|
|
|
testScript = scriptWith { inherit sample deps; };
|
|
|
|
# What `nix/vm-test-typecheck.nix` checks: the same text with stand-in paths,
|
|
# so the check does not build the dependency fetch.
|
|
passthru.typecheckScript = scriptWith {
|
|
sample = "/nix/store/stub";
|
|
deps = "/nix/store/stub";
|
|
};
|
|
}
|