8

I'm new to React Native. The task at hand is to set my Google API key in AndroidManifest.xml without exposing it, when pushed to GitHub. I set up an environment variable called API_KEY, but however I want to access it in the .xml, I get an error when trying to spin up the dev server.

So far I tried:

android:value=${env.API_KEY}
android:value="${env.API_KEY}"
android:value="${API_KEY}"

Thank you!

4 Answers 4

15

Based on the second comment (from kenmistry), the solution that worked for me was indeed to create a placeholder in build.gradle, but since, for whatever reason, configuring and referring a .env file did't work, I invoked my environment variables like so in build.gradle:

manifestPlaceholders = [API_KEY: "$System.env.API_KEY"]

and accessed it in the .xml as suggested by kenmistry:

android:value="${API_KEY}"
Sign up to request clarification or add additional context in comments.

1 Comment

This works for me. I tried the accepted answer first, but $process.env.API_KEY threw an error Could not get unknown property 'process'.
10

assuming that you have defined the key in your .env file, you can set that up on build.gradle as manifestPlaceholders.

android {
    defaultConfig {
        manifestPlaceholders = [API_KEY: "$process.env.your_key"]
    }
    ...
}

on your AndroidManifest.xml,

android:value="${API_KEY}"

6 Comments

If I understand it right, now the key is in an other .gradle, still exposed in my repo. It should be nice to access environment variable instead.
hey @kenmistry, I am working with react and I am reading several env files via:project.ext.envConfigFiles = [ release: ".env.prod", debug: ".env.dev", staging: ".env.stage"] - can I also read it from there? Somehow it tells me the value is "null" when I try.
@Fapi , how did you call it? like this - $process.env.prod.your_key
I think I tried like this and also with $System
note that there are more than one build.gradle files, this change should be done in android/app/build.gradle
|
4

For anyone else that the above solutions aren't working for, I had to use this syntax in android/app/build.gradle under the android.defaultConfig section:

manifestPlaceholders = [MY_ENV_VAR:"${System.getenv("MY_ENV_VAR")}"]

and then in android/app/src/main/AndroidManifest.xml under the <manifest><application> section

<meta-data android:name="myEnvVar" android:value="${MY_ENV_VAR}" />

Comments

0

Another solution that works for me is:

Make sure that in the .env file is defined in the root of the project. Then:

Edit your android\app\build.gradle with this:

apply plugin: "com.android.application"
apply plugin: "org.jetbrains.kotlin.android"
apply plugin: "com.facebook.react"

import com.android.build.gradle.internal.dsl.BaseAppModuleExtension

// Import dotenv and load the environment vars
project.ext.env = new Properties()
file('../../.env').withInputStream { project.ext.env.load(it) }

react {
    // React Native settings
}

android {
    // other code

    defaultConfig {
        // Use the loaded environment variable
        manifestPlaceholders = [MY_ENV_VAR: project.ext.env['MY_ENV_VAR']]
    }
}

And in your android\app\src\main\AndroidManifest.xml add this:

<meta-data android:name="myEnvVar" android:value="${MY_ENV_VAR}" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.