build.gradle.kts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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://raw.githubusercontent.com/Loyalsoldier/geoip/release/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("meta-alpha") {
  20. tags.set(listOf("foss"))
  21. srcDir.set(file("src/foss/golang"))
  22. }
  23. all {
  24. fileName.set("libclash.so")
  25. packageName.set("cfa/native")
  26. }
  27. }
  28. }
  29. android {
  30. productFlavors {
  31. all {
  32. externalNativeBuild {
  33. cmake {
  34. arguments("-DGO_SOURCE:STRING=${golangSource}")
  35. arguments("-DGO_OUTPUT:STRING=${GolangPlugin.outputDirOf(project, null, null)}")
  36. arguments("-DFLAVOR_NAME:STRING=$name")
  37. }
  38. }
  39. }
  40. }
  41. externalNativeBuild {
  42. cmake {
  43. path = file("src/main/cpp/CMakeLists.txt")
  44. }
  45. }
  46. }
  47. dependencies {
  48. implementation(project(":common"))
  49. implementation(libs.androidx.core)
  50. implementation(libs.kotlin.coroutine)
  51. implementation(libs.kotlin.serialization.json)
  52. }
  53. afterEvaluate {
  54. tasks.withType(GolangBuildTask::class.java).forEach {
  55. it.inputs.dir(golangSource)
  56. }
  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. }