build.gradle.kts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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"))
  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(kotlin("stdlib-jdk7"))
  54. implementation(deps.androidx.core)
  55. implementation(deps.kotlin.coroutine)
  56. implementation(deps.kotlin.serialization.json)
  57. }
  58. repositories {
  59. mavenCentral()
  60. }
  61. afterEvaluate {
  62. tasks.withType(GolangBuildTask::class.java).forEach {
  63. it.inputs.file(golangSource)
  64. }
  65. }
  66. task("downloadGeoipDatabase") {
  67. val databaseFile = geoipOutput.resolve("Country.mmdb")
  68. val moduleFile = geoipOutput.resolve("go.mod")
  69. val sourceFile = geoipOutput.resolve("blob.go")
  70. val moduleContent = """
  71. module "cfa/blob"
  72. """.trimIndent()
  73. val sourceContent = """
  74. package blob
  75. import _ "embed"
  76. //go:embed Country.mmdb
  77. var GeoipDatabase []byte
  78. """.trimIndent()
  79. outputs.dir(geoipOutput)
  80. onlyIf {
  81. System.currentTimeMillis() - databaseFile.lastModified() > geoipInvalidate.toMillis()
  82. }
  83. doLast {
  84. geoipOutput.mkdirs()
  85. moduleFile.writeText(moduleContent)
  86. sourceFile.writeText(sourceContent)
  87. URL(geoipDatabaseUrl).openConnection().getInputStream().use { input ->
  88. FileOutputStream(databaseFile).use { output ->
  89. input.copyTo(output)
  90. }
  91. }
  92. }
  93. }
  94. afterEvaluate {
  95. val downloadTask = tasks["downloadGeoipDatabase"]
  96. tasks.forEach {
  97. if (it.name.startsWith("externalGolangBuild")) {
  98. it.dependsOn(downloadTask)
  99. }
  100. }
  101. }