build.gradle.kts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import com.github.kr328.golang.GolangBuildTask
  2. import com.github.kr328.golang.GolangPlugin
  3. import java.io.FileOutputStream
  4. import java.net.URL
  5. import java.time.Duration
  6. plugins {
  7. kotlin("android")
  8. id("com.android.library")
  9. id("kotlinx-serialization")
  10. id("golang-android")
  11. }
  12. val geoipDatabaseUrl =
  13. "https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb"
  14. val geoipInvalidate = Duration.ofDays(7)!!
  15. val geoipOutput = buildDir.resolve("intermediates/golang_blob")
  16. val golangSource = file("src/main/golang/native")
  17. golang {
  18. sourceSets {
  19. create("foss") {
  20. tags.set(listOf("foss"))
  21. srcDir.set(file("src/foss/golang"))
  22. }
  23. create("premium") {
  24. tags.set(listOf("premium", "without_gvisor", "without_system"))
  25. srcDir.set(file("src/premium/golang"))
  26. }
  27. all {
  28. fileName.set("libclash.so")
  29. packageName.set("cfa/native")
  30. }
  31. }
  32. }
  33. android {
  34. productFlavors {
  35. all {
  36. externalNativeBuild {
  37. cmake {
  38. arguments("-DGO_SOURCE:STRING=${golangSource}")
  39. arguments("-DGO_OUTPUT:STRING=${GolangPlugin.outputDirOf(project, null, null)}")
  40. arguments("-DFLAVOR_NAME:STRING=$name")
  41. }
  42. }
  43. }
  44. }
  45. externalNativeBuild {
  46. cmake {
  47. path = file("src/main/cpp/CMakeLists.txt")
  48. }
  49. }
  50. }
  51. dependencies {
  52. implementation(project(":common"))
  53. implementation(libs.androidx.core)
  54. implementation(libs.kotlin.coroutine)
  55. implementation(libs.kotlin.serialization.json)
  56. }
  57. afterEvaluate {
  58. tasks.withType(GolangBuildTask::class.java).forEach {
  59. it.inputs.dir(golangSource)
  60. }
  61. }
  62. task("downloadGeoipDatabase") {
  63. val databaseFile = geoipOutput.resolve("Country.mmdb")
  64. val moduleFile = geoipOutput.resolve("go.mod")
  65. val sourceFile = geoipOutput.resolve("blob.go")
  66. val moduleContent = """
  67. module "cfa/blob"
  68. """.trimIndent()
  69. val sourceContent = """
  70. package blob
  71. import _ "embed"
  72. //go:embed Country.mmdb
  73. var GeoipDatabase []byte
  74. """.trimIndent()
  75. outputs.dir(geoipOutput)
  76. onlyIf {
  77. System.currentTimeMillis() - databaseFile.lastModified() > geoipInvalidate.toMillis()
  78. }
  79. doLast {
  80. geoipOutput.mkdirs()
  81. moduleFile.writeText(moduleContent)
  82. sourceFile.writeText(sourceContent)
  83. URL(geoipDatabaseUrl).openConnection().getInputStream().use { input ->
  84. FileOutputStream(databaseFile).use { output ->
  85. input.copyTo(output)
  86. }
  87. }
  88. }
  89. }
  90. afterEvaluate {
  91. val downloadTask = tasks["downloadGeoipDatabase"]
  92. tasks.forEach {
  93. if (it.name.startsWith("externalGolangBuild")) {
  94. it.dependsOn(downloadTask)
  95. }
  96. }
  97. }