ZapretKVN-android/app/build.gradle.kts
loop-uh 0b7431000c
Some checks failed
Android CI / build (push) Has been cancelled
NaiveProxy keys run on the stock naive client (libnaive.so) behind sing-box
naive+https:// keys of the VPN bot are imported (buffer, QR, file, subscription text) and served by core patch 0029:
a naive outbound that starts the stock client as a child process on a loopback SOCKS5 port and reaches the server
through a loopback forwarder dialed by sing-box's own protected dialer. libnaive.so is taken from the signed upstream
plugin APKs (SHA-256 and signer pinned, scripts/ensure-naive.sh) and runs from the native library directory
(extractNativeLibs). Verified on the API 36 emulator with a real canary key. Freeze for v0.6.66.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-27 06:11:44 -05:00

315 lines
13 KiB
Kotlin
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import java.util.Properties
import javax.inject.Inject
import org.gradle.process.ExecOperations
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.plugin.compose")
}
val coreProperties = Properties().apply {
rootProject.file("core.properties").inputStream().use { load(it) }
}
val coreTag = coreProperties.getProperty("CORE_TAG")
val coreCommit = coreProperties.getProperty("CORE_COMMIT")
val corePatchFile = coreProperties.getProperty("CORE_PATCH_FILE")
val corePatchSha256 = coreProperties.getProperty("CORE_PATCH_SHA256")
val hysteriaCoreTag = coreProperties.getProperty("HYSTERIA_CORE_TAG")
val hysteriaCoreCommit = coreProperties.getProperty("HYSTERIA_CORE_COMMIT")
val xrayCoreModule = coreProperties.getProperty("XRAY_CORE_MODULE")
val naiveTag = coreProperties.getProperty("NAIVE_TAG")
val amneziaCoreModule = coreProperties.getProperty("ANDROID_AMNEZIAWG_GO")
val libboxAar = layout.projectDirectory.file("libs/libbox.aar").asFile
val appVersionCode = providers.gradleProperty("zapretVersionCode")
.orElse(providers.environmentVariable("ZAPRET_VERSION_CODE"))
.orElse("200099")
.get()
.toInt()
val appVersionName = providers.gradleProperty("zapretVersionName")
.orElse(providers.environmentVariable("ZAPRET_VERSION_NAME"))
.orElse("0.2.0")
.get()
val defaultUpdateChannel = if (
Regex("""^\d+\.\d+\.\d+-beta\.\d+$""").matches(appVersionName)
) {
"Beta"
} else {
"Stable"
}
val debugVersionNameSuffix = providers.gradleProperty("zapretDebugVersionNameSuffix")
.orElse("-debug")
.get()
val updateRepository = providers.gradleProperty("zapretUpdateRepository")
.orElse(providers.environmentVariable("ZAPRET_UPDATE_REPOSITORY"))
.orElse("zapretkvn/ZapretKVN-android")
.get()
.also { require(Regex("[A-Za-z0-9_.-]+/[A-Za-z0-9_.-]+").matches(it)) }
val supportedApkAbis = setOf("arm64-v8a", "armeabi-v7a", "x86_64")
val requestedApkAbi = providers.gradleProperty("zapretAbi")
.orElse(providers.gradleProperty("zapretReleaseAbi"))
.orNull
?.also { require(it in supportedApkAbis) { "Unsupported APK ABI: $it" } }
val signingStorePath = providers.environmentVariable("ZAPRET_SIGNING_STORE_FILE").orNull
val signingStorePassword = providers.environmentVariable("ZAPRET_SIGNING_STORE_PASSWORD").orNull
val signingKeyAlias = providers.environmentVariable("ZAPRET_SIGNING_KEY_ALIAS").orNull
val signingKeyPassword = providers.environmentVariable("ZAPRET_SIGNING_KEY_PASSWORD").orNull
val releaseSigningConfigured = listOf(
signingStorePath,
signingStorePassword,
signingKeyAlias,
signingKeyPassword,
).all { !it.isNullOrBlank() }
val releaseSigningValueCount = listOf(
signingStorePath,
signingStorePassword,
signingKeyAlias,
signingKeyPassword,
).count { !it.isNullOrBlank() }
require(releaseSigningValueCount == 0 || releaseSigningValueCount == 4) {
"Release signing requires all four ZAPRET_SIGNING_* environment variables."
}
android {
namespace = "io.github.zapretkvn.android"
compileSdk = 36
ndkVersion = coreProperties.getProperty("ANDROID_NDK_VERSION")
defaultConfig {
applicationId = "io.github.zapretkvn.android"
minSdk = 26
targetSdk = 36
versionCode = appVersionCode
versionName = appVersionName
testInstrumentationRunner = "io.github.zapretkvn.android.ZapretTestRunner"
requestedApkAbi?.let { abi ->
ndk {
abiFilters += abi
}
}
buildConfigField("String", "CORE_TAG", "\"$coreTag\"")
buildConfigField("String", "UPDATE_REPOSITORY", "\"$updateRepository\"")
buildConfigField("String", "DEFAULT_UPDATE_CHANNEL", "\"$defaultUpdateChannel\"")
buildConfigField(
"String",
"CORE_COMMIT",
"\"$coreCommit\"",
)
buildConfigField("String", "CORE_PATCH_SHA256", "\"$corePatchSha256\"")
buildConfigField("String", "CORE_UDP_PATCH_SHA256", "\"${coreProperties.getProperty("CORE_UDP_PATCH_SHA256")}\"")
buildConfigField("String", "HYSTERIA_CORE_TAG", "\"$hysteriaCoreTag\"")
buildConfigField("String", "HYSTERIA_CORE_COMMIT", "\"$hysteriaCoreCommit\"")
// Версии ядер для отчётов о подключениях (schema 2).
buildConfigField("String", "XRAY_CORE_MODULE", "\"$xrayCoreModule\"")
buildConfigField("String", "NAIVE_TAG", "\"$naiveTag\"")
buildConfigField("String", "AMNEZIA_CORE_MODULE", "\"$amneziaCoreModule\"")
}
signingConfigs {
if (releaseSigningConfigured) {
create("release") {
storeFile = file(requireNotNull(signingStorePath))
storePassword = signingStorePassword
keyAlias = signingKeyAlias
keyPassword = signingKeyPassword
enableV1Signing = true
enableV2Signing = true
enableV3Signing = true
}
}
}
buildTypes {
debug {
applicationIdSuffix = ".debug"
if (debugVersionNameSuffix.isNotEmpty()) {
versionNameSuffix = debugVersionNameSuffix
}
if (requestedApkAbi == null) {
ndk {
// CI and the default local Android test target use an x86_64 AVD.
// Device tests can select another single ABI with -PzapretAbi.
abiFilters += "x86_64"
}
}
}
release {
signingConfig = signingConfigs.findByName("release")
isMinifyEnabled = true
isShrinkResources = true
if (requestedApkAbi == null) {
ndk {
// A direct assembleRelease remains small and predictable. The release
// workflow passes zapretAbi once per supported architecture.
abiFilters += "arm64-v8a"
}
}
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro",
)
}
// Только для scripts/generate-baseline-profile.sh: тот же release, но без R8, чтобы
// записанный профиль содержал исходные имена классов (AGP при сборке настоящего
// release сам перенесёт правила baseline-prof.txt через R8). В выпуск не попадает.
create("profileGen") {
initWith(getByName("release"))
matchingFallbacks += "release"
isMinifyEnabled = false
isShrinkResources = false
isDebuggable = false
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
buildFeatures {
buildConfig = true
compose = true
}
sourceSets {
// The stock NaiveProxy client (scripts/ensure-naive.sh): an ELF executable named libnaive.so per ABI.
getByName("main").jniLibs.srcDir(rootProject.layout.projectDirectory.dir("core-build/naive"))
}
packaging {
jniLibs {
// naive is EXECUTED, not loaded: Android 10+ only lets an app exec files from its native library directory, and
// that directory holds real files only when the libraries are extracted at install time. Owner decision 2026-11-27.
useLegacyPackaging = true
// Never let a strip step touch the pinned upstream binary.
keepDebugSymbols += "**/libnaive.so"
}
}
}
dependencies {
val composeBom = platform("androidx.compose:compose-bom:2026.06.00")
implementation("androidx.core:core-ktx:1.18.0")
implementation("androidx.activity:activity-compose:1.13.0")
implementation("androidx.datastore:datastore-preferences:1.2.1")
implementation(composeBom)
implementation("androidx.compose.foundation:foundation")
implementation("androidx.compose.material:material-icons-core")
implementation("androidx.compose.material3:material3")
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation(files(libboxAar))
implementation(project(":app-updater"))
implementation(project(":network-bootstrap"))
implementation(project(":runtime-log"))
implementation(project(":client-reports"))
implementation(project(":masking"))
implementation(project(":wireguard-import"))
implementation(project(":core:model"))
implementation(project(":core:designsystem"))
implementation(project(":core:ui-model"))
implementation(project(":feature:home"))
implementation(project(":feature:servers"))
implementation(project(":feature:routing"))
implementation(project(":feature:settings"))
implementation("androidx.lifecycle:lifecycle-runtime-compose:2.9.4")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:1.10.0")
implementation("com.journeyapps:zxing-android-embedded:4.3.0")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")
testImplementation("junit:junit:4.13.2")
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.9.0")
androidTestImplementation("androidx.test.ext:junit:1.3.0")
androidTestImplementation("androidx.test:runner:1.7.0")
// Compose UI test currently carries an older transitive Espresso release.
// Pin the API 37-compatible stable test stack explicitly.
androidTestImplementation("androidx.test.espresso:espresso-core:3.7.0")
androidTestImplementation(composeBom)
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
}
// The fingerprint comparison and the rebuild live in scripts/check-libbox-fingerprint.sh
// and scripts/ensure-libbox.sh, shared with the shell tooling and covered by
// scripts/test-libbox-fingerprint.sh. A silent fallback to a stale AAR is forbidden.
abstract class EnsurePinnedLibboxTask : DefaultTask() {
@get:Inject
abstract val execOperations: ExecOperations
@get:Internal
abstract val repositoryRoot: DirectoryProperty
@TaskAction
fun ensure() {
val root = repositoryRoot.get().asFile
val script = root.resolve("scripts/ensure-libbox.sh")
check(script.isFile) { "Missing $script" }
val result = try {
execOperations.exec {
workingDir(root)
commandLine("bash", script.absolutePath, root.absolutePath)
isIgnoreExitValue = true
}
} catch (failure: Exception) {
throw GradleException("Could not run $script; bash is required to verify libbox.aar", failure)
}
if (result.exitValue != 0) {
throw GradleException(
"app/libs/libbox.aar does not match the pinned core fingerprint " +
"(core.properties + core-patches/series.sha256) and was not rebuilt " +
"automatically. See the messages above and run scripts/build-core.sh.",
)
}
}
}
val ensurePinnedLibbox by tasks.registering(EnsurePinnedLibboxTask::class) {
group = "verification"
description =
"Verifies libbox.aar against the pinned core fingerprint and rebuilds a stale core."
repositoryRoot.set(rootProject.layout.projectDirectory)
outputs.upToDateWhen { false }
}
abstract class EnsureNaiveTask : DefaultTask() {
@get:Inject
abstract val execOperations: ExecOperations
@get:Internal
abstract val repositoryRoot: DirectoryProperty
@TaskAction
fun ensure() {
val root = repositoryRoot.get().asFile
val script = root.resolve("scripts/ensure-naive.sh")
check(script.isFile) { "Missing $script" }
val result = execOperations.exec {
workingDir(root)
commandLine("bash", script.absolutePath, root.absolutePath)
isIgnoreExitValue = true
}
if (result.exitValue != 0) {
throw GradleException(
"The pinned stock NaiveProxy client (libnaive.so) could not be verified; see the messages above. " +
"A build without it, or with an unverified copy, is refused.",
)
}
}
}
val ensureNaive by tasks.registering(EnsureNaiveTask::class) {
group = "verification"
description = "Downloads and verifies the pinned stock naive client for every ABI (signer and SHA-256 pins)."
repositoryRoot.set(rootProject.layout.projectDirectory)
outputs.upToDateWhen { false }
}
tasks.named("preBuild").configure {
dependsOn(ensurePinnedLibbox)
dependsOn(ensureNaive)
}