问题
I put my crashlytics key in xml
and I got this error:
Error:Execution failed for task ':app:fabricGenerateResourcesDebug'.
Crashlytics Developer Tools error.
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?
回答1:
Place your API key in local.properties.
crashlytics.key=api_key_here
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")]
}
}
In your manifest, you can now access the API key as an injected variable with the following syntax:
${crashlytics}
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.
回答2:
There is no need to do it in a such complex way as in accepted answer.
According to the official docs, you can simply remove your API key from Manifest and put to fabric.properties
file, where your secret already lies in a next form:
apiKey=fabric_api_key
apiSecret=fabric_api_secret
And that is it. Fabric plugin (or Gradle task) will automatically do all the needed work inside the hood.
P.s. Don't forget to keep your fabric.properties
file outside of your Version Control System
回答3:
Well placing both keys to fabric.properties
didn't work for me and fabric could not found api key there and wanted, me to put it in Manifest. Then I discovered this solution:
Place io.fabric.ApiKey=676897890789790...
to your local.properties
To AndroidManifest add this:
<meta-data
android:name="io.fabric.ApiKey"
android:value="@string/fabric_api_key" />
Then in app module build.gradle
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def fabricApiKey = properties.getProperty('io.fabric.ApiKey')
if (fabricApiKey == null) fabricApiKey = "Place io.fabric.ApiKey to local.properties"
then in defaultConfig closure:
defaultConfig {
.....
resValue "string", "fabric_api_key", fabricApiKey
}
This way you can hide also google maps key from source control for open source projects and still keep application buildable for continuous integration or people wanting to try it out...
来源:https://stackoverflow.com/questions/45428622/what-is-the-best-way-to-hide-crashlytics-key