Files
jizhi/frontend/android/app/build.gradle.kts
T

197 lines
7.2 KiB
Kotlin

import java.io.FileInputStream
import java.net.URI
import java.util.Base64
import java.util.Properties
plugins {
id("com.android.application")
id("kotlin-android")
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
id("dev.flutter.flutter-gradle-plugin")
}
val keystorePropertiesFile = rootProject.file("key.properties")
val keystoreProperties = Properties().apply {
if (keystorePropertiesFile.exists()) {
load(FileInputStream(keystorePropertiesFile))
}
}
val releaseSigningKeys = listOf(
"storePassword",
"keyPassword",
"keyAlias",
"storeFile",
)
val releaseSigningConfigured = keystorePropertiesFile.exists() &&
releaseSigningKeys.all { !keystoreProperties.getProperty(it).isNullOrBlank() }
fun pushBuildValue(name: String): String =
(project.findProperty(name)?.toString() ?: System.getenv(name) ?: "")
.replace("\\", "\\\\")
.replace("\"", "\\\"")
android {
namespace = "com.nx.miaoji"
compileSdk = flutter.compileSdkVersion
ndkVersion = flutter.ndkVersion
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_17.toString()
}
defaultConfig {
applicationId = "com.nx.miaoji"
minSdk = flutter.minSdkVersion
targetSdk = flutter.targetSdkVersion
versionCode = flutter.versionCode
versionName = flutter.versionName
buildConfigField("String", "PUSH_HUAWEI_APP_ID", "\"${pushBuildValue("PUSH_HUAWEI_APP_ID")}\"")
buildConfigField("String", "PUSH_HONOR_APP_ID", "\"${pushBuildValue("PUSH_HONOR_APP_ID")}\"")
buildConfigField("String", "PUSH_XIAOMI_APP_ID", "\"${pushBuildValue("PUSH_XIAOMI_APP_ID")}\"")
buildConfigField("String", "PUSH_XIAOMI_APP_KEY", "\"${pushBuildValue("PUSH_XIAOMI_APP_KEY")}\"")
buildConfigField("String", "PUSH_OPPO_APP_KEY", "\"${pushBuildValue("PUSH_OPPO_APP_KEY")}\"")
buildConfigField("String", "PUSH_OPPO_APP_SECRET", "\"${pushBuildValue("PUSH_OPPO_APP_SECRET")}\"")
buildConfigField("String", "PUSH_VIVO_APP_ID", "\"${pushBuildValue("PUSH_VIVO_APP_ID")}\"")
buildConfigField("String", "PUSH_VIVO_APP_KEY", "\"${pushBuildValue("PUSH_VIVO_APP_KEY")}\"")
buildConfigField("String", "PUSH_MEIZU_APP_ID", "\"${pushBuildValue("PUSH_MEIZU_APP_ID")}\"")
buildConfigField("String", "PUSH_MEIZU_APP_KEY", "\"${pushBuildValue("PUSH_MEIZU_APP_KEY")}\"")
}
buildFeatures {
buildConfig = true
}
signingConfigs {
if (releaseSigningConfigured) {
create("release") {
keyAlias = keystoreProperties["keyAlias"] as String
keyPassword = keystoreProperties["keyPassword"] as String
storeFile = file(keystoreProperties["storeFile"] as String)
storePassword = keystoreProperties["storePassword"] as String
storeType = keystoreProperties.getProperty("storeType", "PKCS12")
}
}
}
flavorDimensions += "environment"
productFlavors {
create("internal") {
dimension = "environment"
applicationIdSuffix = ".internal"
versionNameSuffix = "-internal"
signingConfig = signingConfigs.getByName("debug")
}
create("production") {
dimension = "environment"
if (releaseSigningConfigured) {
signingConfig = signingConfigs.getByName("release")
}
}
}
buildTypes {
release {
proguardFiles("proguard-rules.pro")
}
}
}
val dartDefines = (project.findProperty("dart-defines") as? String)
.orEmpty()
.split(',')
.mapNotNull { encoded ->
runCatching {
String(Base64.getDecoder().decode(encoded), Charsets.UTF_8)
}.getOrNull()
}
.mapNotNull { define ->
val separator = define.indexOf('=')
if (separator <= 0) null
else define.substring(0, separator) to define.substring(separator + 1)
}
.toMap()
val internalReleaseRequested = gradle.startParameter.taskNames.any { requestedTask ->
val taskName = requestedTask.substringAfterLast(':')
taskName.contains("InternalRelease", ignoreCase = true)
}
if (internalReleaseRequested) {
if (dartDefines["INTERNAL_BUILD"] != "true") {
throw GradleException(
"Internal release requires --dart-define=INTERNAL_BUILD=true",
)
}
val apiBaseUrl = dartDefines["API_BASE_URL"]?.trim().orEmpty()
val validApiBaseUrl = runCatching {
val uri = URI(apiBaseUrl)
uri.scheme in setOf("http", "https") &&
!uri.host.isNullOrBlank() &&
uri.userInfo == null
}.getOrDefault(false)
if (!validApiBaseUrl) {
throw GradleException(
"Internal release requires an explicit HTTP(S) --dart-define=API_BASE_URL=<url>",
)
}
if (dartDefines["APP_VERSION"].isNullOrBlank()) {
throw GradleException(
"Internal release requires --dart-define=APP_VERSION=<build-id>",
)
}
}
val productionReleaseRequested = gradle.startParameter.taskNames.any { requestedTask ->
val taskName = requestedTask.substringAfterLast(':')
taskName.contains("ProductionRelease", ignoreCase = true) ||
taskName.equals("assembleRelease", ignoreCase = true) ||
taskName.equals("bundleRelease", ignoreCase = true)
}
if (productionReleaseRequested && !releaseSigningConfigured) {
throw GradleException(
"Production release signing is not configured. " +
"Create frontend/android/key.properties before building productionRelease.",
)
}
if (releaseSigningConfigured) {
val releaseStore = file(keystoreProperties["storeFile"] as String)
if (!releaseStore.isFile) {
throw GradleException("Configured production release keystore does not exist: $releaseStore")
}
}
val stripIntegrationTestFromReleaseRegistrant = tasks.register(
"stripIntegrationTestFromReleaseRegistrant",
) {
doLast {
val registrant = file(
"src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java",
)
if (!registrant.isFile) return@doLast
val integrationTestRegistration = Regex(
"""(?ms)^\s*try \{\r?\n\s*flutterEngine\.getPlugins\(\)\.add\(new dev\.flutter\.plugins\.integration_test\.IntegrationTestPlugin\(\)\);\r?\n\s*\} catch \(Exception e\) \{\r?\n\s*Log\.e\(TAG, "Error registering plugin integration_test,[^\r\n]*\);\r?\n\s*\}\r?\n?""",
)
val source = registrant.readText()
val sanitized = source.replace(integrationTestRegistration, "")
if (sanitized != source) registrant.writeText(sanitized)
}
}
tasks.matching {
it.name.matches(Regex("compile.*ReleaseJavaWithJavac"))
}.configureEach {
dependsOn(stripIntegrationTestFromReleaseRegistrant)
}
flutter {
source = "../.."
}
dependencies {
implementation("com.google.mlkit:text-recognition-chinese:16.0.1")
implementation(fileTree(mapOf("dir" to "libs/push", "include" to listOf("*.aar", "*.jar"))))
testImplementation("junit:junit:4.13.2")
}