-
-
Save alexsinger/2b5b1b7ae2d2fca1ffdb to your computer and use it in GitHub Desktop.
// 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 | |
} |
Inspecting the AndroidManifest.xml within the apk package, I see the apiKey is in fact added to the final manifest in the build. Good to know how this works! Thanks.
What changes we need to do in AndroidManifest.xml file ?
OR You could just create an AndroidManifest.xml
per build flavor and add <metadata>
tag corresponding for key to use within that flavor. So essentially you'd have:
src
debug
AndroidManifest.xml // contains key for debug
release
AndroidManifest.xml // contains key for release
Then gradle does its magic and takes resources, be it classes, resources, manifests, etc. depending on current build flavor and merges their manifests. You can read about building for flavors here.
@jeffdgr8
I do have the question of how the apiKey in fabric.properties works. Does this replace the need for having it in the AndroidManifest.xml?
+1
How do we separate out fabric.properties for debug and release build?
Hi.
Can you please, specify where to get crashlyticsApiSecret. I can see where to get crashlyticsApiKey. For example, with using of fabric plugin for IDE
@DmitriyYakovlev, you can get the crashlyticsApiSecret on
Select your desired organisation and right bellow the name of your organization you will find API key and Build Secret.
Hope it helps.
If you get an error like could not find method leftshift()
after applying the above thing then.
Replace leftshift(<<) with doLast
.
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}") doLast {
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
}
OK, I figured it out. I needed to remove
ext.enableCrashlytics = false
from the debug build type for that gradle task to be generated. Testing it out now.I do have the question of how the apiKey in fabric.properties works. Does this replace the need for having it in the AndroidManifest.xml?