react-native-config 无法在 build.gradle 中使用环境变量

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

我已 react-native-config 设置并在 javascript 中使用它没问题,但我无法在 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")
     }
}

版本

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

输出

$ ./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

MYAPP_RELEASE_STORE_FILE中的文件似乎不存在。你在里面有什么价值?该文件是否存在于同一文件夹中?

但也请注意,您不应在 .env 中存储签名机密;来自 react-native-config 自述文件:

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.

你可以做的是使用 Gradle 属性 文件来保守你的秘密。例如,在 ~/.gradle/gradle.properties:

MYAPP_RELEASE_KEY_ALIAS=myapp
MYAPP_RELEASE_STORE_PASSWORD=abc
MYAPP_RELEASE_KEY_PASSWORD=def

这些仅在 build.gradle 中可用:

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

在 gradle 构建中使用 project.env.get("VAR NAME")

尽管所有自动链接设置都已为您完成,但仍需要手动设置才能在 build.gradle

中使用它

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

到最顶部,android/app/build.gradle 文件 import com.android.build.OutputFile 的正下方

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