# Builds a signed release APK without Gradle: # aapt2 (resources + manifest + assets) -> javac -> d8 -> zipalign -> apksigner # Usage: Set-ExecutionPolicy -Scope Process Bypass; .\android\build-apk.ps1 $ErrorActionPreference = 'Continue' # native tools write hints to stderr; exit codes are checked explicitly $androidRoot = $PSScriptRoot $repoRoot = (Resolve-Path "$androidRoot\..").Path $sdk = if ($env:ANDROID_SDK_ROOT) { $env:ANDROID_SDK_ROOT } else { 'E:\toolchain\android-sdk' } $javaHome = if ($env:JAVA_HOME) { $env:JAVA_HOME } else { 'E:\toolchain\jdk\jdk-17.0.21.1+2' } $buildTools = Join-Path $sdk 'build-tools\35.0.0' $androidJar = Join-Path $sdk 'platforms\android-35\android.jar' $env:JAVA_HOME = $javaHome $env:PATH = "$javaHome\bin;$buildTools;$env:PATH" foreach ($p in @($buildTools, $androidJar, (Join-Path $javaHome 'bin\javac.exe'))) { if (-not (Test-Path $p)) { throw "missing toolchain path: $p" } } $build = Join-Path $androidRoot 'build' $classes = Join-Path $build 'classes' $dexDir = Join-Path $build 'dex' $res = Join-Path $androidRoot 'app\src\main\res' $srcJava = Join-Path $androidRoot 'app\src\main\java' $keystore = Join-Path $androidRoot 'keystore\occupancy.jks' $apkOut = Join-Path $repoRoot 'dist\occupancy-fitting-1.9.apk' if (Test-Path $build) { Remove-Item -Recurse -Force $build } New-Item -ItemType Directory -Force -Path $build, $classes, $dexDir, (Join-Path $repoRoot 'dist') | Out-Null function Invoke-Tool { param([string]$Exe, [string[]]$Arguments, [string]$What) $out = & $Exe @Arguments 2>&1 $code = $LASTEXITCODE if ($out) { $out | ForEach-Object { Write-Host " $_" } } if ($code -ne 0) { throw "$What failed (exit $code)" } } # ---- 1. manifest: inject package name (AGP-compatible source stays package-free) ---- $manifestSrc = Join-Path $androidRoot 'app\src\main\AndroidManifest.xml' $manifest = Get-Content -Raw $manifestSrc $manifest = $manifest -replace '', '' if ($manifest -notmatch 'package="com\.occupancyfit\.app"') { throw 'manifest package injection failed' } $manifestBuilt = Join-Path $build 'AndroidManifest.xml' [System.IO.File]::WriteAllText($manifestBuilt, $manifest, [System.Text.UTF8Encoding]::new($false)) $aapt2 = Join-Path $buildTools 'aapt2.exe' # ---- 2. resources + assets ---- Write-Host '[1/6] aapt2 compile resources' $resZip = Join-Path $build 'res.zip' Invoke-Tool $aapt2 @('compile', '--dir', $res, '-o', $resZip) 'aapt2 compile' Write-Host '[2/6] aapt2 link (manifest + resources + assets/web)' $unsigned = Join-Path $build 'unsigned.apk' Invoke-Tool $aapt2 @( 'link', '-o', $unsigned, '-I', $androidJar, '--manifest', $manifestBuilt, '-A', (Join-Path $repoRoot 'web'), '-R', $resZip, '--auto-add-overlay', '--min-sdk-version', '24', '--target-sdk-version', '34', '--version-code', '10', '--version-name', '1.9' ) 'aapt2 link' # ---- 3. java -> classes (argfile avoids PowerShell/backslash quoting problems) ---- Write-Host '[3/6] javac' $javaFiles = Get-ChildItem -Path $srcJava -Recurse -Filter *.java | ForEach-Object { $_.FullName } $argFile = Join-Path $build 'javac.args' $cp = $androidJar -replace '\\', '/' $dst = $classes -replace '\\', '/' $argLines = @( '-encoding', 'UTF-8', '-source', '8', '-target', '8', '-nowarn', '-classpath', "`"$cp`"", '-d', "`"$dst`"" ) foreach ($f in $javaFiles) { $fp = $f -replace '\\', '/' $argLines += "`"$fp`"" } [System.IO.File]::WriteAllLines($argFile, $argLines, [System.Text.UTF8Encoding]::new($false)) Invoke-Tool (Join-Path $javaHome 'bin\javac.exe') @("@$argFile") 'javac' # ---- 4. classes -> classes.dex ---- Write-Host '[4/6] d8' $d8 = Join-Path $buildTools 'd8.bat' $classFiles = Get-ChildItem -Path $classes -Recurse -Filter *.class | ForEach-Object { $_.FullName } if (-not $classFiles) { throw 'no .class files produced by javac' } Invoke-Tool $d8 (@('--release', '--min-api', '24', '--lib', $androidJar, '--output', $dexDir) + $classFiles) 'd8' $dex = Join-Path $dexDir 'classes.dex' if (-not (Test-Path $dex)) { throw 'classes.dex not produced' } # ---- 5. assemble final archive: forward-slash paths + resources.arsc stored ---- # aapt2 on Windows stores nested assets as "assets/vendor\file.js" (Android looks them up # with '/' only), and .NET's ZipArchive cannot emit a true Store entry (NoCompression still # writes deflate stored-blocks). Android 11+ refuses to install an APK whose # resources.arsc is not stored, so the archive is rebuilt with `jar -cf0M`. Write-Host '[5/6] repack (forward slashes, Store entries) + zipalign' Add-Type -AssemblyName System.IO.Compression.FileSystem $staging = Join-Path $build 'apk-contents' [System.IO.Compression.ZipFile]::ExtractToDirectory($unsigned, $staging) if (-not (Test-Path (Join-Path $staging 'AndroidManifest.xml'))) { throw 'staging extraction failed' } Copy-Item -Force $dex (Join-Path $staging 'classes.dex') $repacked = Join-Path $build 'repacked.apk' Invoke-Tool (Join-Path $javaHome 'bin\jar.exe') @('-cf0M', $repacked, '-C', $staging, '.') 'jar' $aligned = Join-Path $build 'aligned.apk' Invoke-Tool (Join-Path $buildTools 'zipalign.exe') @('-p', '-f', '4', $repacked, $aligned) 'zipalign' # ---- 6. sign ---- Write-Host '[6/6] apksigner' if (-not (Test-Path $keystore)) { Write-Host ' keystore missing, generating a local demo key' New-Item -ItemType Directory -Force -Path (Split-Path $keystore) | Out-Null Invoke-Tool (Join-Path $javaHome 'bin\keytool.exe') @( '-genkeypair', '-keystore', $keystore, '-alias', 'occupancy', '-keyalg', 'RSA', '-keysize', '2048', '-validity', '10000', '-storepass', 'occupancy', '-keypass', 'occupancy', '-dname', 'CN=Occupancy Fitting, OU=Demo, O=Local Build, L=Local, ST=Local, C=CN' ) 'keytool' } $apksigner = Join-Path $buildTools 'apksigner.bat' Invoke-Tool $apksigner @( 'sign', '--ks', $keystore, '--ks-key-alias', 'occupancy', '--ks-pass', 'pass:occupancy', '--key-pass', 'pass:occupancy', '--min-sdk-version', '24', '--out', $apkOut, $aligned ) 'apksigner sign' Write-Host '' # ---- sanity checks ---- $chk = [System.IO.Compression.ZipFile]::OpenRead($apkOut) $entries = @($chk.Entries | ForEach-Object { $_.FullName }) $arscEntry = $chk.Entries | Where-Object { $_.FullName -eq 'resources.arsc' } $arscStored = ($arscEntry.Length -eq $arscEntry.CompressedLength) $chk.Dispose() $backslash = $entries | Where-Object { $_ -like '*\*' } if ($backslash) { throw "backslash entry names remain: $($backslash -join ', ')" } foreach ($need in @( 'assets/index.html', 'assets/app.js', 'assets/data.js', 'assets/style.css', 'assets/vendor/three.min.js', 'assets/vendor/OrbitControls.js', 'classes.dex', 'resources.arsc')) { if ($entries -notcontains $need) { throw "missing APK entry: $need" } } if (-not $arscStored) { throw 'resources.arsc must be Stored (uncompressed) for Android 11+' } Write-Host 'entry check: ok (forward slashes, all assets present, resources.arsc stored)' Write-Host '' Invoke-Tool $apksigner @('verify', '--verbose', '--print-certs', $apkOut) 'apksigner verify' Write-Host '' $badging = & $aapt2 dump badging $apkOut 2>&1 $badging | Select-String 'package:|application-label:|launchable-activity:|sdkVersion|targetSdkVersion|uses-permission' | ForEach-Object { $_.Line } Get-Item $apkOut | ForEach-Object { "APK: {0} ({1:N1} MB)" -f $_.FullName, ($_.Length / 1MB) }