build.gradle 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. allprojects {
  2. repositories {
  3. google()
  4. mavenCentral()
  5. }
  6. }
  7. rootProject.buildDir = '../build'
  8. subprojects {
  9. project.buildDir = "${rootProject.buildDir}/${project.name}"
  10. // AGP 8 强制要求每个模块声明 namespace,但老式插件(如 alipay_kit_android 6.0.0)
  11. // 未声明。这里在插件内部 afterEvaluate 创建 variant 之前,从 AndroidManifest 的
  12. // package 属性自动补 namespace(AGP 8 标准 workaround)。
  13. // 注意:必须放在 evaluationDependsOn(':app') 之前注册——否则子工程被提前求值,
  14. // afterEvaluate 会抛 "project is already evaluated"。
  15. afterEvaluate { proj ->
  16. def isAndroid = proj.plugins.hasPlugin('com.android.library') ||
  17. proj.plugins.hasPlugin('com.android.application')
  18. if (!isAndroid) return
  19. def androidExt = proj.extensions.findByName('android')
  20. if (androidExt == null) return
  21. // namespace 已显式声明时无需处理
  22. if (androidExt.hasProperty('namespace') &&
  23. androidExt.namespace != null && !androidExt.namespace.isEmpty()) return
  24. def manifestFile = proj.file('src/main/AndroidManifest.xml')
  25. if (!manifestFile.exists()) return
  26. try {
  27. def manifest = new XmlSlurper().parse(manifestFile)
  28. def pkg = manifest.@package.text()
  29. if (pkg) {
  30. androidExt.namespace = pkg
  31. println "Auto namespace for ${proj.name}: ${pkg}"
  32. }
  33. } catch (Exception ignore) {
  34. // manifest 解析失败时保持默认,让 AGP 继续报错提示
  35. }
  36. }
  37. }
  38. subprojects {
  39. project.evaluationDependsOn(':app')
  40. }
  41. tasks.register("clean", Delete) {
  42. delete rootProject.buildDir
  43. }