Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android and the Fabric (Crashlytics) plugin always generates a UUID (Gradle Kotlin DSL)

I want Fabric to stop generating a UUID on each build. What used to work with Gradle's Groovy DSL does not work with the newer Kotlin DSL. How can I achieve my goal with the Kotlin DSL?

(Gradle version 4.10.2, Fabric 1.25.4)

According to Fabric's documentation, you can add the following to your app's build script

android {
    buildTypes {
        debug {
          // Only use this flag on builds you don't proguard or upload
          // to beta-by-crashlytics
          ext.alwaysUpdateBuildId = false

and this works. It prevents Fabric from generating a UUID on each debug build. However, if I convert my build script to Kotlin DSL, the following doesn't work

android {
    buildTypes {
        getByName("debug") {
          // Only use this flag on builds you don't proguard or upload
          // to beta-by-crashlytics
          ext.set("alwaysUpdateBuildId", false)

Fabric ignores this value, now.

I have tried variations, such as the following:

project.ext.set("alwaysUpdateBuildId", false)
rootProject.ext.set("alwaysUpdateBuildId", false)
val alwaysUpdateBuildId by extra(false)
val alwaysUpdateBuildId by project.extra(false)
val alwaysUpdateBuildId by rootProject.extra(false)

None work.

For further reference, the Gradle task generating this value appears to be named :app:fabricGenerateResourcesDebug, and has type DefaultTask.

like image 449
AutonomousApps Avatar asked Nov 09 '18 22:11

AutonomousApps


2 Answers

As Martin Rajniak mentioned, you can only call extra on ExtensionAware objects, with BuildType not being declared as one.

However, during runtime, build types actually are ExtensionAware, which is why this works in Groovy due to its dynamicity, but not in Kotlin where extra in this scope will reference the Project's extensions.

In order to achieve this without Groovy, we can simply cast the build type to ExtensionAware:

android {
    buildTypes {
        getByName("debug") {
            (this as ExtensionAware).extra["alwaysUpdateBuildId"] = false
        }
    }
}
like image 155
Ivan Tokić Avatar answered Oct 13 '22 11:10

Ivan Tokić


I have found a workaround to this problem. Create a file, fabric.gradle (Groovy build script!) and place it in your project structure somewhere. It will have the following contents:

// or "com.android.library"
project.pluginManager.withPlugin("com.android.application") {
    android.buildTypes.debug.ext.alwaysUpdateBuildId = false
}

Now, in the build script for your module (let's call it app/build.gradle.kts), apply this script plugin:

apply(from = "path/to/fabric.gradle")

This workaround is based on the advice here, in the Kotlin DSL primer.

like image 43
AutonomousApps Avatar answered Oct 13 '22 11:10

AutonomousApps