build.gradle.kts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. }
  20. create("premium") {
  21. tags.set(listOf("premium"))
  22. moduleFile.set("go.premium.mod")
  23. }
  24. all {
  25. fileName.set("libclash.so")
  26. srcDir.set(file("src/main/golang"))
  27. }
  28. }
  29. }
  30. android {
  31. productFlavors {
  32. all {
  33. externalNativeBuild {
  34. cmake {
  35. arguments("-DGO_SOURCE:STRING=${golang.sourceSets.getByName(name).srcDir.asFile.get()}")
  36. arguments("-DGO_OUTPUT:STRING=${GolangPlugin.outputDirOf(project, null, null)}")
  37. arguments("-DFLAVOR_NAME:STRING=$name")
  38. }
  39. }
  40. }
  41. }
  42. externalNativeBuild {
  43. cmake {
  44. path = file("src/main/cpp/CMakeLists.txt")
  45. }
  46. }
  47. }
  48. dependencies {
  49. implementation(project(":common"))
  50. implementation(kotlin("stdlib-jdk7"))
  51. implementation(deps.androidx.core)
  52. implementation(deps.kotlin.coroutine)
  53. implementation(deps.kotlin.serialization.json)
  54. }
  55. repositories {
  56. mavenCentral()
  57. }
  58. task("downloadGeoipDatabase") {
  59. val databaseFile = geoipOutput.resolve("Country.mmdb")
  60. val moduleFile = geoipOutput.resolve("go.mod")
  61. val sourceFile = geoipOutput.resolve("blob.go")
  62. val moduleContent = """
  63. module "cfa/blob"
  64. """.trimIndent()
  65. val sourceContent = """
  66. package blob
  67. import _ "embed"
  68. //go:embed Country.mmdb
  69. var GeoipDatabase []byte
  70. """.trimIndent()
  71. outputs.dir(geoipOutput)
  72. onlyIf {
  73. System.currentTimeMillis() - databaseFile.lastModified() > geoipInvalidate.toMillis()
  74. }
  75. doLast {
  76. geoipOutput.mkdirs()
  77. moduleFile.writeText(moduleContent)
  78. sourceFile.writeText(sourceContent)
  79. URL(geoipDatabaseUrl).openConnection().getInputStream().use { input ->
  80. FileOutputStream(databaseFile).use { output ->
  81. input.copyTo(output)
  82. }
  83. }
  84. }
  85. }
  86. afterEvaluate {
  87. val downloadTask = tasks["downloadGeoipDatabase"]
  88. tasks.forEach {
  89. if (it.name.startsWith("externalGolangBuild")) {
  90. it.dependsOn(downloadTask)
  91. }
  92. }
  93. }