Parcourir la source

优化项目配置

cc12458 il y a 2 semaines
Parent
commit
1b8fd8ed26
2 fichiers modifiés avec 63 ajouts et 20 suppressions
  1. 53 19
      app/build.gradle.kts
  2. 10 1
      gradle.properties

+ 53 - 19
app/build.gradle.kts

@@ -1,6 +1,7 @@
 import com.android.build.gradle.internal.api.BaseVariantOutputImpl
 import java.text.SimpleDateFormat
 import java.util.Date
+import java.util.Properties
 
 fun getVersionCode(): Int {
   val dateFormat = SimpleDateFormat("yyyyMMdd")
@@ -15,6 +16,35 @@ plugins {
   alias(libs.plugins.kotlin.serialization)
 }
 
+// 读取 gradle.properties 并解析 project.xx 字段
+fun buildTypeChain(): Map<String, List<String>> {
+  val properties = Properties()
+  val file = File(rootDir, "gradle.properties")
+  if (file.exists()) {
+    properties.load(file.inputStream())
+  }
+
+  // 只取 project.xx 字段
+  val projectMap = properties.entries
+    .filter { (k, _) -> k.toString().startsWith("project.") }
+    .associate { (k, v) -> k.toString().removePrefix("project.") to v.toString() }
+
+  // 递归查找链
+  fun resolveChain(key: String): List<String> {
+    val next = projectMap[key]
+    return if (next == null || next == "debug" || next == "release") {
+      listOfNotNull(key, next)
+    } else {
+      listOf(key) + resolveChain(next)
+    }
+  }
+
+  // 构建所有链并返回
+  return projectMap.keys.associateWith { resolveChain(it) }
+}
+
+val buildTypeChain = buildTypeChain()
+
 android {
   namespace = "com.hzliuzhi.applet.container"
   compileSdk = 35
@@ -24,7 +54,7 @@ android {
     minSdk = 26
     targetSdk = 35
     versionCode = getVersionCode()
-    versionName = "2.0.0"
+    versionName = "2.2.1"
 
     testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
   }
@@ -34,27 +64,31 @@ android {
       isMinifyEnabled = false
       proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
     }
-    create("aio") {
-      initWith(getByName("debug"))
-      matchingFallbacks += listOf("debug")
-    }
-    create("aio-test") {
-      initWith(getByName("aio"))
-      matchingFallbacks += listOf("aio", "debug")
-    }
-    create("aio-CQ") {
-      initWith(getByName("aio-test"))
-      matchingFallbacks += listOf("aio-test", "aio", "debug")
-    }
+    buildTypeChain.values.sortedBy { it.size }.forEach { chain ->
+      when (val name = chain.first()) {
+        "debug",
+        "release",
+          -> return@forEach
 
-    create("pda") {
-      initWith(getByName("debug"))
-      matchingFallbacks += listOf("debug")
+        else -> create(name) {
+          // initWith 取 chain 的下一个(即父级)
+          initWith(getByName(chain.getOrNull(1) ?: "debug"))
+          // matchingFallbacks 取除自己外的所有
+          matchingFallbacks += chain.drop(1)
+        }
+      }
     }
+  }
 
-    create("pda-test") {
-      initWith(getByName("pda"))
-      matchingFallbacks += listOf("pda", "debug")
+  sourceSets {
+    buildTypeChain.forEach { (name, chain) ->
+      maybeCreate(name).apply {
+        // 不包含 main,只合并链上的自定义类型
+        val dirs = chain.reversed()
+        dirs.forEach { dir ->
+          assets.srcDir("src/$dir/assets")
+        }
+      }
     }
   }
 

+ 10 - 1
gradle.properties

@@ -21,4 +21,13 @@ kotlin.code.style=official
 # Enables namespacing of each library's R class so that its R class includes only the
 # resources declared in the library itself and none from the library's dependencies,
 # thereby reducing the size of the R class for that library
-android.nonTransitiveRClass=true
+android.nonTransitiveRClass=true
+
+project.aio=debug
+project.aio-test=aio
+project.aio-CQ=aio-test
+
+project.pda=debug
+project.pda-test=pda
+project.pda-prod=pda-test
+project.pda-HZ=pda-prod