Hide your api keys from your android manifest file with Flutter using local.properties

Hide your api keys from your android manifest file with Flutter using local.properties

Table of contents

No heading

No headings in the article.

A common problem we face is the protection of our private data such as API keys. When working with Flutter, you may need to integrate a third-party service that requires you to add your API key to your manifest file.

You wouldn't want to put your API keys in the source code. Instead, you would like to embed them into an internal file ignored by your source control and use them through your app. For Android, there's already a standard file that's used for this purpose called local.properties.

The AndroidManifest.xml is an XML file thus we cannot write scripts there. Instead, we can write a small script in build.gradle file, read the API key from the local.properties file and assign it to a variable. AndroidManifest will be able to read that variable.

Let's dive into the code!

Assuming we want to integrate Segment, we would need to add the write key to our AndroidManifest.xml file, instead of writing the clear API key, we would write :

<meta-data android:name="com.claimsforce.segment.WRITE_KEY" android:value="${segmentWriteKey}" />

And then register the API Key in the local.properties file under the variable named "segment.writeKey" like:

sdk.dir=
flutter.sdk=
flutter.buildMode=debug
flutter.versionName=1.0.0
flutter.versionCode=1
segment.writeKey=<SEGMENT API KEY GOES HERE> # We've added this line

segmentWriteKey is the name of the variable we are going to create in the module-level build.gradle file (located in android/app/build.gradle).

Add these lines before android{} :

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def segmentWriteKey = properties.getProperty('segment.writeKey') // Notice we get the property we the exact same name we wrote in the local.properties file

This should look like this:

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream()) 
def segmentWriteKey = properties.getProperty('segment.writeKey')

android {
    compileSdkVersion 28
    ...
}

Explanation: Here we read the content from the local.properties file and then we extract the desired property (here the one named "segment.writeKey")

Under the android section, in the defaultConfig, add this line to the end:

manifestPlaceholders = [segmentWriteKey: segmentWriteKey]

Now everything should look like this:

Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def segmentWriteKey = properties.getProperty('segment.writeKey')

android {
    ...
    defaultConfig {
        ...
        manifestPlaceholders = [segmentWriteKey: segmentWriteKey]
    }
    ...
}

Note that the local.properties file is ignored by the source control by default so you will not have to do it yourself (but prevention is better than cure ;) ).

Well, we are done.

Thanks for reading.

Did you find this article valuable?

Support Steve Nosse by becoming a sponsor. Any amount is appreciated!