Colton Idle
09/22/2024, 2:50 AMMax
09/22/2024, 7:39 AMMax
09/22/2024, 7:40 AMRok Oblak
09/22/2024, 8:48 AMval markDesktopProd = tasks.register("markDesktopProd") {
doLast {
isBrowserOrDesktop.set(true)
isProdBuild.set(true)
isLocalBuild.set(false)
println("Setting desktop to prod")
}
}
2. the tasks setup the right properties
i.e.
val customProp: Property<String> = project.objects.property(String::class.java).convention(null)
3. you register and use a task to generate a custom build config used by your targets
tasks.register("generateBuildConfig") {
val versionFile = rootDir.resolve(versionTextFile)
if (!versionFile.exists()) throw GradleException("Version file does not exist.")
doLast {
val (major, minor, patch) = versionFile.readVersion()
val customPropValue = customProp.get()
val formattedVersion = "$major.$minor.$patch"
val fileContent = """
package $packageName.config
// Autogenerated, do not modify
object AppBuildConfig {
const val VERSION = "$formattedVersion"
val CUSTOM_PROP: String? = $customPropValue
}
""".trimIndent()
val buildDir = layout.buildDirectory.get().asFile
val commonMainDir = file("${buildDir}/generated/kotlin/config/")
commonMainDir.mkdirs()
val generatedFile = commonMainDir.resolve("AppBuildConfig.kt")
generatedFile.delete()
generatedFile.writeText(fileContent)
val success = generatedFile.setReadOnly()
if (!success) {
logger.warn("Failed to set the file as read-only: ${generatedFile.absolutePath}")
}
}
}
kotlin.targets.all {
compilations.all { compileTaskProvider.dependsOn("generateBuildConfig") }
}
kotlin.sourceSets.getByName("commonMain") {
val buildDir = layout.buildDirectory.get().asFile
kotlin.srcDir("${buildDir}/generated/kotlin/config")
}
4. Now at runtime, you can refer to the per-flavour setup.Rok Oblak
09/22/2024, 8:52 AMColton Idle
09/23/2024, 12:53 PM