react-native-config: Release build unable to access .env file on android

Everything works fine on debug but on release build, no variable in my .env files is accessible. They all appear as undefined.

"react-native": "0.71.7", "react-native-config": "^1.5.0",

I build using the following: react-native run-android --mode=release or react-native run-android --variant=release

Here are my codes: // …/app/build

// Load gradle.properties
def keystorePropertiesFile = rootProject.file('.gradle/gradle.properties')
def keystoreProperties = new Properties()
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))

project.ext.envConfigFiles = [
        debug: '.env',
        release: '.env',
        stagingrelease: '.env.staging',
        productionrelease: '.env.production'
]

apply plugin: "com.android.application"
apply plugin: "com.facebook.react"

import com.android.build.OutputFile

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

apply from: '../../node_modules/react-native-vector-icons/fonts.gradle' // react-native-vector-icons
/**
 * This is the configuration block to customize your React Native Android app.
 * By default you don't need to apply any configuration, just uncomment the lines you need.
 */
react {}

def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = true

def jscFlavor = 'org.webkit:android-jsc:+'

def reactNativeArchitectures() {
    def value = project.getProperties().get("reactNativeArchitectures")
    return value ? value.split(",") : ["armeabi-v7a", "x86", "x86_64", "arm64-v8a"]
}

android {
    ndkVersion rootProject.ext.ndkVersion

    compileSdkVersion rootProject.ext.compileSdkVersion

    namespace "com.myPageName"
    defaultConfig {
        applicationId "com.myPageName"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 5
        versionName "1.0"
    }

    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include (*reactNativeArchitectures())
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
        release {
                storeFile file(keystoreProperties['MYAPP_UPLOAD_STORE_FILE'])
                storePassword keystoreProperties['MYAPP_UPLOAD_STORE_PASSWORD']
                keyAlias keystoreProperties['MYAPP_UPLOAD_KEY_ALIAS']
                keyPassword keystoreProperties['MYAPP_UPLOAD_KEY_PASSWORD']
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }

        stagingrelease {
            initWith release
            matchingFallbacks = ['debug', 'release']
        }

        productionrelease {
            initWith release
            matchingFallbacks = ['release', 'debug']
        }
    }

    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        defaultConfig.versionCode * 1000 + versionCodes.get(abi)
            }

        }
    }
}

dependencies {
    implementation("com.facebook.react:react-android")

    implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.0.0")

    implementation 'com.google.firebase:firebase-messaging:21.1.0'

    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}")
    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.squareup.okhttp3', module:'okhttp'
    }

    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}")
    if (hermesEnabled.toBoolean()) {
        implementation("com.facebook.react:hermes-android")
    } else {
        implementation jscFlavor
    }
}

apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

apply plugin: 'com.google.gms.google-services'

// proguard-rules.pro

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /usr/local/Cellar/android-sdk/24.3.3/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

-keep class com.myPackageName.BuildConfig { *; }

My AndroidManifest.xml does not have a package name

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
...

Please can someone help me with this?

About this issue

  • Original URL
  • State: open
  • Created a year ago
  • Reactions: 3
  • Comments: 15

Most upvoted comments

ensure that you add your BuildConfig to your proguard rules. Took me hours to figure out. IMHO the README needs a major makeover. This should be pretty much on top it.

In your proguard-rules.pro add

-keep class my.app.package.BuildConfig { *; }

Obviously you need to adapt the package to your actual app package name

ensure that you add your BuildConfig to your proguard rules. Took me hours to figure out. IMHO the README needs a major makeover. This should be pretty much on top it.

In your proguard-rules.pro add

-keep class my.app.package.BuildConfig { *; }

I had done the above, but it still didn’t work. Thank you @rburgst for writing also this, which I had missed!:

Obviously you need to adapt the package to your actual app package name

With that it works properly. As stated in another issue, the readme needs an overhaul!