react-native-config: Android. Got compiled but Config is undefined
I got it installed and checked everything. My MainApplication.java is like this.
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new ReactNativeConfigPackage(),
...
The app is compiled, but Config is undefined import Config from ‘react-native-config’
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Reactions: 9
- Comments: 21 (6 by maintainers)
So, after much struggle, I’ve managed to make this work. Here’s a summary of what I did:
Link the library manually
Because react native link was failing (see https://github.com/facebook/react-native/issues/12216 and https://github.com/luggit/react-native-config/issues/80). Here the steps:
settings.gradle
include ':react-native-config' project(':react-native-config').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-config/android')
build.grade
apply from: project(':react-native-config').projectDir.getPath() + "/dotenv.gradle"
not sure it matters but I moved this at the top of the file right after apply plugin and under thedebug {
config addresValue "string", "build_config_package", "com.myappname"
MainApplication.java
import com.lugg.ReactNativeConfig.ReactNativeConfigPackage;
and addnew ReactNativeConfigPackage()
Restart all
Meaning restarted the emulator, react native start and react native run-android
Please fix the link or add info for a manual linking for android in the readme.
After re-visiting this problem, I discovered that the react-native link command was not adding this line to android/app/build.gradle:
apply from: project(‘:react-native-config’).projectDir.getPath() + “/dotenv.gradle”
Once I added that everything went from undefined to the values in my .env file.
Whenever this exception happens, it’s usually because of one of the following reasons:
resValue
forbuild_config_package
as explained here https://github.com/luggit/react-native-config#advanced-setup In your pasted code, I haven’t seen it.import { Config } from 'react-native-config
instead DOimport Config from 'react-native-config'
.env
file needs to be in the root of your project directory, usually where yourindex.js
file is located.You can see if
react-native-config
can access BuildConfig class with logcat:I’ve just checked it and this package works with RN 0.34.0 without problems.
In my opinion @luggit I would change
Log.d("ReactNative"
toLog.d("ReactNativeJS
which can be easily seen withreact-native log-android
and would add a URL in the log pointing to how to set upbuild_config_package
.Cheers, Miguel
It doesn’t work without
for me.
Please update your README to mention this.
Bah. I had meant to comment on https://github.com/infinitered/ignite/issues/368 instead of this issue. I’ll leave it there and this one as a shameful reminder to myself to never reply on my iPad again. 🔔 🔔 🔔
settings.gradle
build.gradle
MainApplication.java