0

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
acharlop
  • 309
  • 4
  • 12

3 Answers3

3

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

Nick Alexander
  • 361
  • 2
  • 5
  • 21
  • Could not get unknown property 'env' for project ':app' of type org.gradle.api.Project. Any idea what am missing ? – Nithyanandan Sathiyanathan Dec 30 '20 at 12:13
  • 2
    Most of the time it means my build.gradle is wrong. Add/change this "apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"" at the top (below: import com.android.build.OutputFile) – Nick Alexander Dec 31 '20 at 13:39
  • Thanks it worked. How to access the env config in gradle.properties file ? – Nithyanandan Sathiyanathan Jan 01 '21 at 11:22
  • You can't. You would need to post-process the properties somehow as indicated in [this](https://stackoverflow.com/a/2263944/2382650). Currently, Gradle does not offer any such functionality to "custom load" gradle.properties – Nick Alexander Jan 04 '21 at 12:45
1

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

Long Nguyen
  • 9,898
  • 5
  • 53
  • 52
0

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
    }
}
Pedro
  • 2,813
  • 2
  • 22
  • 16