build.gradle.kts 2.7 KB

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