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

2

6

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.

Bearnard answered 9/11, 2018 at 22:3 Comment(4)
I guess the question is on which object should the extra property be set.Styrax
I recently found myself wondering if the actual property name might be isAlwaysUpdateBuildId, but I haven't tested yet.Bearnard
Yeah, that doesn't work.Bearnard
I had a similar issue - and the problem is that you can call flavor.ext.get/set in groovy and you can't do that in kotlin, because extras can be fetched only from ExtensionAware types which Flavor is not (docs.gradle.org/current/dsl/…). Apparently in Groovy there is some Google Build Plugin magic to make it work for Flavor.Keelby
U
11

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
        }
    }
}
Undersecretary answered 18/4, 2019 at 12:6 Comment(1)
Was that changed recently? Using Gradle 5.6.2 I can access the extra extensions property from there without the casting like extra["alwaysUpdateBuildId"] = false but, surprisingly, it doesn't work, i.e. the code changes still cannot be applied and Android Studio displays the error about build ID being changed. Any idea what's wrong? That casting seems redundant to me at the moment.Deboer
B
4

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.

Bearnard answered 7/12, 2018 at 21:11 Comment(1)
I have finally had time to experiment, and as such have switched my accepted answer to https://mcmap.net/q/1616568/-android-and-the-fabric-crashlytics-plugin-always-generates-a-uuid-gradle-kotlin-dsl, as it is simpler.Bearnard

© 2022 - 2024 — McMap. All rights reserved.