隐藏Crashlytics密钥的最佳方法是什么? [英] What is the best way to hide Crashlytics key?

查看:83
本文介绍了隐藏Crashlytics密钥的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的crashlytics密钥放在 xml 中,但出现此错误:

I put my crashlytics key in xml and I got this error:


错误:任务':app:fabricGenerateResourcesDebug'的执行失败。

Error:Execution failed for task ':app:fabricGenerateResourcesDebug'.


Crashlytics开发人员工具错误。

Crashlytics Developer Tools error.


以下是我在 AndroidManifest.xml 中的预期代码。

The following is my intended code in AndroidManifest.xml.

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

隐藏它的最佳方法是什么?

What is the best way to hide it?

推荐答案

将您的API密钥放置在local.properties中。

Place your API key in local.properties.

crashlytics.key=api_key_here

在您的build.gradle中,添加以下Groovy方法:

In your build.gradle, add this Groovy method:

def getLocalProperty(String propertyName) {
    def propsFile = rootProject.file('local.properties')
    if (propsFile.exists()) {
        def props = new Properties()
        props.load(new FileInputStream(propsFile))
        return props[propertyName]
    } else {
        return ""
    }
}

然后添加一个清单占位符,例如:

And then add a manifest placeholder, for example:

android {
    defaultConfig {
        manifestPlaceholders = [crashlytics:getLocalProperty("crashlytics.key")]
    }
}

在清单中,您现在可以使用以下语法将API密钥作为注入变量访问:

In your manifest, you can now access the API key as an injected variable with the following syntax:

${crashlytics}

您可能需要调整此代码至 使它可以满足您的需求,但这足以让您入门。并确保将local.properties添加到您的.gitignore中(如果尚未添加!)希望能有所帮助。

You might need to tweak this code to get it to work for your needs, but this should be enough to get you started. And make sure to add local.properties to your .gitignore (if it isn't already!) Hope that helps.

这篇关于隐藏Crashlytics密钥的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆