What's the best way to hide the Crashlytics key?

I put my crashlytics key in xml

and I got this error:

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

Crashlytics developer tools error.

Below is my code in AndroidManifest.xml

.

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

      

What's the best way to hide it?

+3


source to share


3 answers


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



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

      

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

${crashlytics}

      

You may need to tweak this code to make it work for your needs, but it should be enough to get you started. And don't forget to add local.properties to your .gitignore (if not already there!) Hope it helps.

+1


source


There is no need to do it in such a complicated way as in the accepted answer.

According to the official docs, you can simply remove your API key from the manifest and put it in a file fabric.properties

where your secret already lies in the following form:

apiKey=fabric_api_key
apiSecret=fabric_api_secret

      



And it's all. The Fabric (or Gradle) plugin will automatically do all the work needed inside the hood.

Ps Don't forget to save the file fabric.properties

outside of your source control

+4


source


Putting both keys in fabric.properties

well didn't work for me and fabric couldn't find the api key there and wanted me to put it in the Manifest. Then I discovered this solution:

Put io.fabric.ApiKey=676897890789790...

in yourlocal.properties

Add this to AndroidManifest:

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

      

Then in the application 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 closing defaultConfig:

defaultConfig {
   .....
         resValue "string", "fabric_api_key", fabricApiKey
    }

      

This way, you can also hide the google maps key from source control for open source projects and still keep the application ready for continuous integration or people wanting to try it out ...

0


source







All Articles