Crashlytics(Fabric)针对应用程序变体(构建类型,产品风格)分离组织

时间:2015-12-25 10:34:41

标签: android gradle crashlytics twitter-fabric android-productflavors

这是一个自我回答的问题,可以分享我的知识。

我有一个包含多种产品口味的项目,我希望使用不同的组织为每种产品风格整合Fabric。

我尝试使用Android Studio Fabric插件集成Fabric。它增加了

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

输入AndroidManifest.xml来源main来源{/ 1}。

我决定在应用程序变体特定的源集中重写此条目:

<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>

然后我发现Fabric Gradle插件在构建期间生成带有fabric api secret(AKA build secret)的crashlytics.properties文件,我应该将此文件包含在源代码控制中。但是每次构建特定的应用程序变体时都会覆盖此文件,因为api secret对于每个应用程序都是唯一的。

如何针对每种应用程序变体使用单独的组织集成Fabric?

3 个答案:

答案 0 :(得分:27)

在构建fabricGenerateResources任务期间,它会查找名为fabric.properties的文件,其中包含以下内容:

apiSecret=YOUR_BUILD_SECRET
apiKey=YOUR_API_KEY

所以我们需要的是在此之前生成fabric.properties文件。

我找到了this solution并略微修改了它,以完全支持应用程序变体,而不仅仅是构建类型。

将此代码添加到android的{​​{1}}部分:

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() } } } 文件的任务,并在Fabric Gradle插件生成应用程序资源后删除此文件。

现在您只需定义特定产品风格或构建类型fabric.propertiesfabricApiKey

fabricApiSecret

productFlavors { flavor1 { ext.fabricApiKey = "FLAVOR1_API_KEY" ext.fabricApiSecret = "FLAVOR1_API_SECRET" } } 是每个ExtraPropertiesExtention对象提供的ExtensionAware对象。它允许将新属性添加到现有对象。在我的情况下,extflavor1对象,可以使用ExtensionAware语法使用新属性进行扩展,之后这些属性可以用作ext.someProperty = "value"

最好将flavor.someProperty, flavor.fabricApiKey加入fabric.properties

如果您在调试期间使用它来禁用Crashlytics,请不要忘记从调试版本类型中删除.gitignore。您可以在ext.enableCrashlytics = false中禁用它:

Application.onCreate

答案 1 :(得分:7)

如果您不反对使用应用程序ID后缀,则不需要单独的组织。崩溃和答案将被视为单独的应用程序。

例如,假设我的应用程序ID为io.example

在build.gradle中:

buildTypes {
  debug {
    applicationIdSuffix ".debug"
  }
  release {
    //options
  }
}

将调试版本部署到设备或模拟器后,在Fabric网站上您将看到两个应用程序:

  • io.example
  • io.example.debug

这种方法的一个好处是,您还可以分别跟踪其他构建风格:io.exmaple.freeio.exmaple.paidio.example.exterprise等。

答案 2 :(得分:0)

一个更简单的解决方案(也与Gradle 5.x +兼容)是为需要唯一的Fabric API密钥和机密的每个构建变体创建单独的fabric.properties文件。将fabric.properties文件创建为:

#Contains API Secret used to validate your application. Commit to internal source control; avoid making secret public.
apiSecret=YOUR_API_SECRET
apiKey=YOUR_API_KEY

用构建变体的API密钥替换YOUR_API_SECRET,用构建变体的API密钥替换YOUR_API_KEY

然后将每个变体的fabric.properties放在项目src/variant文件夹下,例如app/src/debugapp/src/releaseSee documentation关于构建变体以获取更多详细信息。

在构建时,将使用正在构建的变体的fabric.properties

相关问题