build.gradle.kts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import java.io.FileOutputStream
  2. import java.net.URL
  3. import java.time.Duration
  4. plugins {
  5. id("com.android.library")
  6. kotlin("android")
  7. id("kotlinx-serialization")
  8. id("library-golang")
  9. }
  10. val geoipDatabaseUrl =
  11. "https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb"
  12. val geoipInvalidate = Duration.ofDays(7)!!
  13. val geoipOutput = buildDir.resolve("intermediates/golang_blob")
  14. android {
  15. compileSdk = buildTargetSdkVersion
  16. ndkVersion = buildNdkVersion
  17. flavorDimensions(buildFlavor)
  18. defaultConfig {
  19. minSdk = buildMinSdkVersion
  20. targetSdk = buildTargetSdkVersion
  21. versionCode = buildVersionCode
  22. versionName = buildVersionName
  23. consumerProguardFiles("consumer-rules.pro")
  24. externalNativeBuild {
  25. cmake {
  26. abiFilters("arm64-v8a", "armeabi-v7a", "x86", "x86_64")
  27. arguments(
  28. "-DGO_SOURCE:STRING=$golangSource",
  29. "-DGO_OUTPUT:STRING=$golangBuild",
  30. "-DFLAVOR_NAME=$buildFlavor"
  31. )
  32. }
  33. }
  34. }
  35. buildTypes {
  36. named("release") {
  37. isMinifyEnabled = false
  38. proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
  39. }
  40. }
  41. productFlavors {
  42. create("open") {
  43. dimension = "open"
  44. }
  45. create("premium") {
  46. dimension = "premium"
  47. }
  48. }
  49. compileOptions {
  50. sourceCompatibility = JavaVersion.VERSION_1_8
  51. targetCompatibility = JavaVersion.VERSION_1_8
  52. }
  53. kotlinOptions {
  54. jvmTarget = "1.8"
  55. }
  56. externalNativeBuild {
  57. cmake {
  58. path = file("src/main/cpp/CMakeLists.txt")
  59. }
  60. }
  61. }
  62. dependencies {
  63. api(project(":common"))
  64. implementation(kotlin("stdlib-jdk7"))
  65. implementation("androidx.core:core-ktx:$ktxVersion")
  66. implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion")
  67. implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVersion")
  68. }
  69. repositories {
  70. mavenCentral()
  71. }
  72. task("downloadGeoipDatabase") {
  73. val databaseFile = geoipOutput.resolve("Country.mmdb")
  74. val moduleFile = geoipOutput.resolve("go.mod")
  75. val sourceFile = geoipOutput.resolve("blob.go")
  76. val moduleContent = """
  77. module "cfa/blob"
  78. """.trimIndent()
  79. val sourceContent = """
  80. package blob
  81. import _ "embed"
  82. //go:embed Country.mmdb
  83. var GeoipDatabase []byte
  84. """.trimIndent()
  85. onlyIf {
  86. System.currentTimeMillis() - databaseFile.lastModified() > geoipInvalidate.toMillis()
  87. }
  88. doLast {
  89. geoipOutput.mkdirs()
  90. moduleFile.writeText(moduleContent)
  91. sourceFile.writeText(sourceContent)
  92. URL(geoipDatabaseUrl).openConnection().getInputStream().use { input ->
  93. FileOutputStream(databaseFile).use { output ->
  94. input.copyTo(output)
  95. }
  96. }
  97. }
  98. }
  99. afterEvaluate {
  100. val downloadTask = tasks["downloadGeoipDatabase"]
  101. tasks.forEach {
  102. if (it.name.startsWith("externalGolangBuild")) {
  103. it.dependsOn(downloadTask)
  104. }
  105. }
  106. }