react-native-config unable to use env variables in build.gradle

5.3k Views Asked by At

I have react-native-config set up and use it in javascript no problem but am unable to use it like this in build.gradle.

release {
     if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) {
         storeFile file(project.env.get("MYAPP_RELEASE_STORE_FILE"))
         storePassword project.env.get("MYAPP_RELEASE_STORE_PASSWORD")
         keyAlias project.env.get("MYAPP_RELEASE_KEY_ALIAS")
         keyPassword project.env.get("MYAPP_RELEASE_KEY_PASSWORD")
     }
}

versions

react-native-cli: 2.0.1
react-native: 0.49.5
react: 16.0.0
react-native-config: 0.11.1

Output

$ ./gradlew assembleRelease`
... shortened output
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:validateSigningRelease'.
> Keystore file not set for signing config release
3

There are 3 best solutions below

4
On BEST ANSWER

In gradle build you use project.env.get("VAR NAME")

0
On

It seems like the file in MYAPP_RELEASE_STORE_FILE doesn't exist. What value do you have in there? Does the file exist in the same folder?

But also notice you shouldn't store signing secrets in .env; From the react-native-config readme:

Keep in mind this module doesn't obfuscate or encrypt secrets for packaging, so do not store sensitive keys in .env. It's basically impossible to prevent users from reverse engineering mobile app secrets, so design your app (and APIs) with that in mind.

What you can do instead is use a Gradle property file to keep your secrets. For instance, in ~/.gradle/gradle.properties:

MYAPP_RELEASE_KEY_ALIAS=myapp
MYAPP_RELEASE_STORE_PASSWORD=abc
MYAPP_RELEASE_KEY_PASSWORD=def

These are then just available in build.gradle:

signingConfigs {
    release {
        storeFile "myapp.keystore"
        keyAlias MYAPP_RELEASE_KEY_ALIAS
        storePassword MYAPP_RELEASE_STORE_PASSWORD
        keyPassword MYAPP_RELEASE_KEY_PASSWORD
    }
}
0
On

Although autolinking setup all for you, still need to set up this manually to use it in build.gradle

apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"

to the very top, right below import com.android.build.OutputFile in android/app/build.gradle file

https://github.com/luggit/react-native-config#extra-step-for-android