16

I'm currently implementing the API Key switching script suggested here, except with build types instead of flavors. My build.gradle looks like this:

...
buildTypes {
    debug {
        ...
        set("crashlyticsApiKey", "API_KEY_1")
        set("crashlyticsApiSecret", "API_SECRET_1")
    }
    release {
        ...
        set("crashlyticsApiKey", "API_KEY_2")
        set("crashlyticsApiSecret", "API_SECRET_2")
    }
}
...
productFlavors{...}
...
File crashlyticsProperties = new File("${project.projectDir.absolutePath}/crashlytics.properties")

applicationVariants.all { variant ->
    variant.productFlavors.each { flavor ->
        def variantSuffix = variant.name.capitalize()
        def generateResourcesTask = project.tasks.getByName("crashlyticsGenerateResources${variantSuffix}")
        def generatePropertiesTask = task("crashlyticsGenerateProperties${variantSuffix}") << {
            Properties properties = new Properties()
            println "...copying apiKey for ${variant.name}"
            properties.put("apiKey", variant.buildType.crashlyticsApiKey)
            println "...copying apiSecret for ${variant.name}"
            properties.put("apiSecret", variant.buildType.crashlyticsApiSecret)
            properties.store(new FileWriter(crashlyticsProperties), "")
        }
        generateResourcesTask.dependsOn generatePropertiesTask
        def cleanResourcesTask = project.tasks.getByName("crashlyticsCleanupResourcesAfterUpload${variantSuffix}")
        cleanResourcesTask.doLast {
            println "...removing crashlytics.properties"
            crashlyticsProperties.delete()
        }
    }
}
...

The gradle file builds successfully, and crashlytics.properties updates with the correct information according to the build type. This method of using crashlytics.properties was suggested here, and appears to work without any other updates other than the inclusion of dependencies in the gradle file. However, when Crashlytics.start(this) is called from the main activity, I get a runtime exception:

java.lang.RuntimeException: Unable to create application com.lookout.LookoutApplication: java.lang.IllegalArgumentException: Crashlytics could not be initialized, API key missing from AndroidManifest.xml. Add the following tag to your Application element
<meta-data android:name="com.crashlytics.ApiKey" android:value="YOUR_API_KEY"/>

Stripping it down to a static crashlytics.properties file (i.e. removing the dynamic script in the gradle file and just having one apiKey and apiSecret in crashlytics.properties) produces the same error, even though it builds successfully.

Is there some change to the AndroidManifest or the build.gradle file I should be making to point it towards crashlytics.properties?

Community
  • 1
  • 1

2 Answers2

13

Works fine with:

# Fabric properties file: app/fabric.properties
apiSecret=xx68f6074dxxxxxc11dxxx97c172e8ebf0
apiKey=xxxe76c4xxxx97e8cxxxx0135e9d46f5a2xxx

Add on .gitignore (for open source projects)

REMOVE entry on AndroidManifest.xml:

<meta-data
    android:name="io.fabric.ApiKey"
    android:value="xxx6c41xxx6ec601xxxd4xxxa2" />

Oficial documentation: https://docs.fabric.io/android/fabric/settings/working-in-teams.html#android-projects

Sakiboy
  • 7,252
  • 7
  • 52
  • 69
Hpsaturn
  • 2,702
  • 31
  • 38
  • 2
    Not working on my end with fabric.properties. Still getting the message that fabric cannot be initialized due to lack of manifest entry – BinaryWave Jan 16 '18 at 21:55
6

While this is not the answer to the original question (since Instant Run didn't exist in 2014), you may find that Instant Run can cause problems. My process was:

  • Install Fabric plugin
  • Generate Crashlytics code (including API key in the manifest)
  • Switch to fabric.properties file
  • Spend an hour trying to figure out why it wasn't working
  • Disable Instant Run -> Rebuild -> Install -> Success

I'm on Android Studio 2.0.0-beta6. This will likely be resolved in future, but this was the only resource I could find online with the same problem so hopefully I can save someone else that hour.

Adam S
  • 16,144
  • 6
  • 54
  • 81
  • 3
    Disabling InstantRun fixed the issue for me. Wish I found this post prior to spend an hour trying prophesy. – Soumya Nov 01 '17 at 00:07