Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release build unable to access .env file on android #770

Open
eakenbor opened this issue Aug 4, 2023 · 16 comments
Open

Release build unable to access .env file on android #770

eakenbor opened this issue Aug 4, 2023 · 16 comments

Comments

@eakenbor
Copy link

eakenbor commented Aug 4, 2023

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?

@xlcrr
Copy link

xlcrr commented Aug 6, 2023

Is it working for you in dev mode?
I am unable to get it working with new RN project

@eakenbor
Copy link
Author

eakenbor commented Aug 6, 2023

xlcrr I had to upgrade my RN to 0.72.3 the latest version.

@johnfrades
Copy link

It's working for you now @eakenbor ?

@xlcrr
Copy link

xlcrr commented Aug 9, 2023

xlcrr I had to upgrade my RN to 0.72.3 the latest version.

Thanks for the reply. I am using 0.72.3 and its not working out of the box on android, will post my solution when I find it

@rburgst
Copy link

rburgst commented Aug 10, 2023

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

@StefanWallin
Copy link

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!

@xlcrr
Copy link

xlcrr commented Aug 11, 2023

Thanks for the tip but updating proguard-rules.pro did not work for me

If anyone would like to help collaborate on an open source project with latest versions of everything I would really love some help here!

https://github.com/OpenLitterMap/react-native

Happy to post the solution when I get it

@xlcrr
Copy link

xlcrr commented Aug 21, 2023

Latest minor react-native release fixed it for me, 0.72.3 -> 0.72.4

@gravitypersists
Copy link

No idea why this fixed it for me but I was running through an npm script that was also doing ./gradlew clean which stopped this from working properly. Removing that fixed the issue for me.

@Sahil6458
Copy link

#541 (comment)

@natemartins
Copy link

#541 (comment)

This did not work for me

@natemartins
Copy link

Latest minor react-native release fixed it for me, 0.72.3 -> 0.72.4

I have upgraded to 0.72.4 and 0.72.5 but it still isn't working for me.

@FranciscoPDNeto
Copy link

Hello, I was trying to update from react-native 0.71.11 to 0.72.6 and I started to face the same issue, any news on that?

@FranciscoPDNeto
Copy link

After including the ENVFILE env var right before running react-native it worked, it seems react is trying to make some validation with build gradle before ./gradlew install, and then the env file was not injected

@fikriwebdev
Copy link

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

Thankyou! Its working after add this line pro-guard

@Roasted-Peanut
Copy link

#777 (comment)

I have problem same. Someone help me!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests