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

React native android debug build tries to load env variables in release section of build.gradle #48121

Closed
uchar opened this issue Dec 5, 2024 · 1 comment
Labels
Needs: Triage 🔍 Resolution: Answered When the issue is resolved with a simple answer

Comments

@uchar
Copy link

uchar commented Dec 5, 2024

Description

This gradle config(android/app/build.gradle)

android {
    ndkVersion rootProject.ext.ndkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion
    compileSdk rootProject.ext.compileSdkVersion

    namespace "com.xxx.yyy"
    defaultConfig {
        applicationId "com.xxx.yyy"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
         release {
            storeFile file(System.getenv("MYAPP_UPLOAD_STORE_FILE") ?: MYAPP_UPLOAD_STORE_FILE)
            storePassword System.getenv("MYAPP_UPLOAD_STORE_PASSWORD") ?: MYAPP_UPLOAD_STORE_PASSWORD
            keyAlias System.getenv("MYAPP_UPLOAD_KEY_ALIAS") ?: MYAPP_UPLOAD_KEY_ALIAS
            keyPassword System.getenv("MYAPP_UPLOAD_KEY_PASSWORD") ?: MYAPP_UPLOAD_KEY_PASSWORD
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            signingConfig signingConfigs.release
            minifyEnabled false
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }
}

with react-native run-android generate this error

error Failed to install the app. Command failed with exit code 1: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081 FAILURE: Build failed with an exception. * Where: Build file 'E:\\MobileAppV2\android\app\build.gradle' line: 104 * What went wrong: A problem occurred evaluating project ':app'. > Could not get unknown property 'MYAPP_UPLOAD_STORE_FILE' for SigningConfig$
AgpDecorated_Decorated{name=release, storeFile=null, storePassword=null, keyAlias=null, keyPassword=null, storeType=pkcs12, v1SigningEnabled=true, v2SigningEnabled=true, enableV1Signing=null, enableV2S
igning=null, enableV3Signing=null, enableV4Signing=null} of type com.android.build.gradle.internal.dsl.SigningConfig$AgpDecorated. * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. > Get more help at https://help.gradle.org. BUILD FAILED in 1s.

But it should works fine because I'm using debug build and dont want to set my release env variables during debug builds ( it works fine on release builds if I set them )

Steps to reproduce

1_ Create empty project
2_ Add above to build.grade
3_ Use npm run android and you should see the error
(You can also simply look at reproducer output https://github.com/uchar/react-native-build-bug/actions/runs/12184080305/job/33987094131 )

React Native Version

0.76.3

Affected Platforms

Runtime - Android

Output of npx react-native info

System:
  OS: Windows 11 10.0.22631
  CPU: "(16) x64 AMD Ryzen 7 5800H with Radeon Graphics         "
  Memory: 15.79 GB / 31.86 GB
Binaries:
  Node:
    version: 22.11.0
    path: D:\Programs\nodejs\node.EXE
  Yarn:
    version: 1.22.18
    path: ~\AppData\Roaming\npm\yarn.CMD
  npm:
    version: 10.9.0
    path: D:\Programs\nodejs\npm.CMD
  Watchman: Not Found
SDKs:
  Android SDK: Not Found
  Windows SDK: Not Found
IDEs:
  Android Studio: Not Found
  Visual Studio: Not Found
Languages:
  Java: 17.0.13
  Ruby:
    version: 2.4.10
    path: D:\Programs\Ruby24-x64\bin\ruby.EXE
npmPackages:
  "@react-native-community/cli":
    installed: 15.0.1
    wanted: 15.0.1
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.76.3
    wanted: 0.76.3
  react-native-windows: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: true
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

Stacktrace or Logs

no stacktrace it's happening during build

Reproducer

https://github.com/uchar/react-native-build-bug

Screenshots and Videos

No response

@uchar uchar changed the title React native debug build tries to load env variables in release section of build.gradle React native android debug build tries to load env variables in release section of build.gradle Dec 5, 2024
@cortinico
Copy link
Contributor

This is an Android question and is not necessarily related to React Native.

The problem you're having is that the build.gradle file is all "executed" for both debug and release variants, even if you're attempting to build only one of the two.

I suggest you change your code to something like:

         release {
            if (System.getenv("MYAPP_UPLOAD_STORE_FILE") != null) {
	            storeFile file(System.getenv("MYAPP_UPLOAD_STORE_FILE"))
			}
			// ...
        }

@cortinico cortinico added the Resolution: Answered When the issue is resolved with a simple answer label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs: Triage 🔍 Resolution: Answered When the issue is resolved with a simple answer
Projects
None yet
Development

No branches or pull requests

2 participants