Crashlytics(Fabric)为应用程序变体(构建类型,产品风格) [英] Crashlytics (Fabric) separate organizations for application variants (build types, product flavors)
问题描述
我有一个具有多种产品口味的项目,我希望将织物对每种产品口味使用单独的组织。
我尝试使用Android Studio Fabric插件集成Fabric。它增加了
< meta-data
android:name =io.fabric.ApiKey
android:value =DEFAULT_ORGANIZATION_API_KEY/>
进入 AndroidManifest.xml
of main
源码集。
我决定在应用程序变体特定源集中重写此条目:
< manifest xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools = http://schemas.android.com/tools\">
< application>
将元数据
机器人:名称= io.fabric.ApiKey
机器人:值= SECOND_ORGANIZATION_API_KEY
工具:替换= 机器人:值/> ;
< / application>
< / manifest>
然后我发现Fabric Gradle插件生成 crashlytics.properties
在构建过程中使用fabric api secret(AKA build secret),我应该将此文件包含到源代码控制中。但是每次构建特定的应用程序变体时都会覆盖此文件,因为api秘密对于每个应用程序都是唯一的。
如何使用单独的组织为每个应用程序变体集成Fabric?在构建 fabricGenerateResources
任务期间,调用它并查找一个文件命名为 fabric.properties
,内容如下:
apiSecret = YOUR_BUILD_SECRET
apiKey = YOUR_API_KEY
所以我们需要的是生成 fabric.properties
在此之前的文件。
我发现这个解决方案,并稍加修改以完全支持应用程序变体,不仅可以构建类型。
将此代码添加到 android
build.gradle $ c $>部分:
文件crashlyticsProperties =新文件($ {project.projectDir.absolutePath} /fabric.properties)
applicationVariants.all {variant - >
variant.productFlavors.each {flavor - >
def variantSuffix = variant.name.capitalize()
def generatePropertiesTask = task(fabricGenerateProperties $ {variantSuffix})<< {
Properties properties = new Properties()
properties.put(apiKey,flavor.fabricApiKey)
properties.put(apiSecret,flavor.fabricApiSecret)
属性。 (新的FileWriter(crashlyticsProperties),)
}
def generateResourcesTask = project.tasks.getByName(fabricGenerateResources $ {variantSuffix})
generateResourcesTask.dependsOn generatePropertiesTask
generateResourcesTask.doLast {
println移除fabric.properties
crashlyticsProperties.delete()
}
}
}
它遍历应用程序变体,并为每个应用程序变体创建生成 fabric.properties
文件和任务,在Fabric Gradle插件生成应用程序资源后,删除此文件。
现在您需要的是定义产品风格或特定生成类型 F A
productFlavors {
flavor1 {
ext.fabricApiKey =FLAVOR1_API_KEY
ext.fabricApiSecret =FLAVOR1_API_SECRET
}
}
ext
是 ExtraPropertiesExtention 通过每 ExtensionAware 对象。它允许将新属性添加到现有对象。在我的情况下, flavor1
是 ExtensionAware
对象,它可以通过使用 ext .someProperty =value
语法,后面这些属性可以用作 flavor.someProperty,flavor.fabricApiKey
。
此外,最好将 fabric.properties
加入 .gitignore
。
如果您在调试过程中使用它来禁用Crashlytics,请不要忘记从调试构建类型中删除 ext.enableCrashlytics = false
。您可以在 Application.onCreate
中禁用它:
$ b
Fabric .with(this,new Crashlytics.Builder()。core(
new CrashlyticsCore.Builder()。disabled(BuildConfig.DEBUG).build())。build());
This is self-answered question to share my knowledge.
I have a project with multiple product flavors and I want to integrate Fabric using separate organizations for each product flavor.
I tried to integrate Fabric using Android Studio Fabric Plugin. It adds
<meta-data
android:name="io.fabric.ApiKey"
android:value="DEFAULT_ORGANIZATION_API_KEY" />
entry to AndroidManifest.xml
of main
source set.
I decided to rewrite this entry in application variant specific source sets:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application>
<meta-data
android:name="io.fabric.ApiKey"
android:value="SECOND_ORGANIZATION_API_KEY"
tools:replace="android:value" />
</application>
</manifest>
Then I discovered that Fabric Gradle plugin generates crashlytics.properties
file with fabric api secret (AKA build secret) during build and I should include this file to source control. But this file is overwritten each time I build specific application variant because api secret is unique for each application.
How can I integrate Fabric using separate organizations for each application variant?
During the build fabricGenerateResources
task is called and it looks for a file named fabric.properties
with following content:
apiSecret=YOUR_BUILD_SECRET
apiKey=YOUR_API_KEY
So all we need is to generate fabric.properties
file before this.
I found this solution and slightly modified it to fully support application variants not only build types.
Add this code to android
section of build.gradle
:
File crashlyticsProperties = new File("${project.projectDir.absolutePath}/fabric.properties")
applicationVariants.all { variant ->
variant.productFlavors.each { flavor ->
def variantSuffix = variant.name.capitalize()
def generatePropertiesTask = task("fabricGenerateProperties${variantSuffix}") << {
Properties properties = new Properties()
properties.put("apiKey", flavor.fabricApiKey)
properties.put("apiSecret", flavor.fabricApiSecret)
properties.store(new FileWriter(crashlyticsProperties), "")
}
def generateResourcesTask = project.tasks.getByName("fabricGenerateResources${variantSuffix}")
generateResourcesTask.dependsOn generatePropertiesTask
generateResourcesTask.doLast {
println "Removing fabric.properties"
crashlyticsProperties.delete()
}
}
}
It iterates over application variants and for each application variant creates task that generates fabric.properties
file and task that deletes this file after Fabric Gradle plugin generates application resources.
All you need now is to define product flavor or build type specific fabricApiKey
and fabricApiSecret
:
productFlavors {
flavor1 {
ext.fabricApiKey = "FLAVOR1_API_KEY"
ext.fabricApiSecret = "FLAVOR1_API_SECRET"
}
}
ext
is an ExtraPropertiesExtention object provided by every ExtensionAware object. It allows new properties to be added to existing object. In my case flavor1
is ExtensionAware
object and it can be extended with new properties by using ext.someProperty = "value"
syntax and later these properties can be used as flavor.someProperty, flavor.fabricApiKey
.
Also it's better to include fabric.properties
to .gitignore
.
And do not forget to remove ext.enableCrashlytics = false
from debug build type if you used it to disable Crashlytics during debug. Instead of this you can disable it in Application.onCreate
:
Fabric.with(this, new Crashlytics.Builder().core(
new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build()).build());
这篇关于Crashlytics(Fabric)为应用程序变体(构建类型,产品风格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!