Watch
0
0
Fork
You've already forked hyperhive
0

vm-tests: build a sample android app offline in the android-dev archetype

This commit is contained in:
damocles 2026-11-22 11:58:59 +01:00 • committed by mara
commit 36a8b4fa1f
12 changed files with 235 additions and 10 deletions

View file

@ -0,0 +1,11 @@
# The arguments the `android-dev` archetype passes to `composeAndroidPackages`,
# in one place so the VM test that builds against the same SDK reads them too.
# build-tools 36.1.0 and platform 35: the pair the damocles agent builds with. No
# emulator and no system images; they are large, and a build is run against a
# device or an emulator elsewhere.
{
buildToolsVersions = [ "36.1.0" ];
platformVersions = [ "35" ];
includeEmulator = false;
includeSystemImages = false;
}

View file

@ -31,15 +31,8 @@
module =
{ pkgs, lib, ... }:
let
# build-tools 36.1.0 and platform 35: the pair the damocles agent builds
# with. No emulator and no system images; they are large, and a build
# is run against a device or an emulator elsewhere.
sdk = pkgs.androidenv.composeAndroidPackages {
buildToolsVersions = [ "36.1.0" ];
platformVersions = [ "35" ];
includeEmulator = false;
includeSystemImages = false;
};
# Arguments in ./android-sdk-args.nix.
sdk = pkgs.androidenv.composeAndroidPackages (import ./android-sdk-args.nix);
sdkRoot = "${sdk.androidsdk}/libexec/android-sdk";
# The unfree names the SDK needs: each package it is built from and
# the archives those fetch. Read off the SDK itself, because the

View file

@ -127,6 +127,7 @@ let
tests = {
inherit (vmTests)
agent-android-dev
hive-squid
hive-egress-antispoof
swarm-archive

View file

@ -9,6 +9,7 @@
}:
{
agent-ephemeral-rootfs = import ./vm-tests/agent-ephemeral-rootfs.nix { inherit pkgs hivePriv; };
agent-android-dev = import ./vm-tests/agent-android-dev.nix { inherit pkgs; };
hive-squid = import ./vm-tests/hive-squid.nix { inherit pkgs hivePriv; };
hive-egress-antispoof = import ./vm-tests/hive-egress-antispoof.nix { inherit pkgs hivePriv; };
swarm-archive = import ./vm-tests/swarm-archive.nix { inherit pkgs; };

View file

@ -0,0 +1,145 @@
# `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";
};
}

View file

@ -0,0 +1,17 @@
plugins {
id 'com.android.application' version '8.13.0'
}
android {
namespace = 'test.hyperhive.sample'
compileSdk = 35
buildToolsVersion = '36.1.0'
defaultConfig {
applicationId = 'test.hyperhive.sample'
minSdk = 24
targetSdk = 35
versionCode = 1
versionName = '1.0'
}
}

View file

@ -0,0 +1,11 @@
// Build with the fetched dependencies as the only repository, so a build that
// needs anything else fails instead of reaching for the network.
beforeSettings { settings ->
def repo = System.getenv('ANDROID_DEPS_REPO')
settings.pluginManagement.repositories {
maven { url = uri(repo) }
}
settings.dependencyResolutionManagement.repositories {
maven { url = uri(repo) }
}
}

View file

@ -0,0 +1,12 @@
// Build with the real repositories: the fixed-output fetch of the dependencies.
beforeSettings { settings ->
settings.pluginManagement.repositories {
google()
mavenCentral()
gradlePluginPortal()
}
settings.dependencyResolutionManagement.repositories {
google()
mavenCentral()
}
}

View file

@ -0,0 +1,3 @@
// No repositories here: the init scripts in this directory add them, the
// real ones to fetch the dependencies and a file repository to build offline.
rootProject.name = 'sample'

View file

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<application android:label="@string/app_name">
<activity android:name=".MainActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

View file

@ -0,0 +1,15 @@
package test.hyperhive.sample;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView view = new TextView(this);
view.setText(R.string.app_name);
setContentView(view);
}
}

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">hyperhive sample</string>
</resources>