feat: 构建flutter 基座

This commit is contained in:
Booker
2026-05-18 13:36:54 +07:00
parent ce8ce5ec13
commit 37e4d73861
75 changed files with 3887 additions and 132 deletions

149
android/build.gradle.kts Normal file
View File

@@ -0,0 +1,149 @@
allprojects {
repositories {
// 优先使用阿里云镜像(国内访问更快)
maven { url = uri("https://maven.aliyun.com/repository/public") }
maven { url = uri("https://maven.aliyun.com/repository/google") }
maven { url = uri("https://maven.aliyun.com/repository/central") }
maven { url = uri("https://maven.aliyun.com/repository/gradle-plugin") }
// 备用:官方仓库
google()
mavenCentral()
gradlePluginPortal()
}
// 固定 rtc_room_engine 版本,避免解析 3.6.+ 时去 JitPack 拉 maven-metadata 导致 Read timed out服务器网络
// 该依赖在 Maven Central 有 3.6.x用固定版本后从阿里云/中央仓库解析即可
configurations.all {
resolutionStrategy {
force("io.trtc.uikit:rtc_room_engine:3.6.4.104")
}
}
}
buildscript {
repositories {
// 优先使用阿里云镜像(国内访问更快)
maven { url = uri("https://maven.aliyun.com/repository/public") }
maven { url = uri("https://maven.aliyun.com/repository/google") }
maven { url = uri("https://maven.aliyun.com/repository/central") }
maven { url = uri("https://maven.aliyun.com/repository/gradle-plugin") }
// 备用:官方仓库
google()
mavenCentral()
gradlePluginPortal()
}
dependencies {
// Google Play Services / Firebase 已移除,不再需要 google-services
}
}
val newBuildDir: Directory =
rootProject.layout.buildDirectory
.dir("../../build")
.get()
rootProject.layout.buildDirectory.value(newBuildDir)
subprojects {
val newSubprojectBuildDir: Directory = newBuildDir.dir(project.name)
project.layout.buildDirectory.value(newSubprojectBuildDir)
}
subprojects {
project.evaluationDependsOn(":app")
// 为所有缺少 namespace 的 Android 库项目自动设置 namespace解决 AGP 8.x 要求)
// 使用 whenPluginAdded 在插件应用时立即配置
project.plugins.whenPluginAdded {
if (this is com.android.build.gradle.LibraryPlugin) {
// 在插件应用时立即配置,不要延迟到 afterEvaluate
try {
project.extensions.configure<com.android.build.gradle.LibraryExtension>("android") {
// 检查 namespace 是否已设置
val currentNamespace = try {
this::class.java.getDeclaredField("namespace").apply { isAccessible = true }.get(this) as? String
} catch (e: Exception) {
null
}
if (currentNamespace.isNullOrBlank()) {
// 尝试从 AndroidManifest.xml 读取 package 作为 namespace
val manifestFile = project.file("src/main/AndroidManifest.xml")
val namespaceValue = if (manifestFile.exists()) {
try {
val manifestContent = manifestFile.readText()
val packageMatch = Regex("package=\"([^\"]+)\"").find(manifestContent)
packageMatch?.groupValues?.get(1)
} catch (e: Exception) {
null
}
} else {
null
}
// 如果从 manifest 读取失败,根据项目名称设置默认值
val finalNamespace = namespaceValue ?: when {
project.name.contains("flutter_openim_sdk", ignoreCase = true) -> "io.openim.flutter.openim_sdk"
project.name.contains("flutter_openim", ignoreCase = true) -> "io.openim.flutter.${project.name.replace("-", "_")}"
else -> null
}
if (finalNamespace != null) {
namespace = finalNamespace
println("已为 ${project.name} 设置 namespace: $finalNamespace")
}
}
}
} catch (e: Exception) {
// 如果扩展还不存在,忽略错误
println("警告: 无法为 ${project.name} 配置 namespace: ${e.message}")
}
}
}
// 双重保险:也尝试使用 withId适用于插件已应用的情况
project.plugins.withId("com.android.library") {
try {
project.extensions.configure<com.android.build.gradle.LibraryExtension>("android") {
// 检查 namespace 是否已设置
val currentNamespace = try {
this::class.java.getDeclaredField("namespace").apply { isAccessible = true }.get(this) as? String
} catch (e: Exception) {
null
}
if (currentNamespace.isNullOrBlank()) {
// 尝试从 AndroidManifest.xml 读取 package 作为 namespace
val manifestFile = project.file("src/main/AndroidManifest.xml")
val namespaceValue = if (manifestFile.exists()) {
try {
val manifestContent = manifestFile.readText()
val packageMatch = Regex("package=\"([^\"]+)\"").find(manifestContent)
packageMatch?.groupValues?.get(1)
} catch (e: Exception) {
null
}
} else {
null
}
// 如果从 manifest 读取失败,根据项目名称设置默认值
val finalNamespace = namespaceValue ?: when {
project.name.contains("flutter_openim_sdk", ignoreCase = true) -> "io.openim.flutter.openim_sdk"
project.name.contains("flutter_openim", ignoreCase = true) -> "io.openim.flutter.${project.name.replace("-", "_")}"
else -> null
}
if (finalNamespace != null) {
namespace = finalNamespace
println("已为 ${project.name} 设置 namespace (withId): $finalNamespace")
}
}
}
} catch (e: Exception) {
// 如果扩展还不存在,忽略错误
println("警告: 无法为 ${project.name} 配置 namespace (withId): ${e.message}")
}
}
}
tasks.register<Delete>("clean") {
delete(rootProject.layout.buildDirectory)
}