flutter_config: Different environments when build apk in release mode is null
I have set that:
project.ext.envConfigFiles = [
debug: ".env.development",
release: ".env.production",
]
But when run flutter build apk, FlutterConfig.get('DUMMY') always return null.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 8
- Comments: 15 (5 by maintainers)
I think I found the solution. I had the problem when i get
nullon anyFlutterConfig.get('DUMMY')with a release build usingflutter build apk --releaseorflutter build appbundle --release(debug mode works OK and returns config values correctly).After digging into
flutter_configcode, I found a line inFlutterConfigPlugin.kt:In other words - reflection is used to retrieve
BuildConfigclass for your application which has all your env variables. However, when we use--releaseflag with flutter command - an R8 obfuscator kicks in and changes class nameBuildConfigto something else - thus this line of code results inClassNotFoundExceptionand provides no env vars.SOLUTION
To prevent obfuscation of
BuildConfig, add R8 obfuscator rule to keep class intact:android/app/proguard-rules.proto your app’s project.where replace
com.yourcompany.appwith your app’s package name.This solved the problem for me - release build now returns env vars as expected. I did not use any flavors. I think we can add this information to documentation
@stpch Yes it should work without flavors. I haven’t had much time to debug this issue. I’ll look into the in the coming days.
It would be great if someone could help out 😃
Shouldn’t it work without flavors? https://github.com/luggit/react-native-config (which this project seems inspired by) works like this, where flavors are completely optional.
I’d imagine most people start out without flavors and add them later in development if needed.
thank you @martynasadomaitis - can we get this added to docs - from what I can see it still isn’t listed
@martynasadomaitis you saved my day! and probably my startup! Thanks!
Found the solution, add
-keep class com.YourPackageName.BuildConfig { *; }to android/app/proguard-rules.proI’m using flavors and this is the error I’m getting:
EDIT 1
This is my gradle.build:
EDIT 2 (Solution)
I found the issue.
I didn’t know we have to declare different env config names in gradle.
In short, I have 2 flavors (production and development) and I need the following:
From this:
To this:
@byneapp, please add this information to the Documentation.