Skip to content

Build properties Gradle plugin#

Sometimes you need to inspect information about the current build in your app code. The conventional way is to use custom fields in a BuildConfig or resource values.

android {
  buildTypes {
    release {
      buildConfigField("String", "GIT_COMMIT", "\"${commit}\"")
      resValue("string", "git_commit", "${commit}")
    }
    debug {
      // To avoid rebuilding
      buildConfigField("String", "GIT_COMMIT", "\"_\"")
      resValue("string", "git_commit", "_")
    }
  }
}

BuildConfig and R classes are used to compile the code. Any changes harm incremental compilation and build caching. This is a known problem: unstable task inputs.

To mitigate this issue the plugin uses assets to store properties. This approach is less harmful for incremental compilation and build caching.

Getting started#

1. Apply the plugin in the Android module's build script#

plugins {
    id("com.avito.android.build-properties")
}
Setup plugins

In the settings.gradle:

pluginManagement {
    repositories {
        mavenCentral()
    }
    resolutionStrategy {
        eachPlugin {
            String pluginId = requested.id.id
            if (pluginId.startsWith("com.avito.android")) {
                def artifact = pluginId.replace("com.avito.android.", "")
                useModule("com.avito.android:$artifact:$avitoToolsVersion")
            }
        }
    }
}

avitoToolsVersion could be exact version, or property in project's gradle.properties. The latest version could be found on project's release page.

2. Define properties in a build script#

build.gradle.kts

buildProperties {
    buildProperty("GIT_COMMIT", commit)
}

build.gradle

buildProperties {
    buildProperty("GIT_COMMIT", commit)
}

3. Add auto-generated file to the .gitignore config#

Content of this file depends on the build. It's no use to store it in VCS.

build-info.properties

4. Read properties in the code#

val properties = Properties()
context.assets.open("build-info.properties").use {
    properties.load(it)
}
val gitCommit = properties.getProperty("GIT_COMMIT")