Search code examples
crashlyticsgoogle-fabric

How do I prevent versions from being generated for app in Crashlytics with Gradle build


I'd like to have build-time control of whether a build will create a version for my app. I currently have Crashlytics enabled only for specific Jenkins jobs (in fact, the plugin isn't even applied if it isn't being built by my Release job), and my Jenkins PR job builds both debug and release.

Unfortunately this still seems to cause the feature-branch builds from the PR job to appear in the version list for my app - which clutters up the search bar when I'm trying to filter for recently released versions.

How can I make it so that nothing gets sent to the crashlytics servers? Do I need to segregate the Fabric.with() call so that its not run with in the crashlytics-disabled case? As opposed to currently, it is run with:

Crashlytics crashlyticsKit = new Crashlytics.Builder()
    .core(new CrashlyticsCore.Builder().disabled(!enableCrashlytics()).build())
    .build();
Fabric.with(this, crashlyticsKit);

Would that be enough?

Note: I have seen this question What is an effective way of segregating dev builds in Crashlytics? and Mike's response is not feasable


Solution

  • Mike from Fabric here.

    You need to not initialize Fabric at all. You're init code prevents Crashlytics from being initialized, but not Answers, so data will still be sent.

    Alternatively, separate out your builds so that only for production builds does data flow into an organization for your prod builds only and then have all other builds flow into the same app in a different organization. Something like this:

    // The following code allows an app to report Crashlytics crashes separately 
    // for release and debug buildTypes when using Gradle. This code should be inserted 
    // into the specified locations within your build.gradle (Module:app) file
    
        // The buildTypes { } block should be inserted inside the android { } block
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                ext.crashlyticsApiSecret = "release api secret"
                ext.crashlyticsApiKey = "release api key"
            }
            debug {
                ext.crashlyticsApiSecret = "debug api secret"
                ext.crashlyticsApiKey = "debug api key"
            }
        }
    
    // The following code can be inserted at the bottom of your build.gradle file
    import com.crashlytics.tools.utils.PropertiesUtils
    
    File crashlyticsProperties = new File("${project.projectDir.absolutePath}/fabric.properties")
    android.applicationVariants.all { variant ->
        def variantSuffix = variant.name.capitalize()
        def generateResourcesTask = project.tasks.getByName("fabricGenerateResources${variantSuffix}")
        def generatePropertiesTask = task("fabricGenerateProperties${variantSuffix}") << {
            Properties properties = new Properties()
            println "...copying apiSecret for ${variant.name}"
            properties.put("apiSecret", variant.buildType.ext.crashlyticsApiSecret)
            println "...copying apiKey for ${variant.name}"
            properties.put("apiKey", variant.buildType.ext.crashlyticsApiKey)
            PropertiesUtils.injectPropertyInFile(crashlyticsProperties, properties, "")
        }
        generateResourcesTask.dependsOn generatePropertiesTask
    }