Files

63 lines
5.2 KiB
PowerShell

param([string]$ToolRoot='C:\Users\Administrator\Documents\ChatGPT\优创未来\services\toplc-vpn-android\.tools',[switch]$Terminal)
$ErrorActionPreference='Stop'
$source=Split-Path -Parent $MyInvocation.MyCommand.Path
$stage=Join-Path 'C:\CodexBuild' ('ZhaoDisplay-'+[guid]::NewGuid().ToString('N'))
New-Item -ItemType Directory -Path $stage | Out-Null
$manifest=if($Terminal){'AndroidManifest-terminal.xml'}else{'AndroidManifest.xml'}
$apkName=if($Terminal){'ZhaoTerminal.apk'}else{'ZhaoDisplay.apk'}
Copy-Item -LiteralPath (Join-Path $source $manifest) -Destination (Join-Path $stage 'AndroidManifest.xml')
Copy-Item -LiteralPath (Join-Path $source 'res'),(Join-Path $source 'src') -Destination $stage -Recurse
$jdk=Join-Path $ToolRoot 'jdk-staging\jdk-21.0.12.1+1'
$sdk=Join-Path $ToolRoot 'android-sdk'
$bt=Join-Path $sdk 'build-tools\36.0.0'
$android=Join-Path $sdk 'platforms\android-37.0\android.jar'
$deps=Join-Path $env:LOCALAPPDATA 'ZhaoDisplay\dependencies'
New-Item -ItemType Directory -Force -Path $deps | Out-Null
$vlcArchive=Join-Path $deps 'libvlc-3.7.7.aar'
if(!(Test-Path -LiteralPath $vlcArchive)){Invoke-WebRequest 'https://repo.maven.apache.org/maven2/org/videolan/android/libvlc-all/3.7.7/libvlc-all-3.7.7.aar' -OutFile $vlcArchive}
if((Get-FileHash -LiteralPath $vlcArchive -Algorithm SHA256).Hash.ToLowerInvariant() -ne 'b48dab96e0e90e34cce3861963500c144a5aadb1b249d501d6a4b104d849e61b'){throw 'LibVLC checksum mismatch'}
Add-Type -AssemblyName System.IO.Compression.FileSystem
$vlc=Join-Path $stage 'libvlc'
[IO.Compression.ZipFile]::ExtractToDirectory($vlcArchive,$vlc)
$kotlin=Join-Path $deps 'kotlin-stdlib-2.2.10.jar'
if(!(Test-Path -LiteralPath $kotlin)){Invoke-WebRequest 'https://repo.maven.apache.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/2.2.10/kotlin-stdlib-2.2.10.jar' -OutFile $kotlin}
if((Get-FileHash -LiteralPath $kotlin -Algorithm SHA256).Hash.ToLowerInvariant() -ne '9c67cc79efd6b9215b49d2a4308f5f3433537376c7c88e89bdd6729bd096e61a'){throw 'Kotlin checksum mismatch'}
$env:JAVA_HOME=$jdk
$env:Path="$jdk\bin;$env:Path"
$build=Join-Path $stage 'build'
New-Item -ItemType Directory -Path $build,"$build\classes","$build\dex","$build\generated" | Out-Null
& "$bt\aapt2.exe" compile --dir "$stage\res" -o "$build\compiled.zip"
if($LASTEXITCODE){throw 'Resource compilation failed'}
& "$bt\aapt2.exe" link -o "$build\unsigned.apk" -I $android --manifest "$stage\AndroidManifest.xml" --java "$build\generated" "$build\compiled.zip"
if($LASTEXITCODE){throw 'Resource linking failed'}
$sources=@((Get-ChildItem "$stage\src","$build\generated" -Recurse -Filter '*.java').FullName)
& "$jdk\bin\javac.exe" -encoding UTF-8 -source 8 -target 8 -cp "$android;$vlc\classes.jar;$kotlin" -d "$build\classes" $sources
if($LASTEXITCODE){throw 'Java compilation failed'}
& "$jdk\bin\jar.exe" cf "$build\classes.jar" -C "$build\classes" .
& "$bt\d8.bat" --lib $android --min-api 26 --output "$build\dex" "$build\classes.jar" "$vlc\classes.jar" $kotlin
if($LASTEXITCODE){throw 'Dex compilation failed'}
Copy-Item "$build\unsigned.apk" "$build\with-dex.apk"
Add-Type -AssemblyName System.IO.Compression.FileSystem
$zip=[IO.Compression.ZipFile]::Open("$build\with-dex.apk",[IO.Compression.ZipArchiveMode]::Update)
try{
[IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,"$build\dex\classes.dex",'classes.dex',[IO.Compression.CompressionLevel]::Optimal) | Out-Null
foreach($abi in @('arm64-v8a','armeabi-v7a')){foreach($lib in Get-ChildItem "$vlc\jni\$abi" -Filter '*.so'){[IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,$lib.FullName,"lib/$abi/$($lib.Name)",[IO.Compression.CompressionLevel]::Optimal) | Out-Null}}
foreach($name in @('world-land.json','WORLD-LAND-LICENSE.txt')){[IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,"$source\..\frontend\src\data\$name","assets/$name",[IO.Compression.CompressionLevel]::Optimal) | Out-Null}
[IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,"$source\THIRD_PARTY.md",'assets/THIRD_PARTY.md',[IO.Compression.CompressionLevel]::Optimal) | Out-Null
foreach($license in Get-ChildItem "$source\licenses" -File){[IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,$license.FullName,"assets/licenses/$($license.Name)",[IO.Compression.CompressionLevel]::Optimal) | Out-Null}
}finally{$zip.Dispose()}
& "$bt\zipalign.exe" -f 4 "$build\with-dex.apk" "$build\aligned.apk"
$keydir=Join-Path $env:LOCALAPPDATA 'ZhaoDisplay'
New-Item -ItemType Directory -Force -Path $keydir | Out-Null
$keystore=Join-Path $keydir 'display-signing.jks'
if(!(Test-Path -LiteralPath $keystore)){
& "$jdk\bin\keytool.exe" -genkeypair -keystore $keystore -storepass android -keypass android -alias zhaodisplay -keyalg RSA -keysize 2048 -validity 10000 -dname 'CN=Zhao Household Display,O=UCVL,C=CN'
if($LASTEXITCODE){throw 'Signing key generation failed'}
}
New-Item -ItemType Directory -Force -Path "$source\build" | Out-Null
& "$bt\apksigner.bat" sign --ks $keystore --ks-pass pass:android --key-pass pass:android --ks-key-alias zhaodisplay --out "$source\build\$apkName" "$build\aligned.apk"
if($LASTEXITCODE){throw 'APK signing failed'}
& "$bt\apksigner.bat" verify "$source\build\$apkName"
if($LASTEXITCODE){throw 'APK verification failed'}
Write-Output "$source\build\$apkName"