| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- import java.io.FileOutputStream
- import java.net.URL
- import java.time.Duration
- plugins {
- id("com.android.library")
- kotlin("android")
- id("kotlinx-serialization")
- id("library-golang")
- }
- val geoipDatabaseUrl =
- "https://github.com/Dreamacro/maxmind-geoip/releases/latest/download/Country.mmdb"
- val geoipInvalidate = Duration.ofDays(7)!!
- val geoipOutput = buildDir.resolve("intermediates/golang_blob")
- android {
- compileSdk = buildTargetSdkVersion
- ndkVersion = buildNdkVersion
- flavorDimensions(buildFlavor)
- defaultConfig {
- minSdk = buildMinSdkVersion
- targetSdk = buildTargetSdkVersion
- versionCode = buildVersionCode
- versionName = buildVersionName
- consumerProguardFiles("consumer-rules.pro")
- externalNativeBuild {
- cmake {
- abiFilters("arm64-v8a", "armeabi-v7a", "x86", "x86_64")
- arguments(
- "-DGO_SOURCE:STRING=$golangSource",
- "-DGO_OUTPUT:STRING=$golangBuild",
- "-DFLAVOR_NAME=$buildFlavor"
- )
- }
- }
- }
- buildTypes {
- named("release") {
- isMinifyEnabled = false
- proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
- }
- }
- productFlavors {
- create("open") {
- dimension = "open"
- }
- create("premium") {
- dimension = "premium"
- }
- }
- compileOptions {
- sourceCompatibility = JavaVersion.VERSION_1_8
- targetCompatibility = JavaVersion.VERSION_1_8
- }
- kotlinOptions {
- jvmTarget = "1.8"
- }
- externalNativeBuild {
- cmake {
- path = file("src/main/cpp/CMakeLists.txt")
- }
- }
- }
- dependencies {
- api(project(":common"))
- implementation(kotlin("stdlib-jdk7"))
- implementation("androidx.core:core-ktx:$ktxVersion")
- implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutineVersion")
- implementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVersion")
- }
- repositories {
- mavenCentral()
- }
- task("downloadGeoipDatabase") {
- val databaseFile = geoipOutput.resolve("Country.mmdb")
- val moduleFile = geoipOutput.resolve("go.mod")
- val sourceFile = geoipOutput.resolve("blob.go")
- val moduleContent = """
- module "cfa/blob"
- """.trimIndent()
- val sourceContent = """
- package blob
-
- import _ "embed"
-
- //go:embed Country.mmdb
- var GeoipDatabase []byte
- """.trimIndent()
- onlyIf {
- System.currentTimeMillis() - databaseFile.lastModified() > geoipInvalidate.toMillis()
- }
- doLast {
- geoipOutput.mkdirs()
- moduleFile.writeText(moduleContent)
- sourceFile.writeText(sourceContent)
- URL(geoipDatabaseUrl).openConnection().getInputStream().use { input ->
- FileOutputStream(databaseFile).use { output ->
- input.copyTo(output)
- }
- }
- }
- }
- afterEvaluate {
- val downloadTask = tasks["downloadGeoipDatabase"]
- tasks.forEach {
- if (it.name.startsWith("externalGolangBuild")) {
- it.dependsOn(downloadTask)
- }
- }
- }
|