buildkonfig-gradle-plugin
A Gradle plugin that generates a BuildKonfig Kotlin object at build time - like Android's BuildConfig, but for any Kotlin project (JVM, Multiplatform, Android).
Fields can be constant, overridden per build type (debug/release), or scoped to named dimensions (e.g. environment, region) with their own variants.
Plugin ID: com.bitsycore.konfig
Group: com.bitsycore
Artifact: konfig-gradle-plugin
Version: 0.8.0
JVM target: 17
Setup
1. Configure plugin resolution
The plugin is published to maven.bitsycore.com (no authentication) and to
GitHub Packages as a fallback (requires a GitHub PAT with read:packages).
settings.gradle.kts:
pluginManagement {
repositories {
maven("https://maven.bitsycore.com/releases")
gradlePluginPortal()
}
}
GitHub Packages fallback (authenticated)
Store credentials in ~/.gradle/gradle.properties - never commit them:
gpr.user=YOUR_GITHUB_USERNAME
gpr.key=YOUR_GITHUB_PERSONAL_ACCESS_TOKEN
pluginManagement {
repositories {
maven {
name = "GitHubPackages"
url = uri("https://maven.pkg.github.com/bitsycore/bitsykonfig")
credentials {
username = providers.gradleProperty("gpr.user").orNull ?: System.getenv("GPR_USER")
password = providers.gradleProperty("gpr.key").orNull ?: System.getenv("GPR_KEY")
}
}
gradlePluginPortal()
}
}
2. Declare the plugin
Using a version catalog (libs.versions.toml):
[versions]
konfig = "0.8.0"
[plugins]
konfig = { id = "com.bitsycore.konfig", version.ref = "konfig" }
build.gradle.kts:
plugins {
alias(libs.plugins.konfig)
}
Or inline:
plugins {
id("com.bitsycore.konfig") version "0.8.0"
}
Basic usage
The plugin auto-detects your project's group and name to set a default package. The generated file is placed in build/generated/konfig/ and wired into your source sets automatically.
konfig {
field("APP_NAME", "My App")
field("VERSION_CODE", 42)
field("ENABLE_LOGGING", false).debug(true)
}
Generated output (BuildKonfig.kt):
public object BuildKonfig {
const val BUILD_TYPE: String = "release"
const val MODULE_NAME: String = "my-app"
const val IS_DEBUG: Boolean = false
const val APP_NAME: String = "My App"
const val VERSION_CODE: Int = 42
const val ENABLE_LOGGING: Boolean = false
}
In debug builds (-Pkonfig.buildtype=DEBUG), ENABLE_LOGGING becomes inline val ENABLE_LOGGING: Boolean get() = true.
Configuration reference
Object settings
konfig {
objectPackage = "com.example.app" // default: derived from group + project name
objectName = "BuildKonfig" // default: "BuildKonfig"
objectVisibility = Visibility.INTERNAL // default: Visibility.PUBLIC
}
Supported field types
| Kotlin type |
Example |
String |
field("BASE_URL", "https://example.com") |
Boolean |
field("FEATURE_X", false) |
Int |
field("TIMEOUT", 30) |
Long |
field("MAX_SIZE", 1_000_000L) |
Float |
field("RATIO", 1.5f) |
Double |
field("PI", 3.14159) |
Byte, Short, Char |
field("SEPARATOR", ':') |
List<T> |
field("HOSTS", listOf("api.example.com")) |
Set<T> |
field("FEATURES", setOf("search", "export")) |
Map<K, V> |
field("PORTS", mapOf("https" to 443)) |
Array<T> |
field("REGIONS", arrayOf("eu", "us")) |
| Primitive arrays |
field("RETRIES", intArrayOf(1, 3, 5)) |
| Nullable types |
field<String?>("OPTIONAL_URL", null) |
| Enum values |
field("DAY", java.time.DayOfWeek.MONDAY) (JVM) |
UByte, UShort, UInt, ULong |
field("MAX_ID", ULong.MAX_VALUE) |
Lists, sets, maps, and arrays
Collections generate ordinary val properties initialized with listOf,
setOf, mapOf, or an array factory. They work with providers, build-type overrides,
dimension variants, common {}, and flat dimensions:
konfig {
field("HOSTS", listOf("api.example.com")).debug(listOf("localhost"))
field("PORTS", mapOf("http" to 80, "https" to 443))
field("REGIONS", arrayOf("eu", "us"))
field("RETRIES", arrayOf(1, 3, 5))
field("EMPTY", emptyList<String>())
field("ROUTES", mapOf("primary" to listOf("/health", "/status")))
}
val HOSTS: List<String> = listOf<String>("api.example.com")
val PORTS: Map<String, Int> = mapOf<String, Int>("http" to 80, "https" to 443)
val REGIONS: Array<String> = arrayOf<String>("eu", "us")
val RETRIES: IntArray = intArrayOf(1, 3, 5)
val EMPTY: List<String> = listOf<String>()
val ROUTES: Map<String, List<String>> = mapOf<String, List<String>>("primary" to listOf<String>("/health", "/status"))
Arrays of non-null primitive elements are specialized: Array<Int> generates
IntArray with intArrayOf. The same applies to Boolean, Byte, Short, Char,
Long, Float, and Double. Existing primitive arrays retain their primitive type.
Arrays with nullable elements such as Array<Int?> stay generic. Specialization also applies
inside nested collections, so List<Array<Int>> generates List<IntArray>.
Empty collections retain the type supplied in the DSL. Nullable elements,
nested lists/sets/maps/arrays, and mixed values explicitly typed as Any are
supported; values must ultimately be supported scalars or collections.
Custom objects other than enums are rejected. Lists, sets, and maps expose read-only Kotlin
interfaces; generated arrays are mutable. Collection properties are not const.
Build-type scope overrides must keep the declared field type.
Array policies
konfig {
specializeArrays = false // Keep Array<Int> as Array<Int>; default is true
copyArraysOnAccess = true // Return fresh array-containing values; default is false
field("RETRIES", arrayOf(1, 3, 5))
}
Explicit primitive arrays such as intArrayOf(1, 2) stay primitive under either
policy. Both policies apply recursively inside collections. With copying enabled,
an array-containing field uses a getter that recreates the whole value on every
access. This prevents a caller's array mutations from affecting later reads, at
the cost of allocations. Fields without arrays retain their usual declarations.
Nullable fields, enums, and unsigned numbers
konfig {
field<String?>("OPTIONAL_URL", null).debug("http://localhost")
field<String?>("TOKEN", providers.gradleProperty("token"))
field("MAX_ID", ULong.MAX_VALUE)
}
An explicit null generates a present nullable val. An absent Gradle provider
still omits the field; Gradle providers cannot carry a present null. Specify the
nullable type explicitly, including in debug {} / release {} declarations
that override a nullable field. Nullable properties are ordinary val properties.
Enums generate a qualified reference such as java.time.DayOfWeek.MONDAY and
support nullable values and nesting in collections. The enum must be accessible
both from the build script and from the consuming source set. A build-script-only
enum is not automatically copied into application code. Unsigned scalar numbers
and collections of those numbers are supported; unsigned array classes are not.
Build-type overrides
Three equivalent forms:
konfig {
// Fluent handle - default + one or both overrides
field("BASE_URL", "https://prod.example.com").debug("https://dev.example.com")
// Scope blocks - build type is fixed, field() returns Unit, no chaining
debug { field("MOCK_API", true) }
release { field("MOCK_API", false) }
}
field() inside debug {} / release {} blocks intentionally returns Unit - the build type is already fixed by the enclosing scope, so .debug() / .release() chaining is impossible by design.
Dimensions
Dimensions let you select a named variant at build time (e.g. env=prod, env=dev). Each active dimension generates a nested object inside BuildKonfig.
konfig {
dimension("env", defaultTo = "prod") {
common {
field("TIMEOUT", 30)
debug { field("TIMEOUT", 5) }
}
variant("prod") {
field("BASE_URL", "https://prod.example.com")
field("ANALYTICS", true)
}
variant("dev") {
field("BASE_URL", "https://dev.example.com")
field("ANALYTICS", false)
}
}
}
Generated output (with env=dev, debug build):