|
| 1 | +@file:Suppress("OPT_IN_USAGE") |
| 2 | + |
| 3 | +import com.github.gmazzo.buildconfig.generators.BuildConfigKotlinGenerator |
| 4 | +import com.squareup.kotlinpoet.AnnotationSpec |
| 5 | +import com.squareup.kotlinpoet.ClassName |
| 6 | +import com.squareup.kotlinpoet.TypeSpec |
| 7 | + |
| 8 | +plugins { |
| 9 | + alias(libs.plugins.android.multiplatform) |
| 10 | + alias(libs.plugins.kotlin.multiplatform) |
| 11 | + alias(libs.plugins.kotlin.ksp) |
| 12 | + id("com.github.gmazzo.buildconfig") |
| 13 | +} |
| 14 | + |
| 15 | +java.toolchain.languageVersion = JavaLanguageVersion.of(libs.versions.java.get()) |
| 16 | + |
| 17 | +kotlin { |
| 18 | + androidLibrary { // new AGP KMP plugin does not supports variants nor app |
| 19 | + compileSdk = 36 |
| 20 | + namespace = "com.github.gmazzo.buildconfig.demos.android" |
| 21 | + minSdk = 21 |
| 22 | + |
| 23 | + withHostTest { } // the old `androidUnitTest` variant |
| 24 | + withDeviceTest { } // the old `androidInstrumentedTest` variant |
| 25 | + } |
| 26 | + jvm() |
| 27 | + iosArm64() |
| 28 | + iosSimulatorArm64() |
| 29 | + js(IR) { nodejs() } |
| 30 | + wasmJs { nodejs() } |
| 31 | + applyDefaultHierarchyTemplate() |
| 32 | +} |
| 33 | + |
| 34 | +dependencies { |
| 35 | + "kspJvm"(libs.autoservice.ksp) |
| 36 | + "jvmMainCompileOnly"(libs.autoservice) |
| 37 | + commonMainImplementation(libs.uriKMP) |
| 38 | + commonTestImplementation(libs.kotlin.test) |
| 39 | +} |
| 40 | + |
| 41 | +buildConfig { |
| 42 | + buildConfigField("COMMON_VALUE", expect("aCommonValue")) // a constant for all platforms |
| 43 | + buildConfigField("PLATFORM", expect<String>()) // expect a platform specific value |
| 44 | + buildConfigField("DEBUG", expect(false)) // expect with a default |
| 45 | + buildConfigField("com.eygraber.uri.Uri", "ENDPOINT", |
| 46 | + expect(expression("Uri.parse(\"https://api.example.com\")"))) |
| 47 | + buildConfigField("PRODUCT_VALUE", expect<String?>(null)) |
| 48 | + |
| 49 | + forClass("i18n") { |
| 50 | + useKotlinOutput { topLevelConstants = true } |
| 51 | + |
| 52 | + buildConfigField("i18n_hello", expect("Hello")) |
| 53 | + buildConfigField("i18n_kind", expect<String>()) |
| 54 | + } |
| 55 | + |
| 56 | + forClass("Single") { |
| 57 | + buildConfigField("IS_MOBILE", expect(false)) |
| 58 | + } |
| 59 | + |
| 60 | + sourceSets.named("androidMain") { |
| 61 | + buildConfigField("PLATFORM", "android") |
| 62 | + buildConfigField("ANDROID_VALUE", "anAndroidValue") |
| 63 | + buildConfigField("DEBUG", false) |
| 64 | + forClass("i18n").buildConfigField("i18n_kind", "android") |
| 65 | + forClass("Single").buildConfigField("IS_MOBILE", true) |
| 66 | + } |
| 67 | + |
| 68 | + sourceSets.named("test") { |
| 69 | + buildConfigField("TEST_VALUE", "aTestValue") |
| 70 | + } |
| 71 | + |
| 72 | + sourceSets.named("jvmMain") { |
| 73 | + buildConfigField("PLATFORM", "jvm") |
| 74 | + buildConfigField("JVM_VALUE", "aJvmValue") |
| 75 | + forClass("i18n").buildConfigField("i18n_kind", "jvm") |
| 76 | + forClass("Single") // will inherit all defaults from `commonMain` |
| 77 | + } |
| 78 | + |
| 79 | + sourceSets.named("iosMain") { |
| 80 | + buildConfigField("PLATFORM", "ios") |
| 81 | + buildConfigField("IOS_VALUE", "anIOSValue") |
| 82 | + forClass("i18n").buildConfigField("i18n_kind", "ios") |
| 83 | + forClass("Single").buildConfigField("IS_MOBILE", true) |
| 84 | + } |
| 85 | + |
| 86 | + sourceSets.named("webMain") { |
| 87 | + // Customize the generator to add @JsName annotations to the generated object |
| 88 | + generator = object : BuildConfigKotlinGenerator() { |
| 89 | + override fun adaptSpec(spec: TypeSpec) = spec.toBuilder() |
| 90 | + .addAnnotation( |
| 91 | + AnnotationSpec.builder(ClassName.bestGuess("kotlin.js.JsName")) |
| 92 | + .addMember("name = %S", spec.name!!) |
| 93 | + .build() |
| 94 | + ) |
| 95 | + .build() |
| 96 | + } |
| 97 | + |
| 98 | + buildConfigField("PLATFORM", "web") |
| 99 | + buildConfigField("WEB_VALUE", "aWebValue") |
| 100 | + forClass("i18n").buildConfigField("i18n_kind", "web") |
| 101 | + forClass("Single") // will inherit all defaults from `commonMain` |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +tasks.named("compileTestDevelopmentExecutableKotlinJs") { |
| 106 | + notCompatibleWithConfigurationCache("uses Task.project") |
| 107 | +} |
0 commit comments