react-native-community / discussions-and-proposals

Discussions and proposals related to the main React Native project
https://reactnative.dev
1.68k stars 126 forks source link

window.__DEV__ is false on debug build on RN version 0.75.4 #823

Open sahad00 opened 1 week ago

sahad00 commented 1 week ago

Introduction

Earlier I used to detect the env based like this

 let isProduction = window.__DEV__ ? false : true;

problem is that isProduction is true on debug build I have generated

build command used

 "build:android": "react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res",

Details

app/build.gradle

apply plugin: "com.android.application"
apply plugin: "org.jetbrains.kotlin.android"
apply plugin: "com.facebook.react"

//Firebase plugins
apply plugin: "com.google.gms.google-services"
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.google.firebase.firebase-perf'

//React Native Vector Icons
apply from: file("../../node_modules/react-native-vector-icons/fonts.gradle")

//Sentry AutoGenerated
apply from: new File(["node", "--print", "require.resolve('@sentry/react-native/package.json')"].execute().text.trim(), "../sentry.gradle")

import groovy.json.JsonSlurper

react {

    /* Autolinking */
    autolinkLibrariesWithApp()
}

/**
 * Set this to true to Run Proguard on Release builds to minify the Java bytecode.
 */
def enableProguardInReleaseBuilds = true

def enableSeparateBuildPerCPUArchitecture = true

if(System.env.BUILD_TYPE=='aab') {
  enableSeparateBuildPerCPUArchitecture = false
}

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

def getVersionFromPackage() {
    //  Read and parse package.json file from project root
    def inputFile = new File("$rootDir/../package.json")
    def packageJson = new JsonSlurper().parseText(inputFile.text)

    // Return the version, you can get any value this way
    return packageJson["version"]
}

def getVersionCodeFromPackage() {
    def inputFile = new File("$rootDir/../package.json")
    def packageJson = new JsonSlurper().parseText(inputFile.text)
    return packageJson["androidVersionCode"]
}

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

    namespace "com.swific.registerbook"
    defaultConfig {
        applicationId "com.swific.registerbook"
        // applicationId "com.swific.registerbook.internal"
        // applicationId "booleanlabs.reactjs.natv.logbook.app"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        missingDimensionStrategy 'react-native-camera','general'
        versionCode getVersionCodeFromPackage()
        versionName getVersionFromPackage()
        multiDexEnabled true
        buildConfigField "String", "SENTRY_RELEASE", "\"${System.env.SENTRY_RELEASE}\""
        buildConfigField "String", "SENTRY_DIST", "\"${System.env.SENTRY_DIST}\""
        //FOR NOTIFEE ERROR
        configurations.all {
            resolutionStrategy { force 'androidx.work:work-runtime:2.7.0' }
            resolutionStrategy { force 'androidx.sqlite:sqlite-framework:2.1.0' }
        }
    }

    splits {
        abi {
            enable enableSeparateBuildPerCPUArchitecture
            reset()
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }

    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
        release {
            if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
                storeFile file(MYAPP_UPLOAD_STORE_FILE)
                storePassword MYAPP_UPLOAD_STORE_PASSWORD
                keyAlias MYAPP_UPLOAD_KEY_ALIAS
                keyPassword MYAPP_UPLOAD_KEY_PASSWORD
            }
        }
    }
    buildFeatures {
        viewBinding true
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
            minifyEnabled false 
            shrinkResources false 
            applicationIdSuffix ".debug"
            debuggable true
            resValue("string", "google_apiKey", "${System.env.MAPS_API_KEY}")
            manifestPlaceholders = [
                    appIcon: "@mipmap/ic_launcher_debug",
                    appIconRound: "@mipmap/ic_launcher_debug_round"
            ]
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://facebook.github.io/react-native/docs/signed-apk-android.
            minifyEnabled enableProguardInReleaseBuilds
            shrinkResources true
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
            resValue 'string', 'facebook_app_id', '"411177813230527"'
            resValue("string", "CodePushDeploymentKey", "${System.env.CODE_PUSH_DEPLOYMENT_KEY}")
            resValue("string", "fcm_sender_id", "${System.env.FCM_SENDER_ID}")
            resValue("string", "branch_key", "${System.env.BRANCH_KEY}")
            resValue("string", "branch_app_link", "${System.env.BRANCH_APP_LINK}")
            resValue("string", "branch_app_link_alternate", "${System.env.BRANCH_APP_LINK_ALTERNATE}")
            resValue("string", "branch_test_mode", "${System.env.BRANCH_TEST_MODE}")
            resValue("string", "google_apiKey", "${System.env.MAPS_API_KEY}")
            manifestPlaceholders = [
                    appIcon: "@mipmap/ic_launcher",
                    appIconRound: "@mipmap/ic_launcher_round"
            ]
        }
        releaseStaging {
            initWith release
            applicationIdSuffix ".staging"
            // Note: It is a good idea to provide matchingFallbacks for the new buildType you create to prevent build issues
            // Add the following line if not already there
            matchingFallbacks = ['release']
        }
    }
}

dependencies {
    // The version of react-native is set by the React Native Gradle Plugin
    implementation("com.facebook.react:react-android")

    if (hermesEnabled.toBoolean()) {
        implementation("com.facebook.react:hermes-android")
    } else {
        implementation jscFlavor
    }

    //used in API 34 Android upgrade
    implementation 'org.jetbrains:annotations:16.0.2'  

    implementation 'com.google.firebase:firebase-auth'
    implementation 'com.google.firebase:firebase-firestore'
    implementation "com.google.firebase:firebase-analytics"
    implementation 'com.google.firebase:firebase-messaging'
    implementation 'com.google.firebase:firebase-storage'
    implementation 'com.google.firebase:firebase-functions'

    //MultiDex
    implementation 'com.android.support:multidex:2.0.1'

    //Carrier Info
    implementation project(':react-native-carrier-info')

    //Fresco Animations
    implementation 'com.facebook.fresco:fresco:2.0.0'
    implementation 'com.facebook.fresco:animated-gif:2.0.0'

    implementation 'com.google.android.gms:play-services-ads-identifier'

    // implementation 'com.google.android.exoplayer:exoplayer' //Optional for Audio/Video
    // implementation 'com.google.android.exoplayer:exoplayer-hls' //Optional for Audio/Video
    // implementation 'com.google.android.exoplayer:exoplayer-ui' //Optional for Audio/Video
    implementation 'com.github.bumptech.glide:glide:4.11.0' //Mandatory for App Inbox
    implementation 'androidx.recyclerview:recyclerview:1.1.0' //Mandatory for App Inbox,
    implementation 'androidx.viewpager:viewpager:1.0.0' //Mandatory for App Inbox
    implementation 'com.google.android.material:material:1.2.1' //Mandatory for App Inbox
    implementation 'androidx.appcompat:appcompat:1.2.0' //Mandatory for App Inbox
    implementation 'androidx.core:core:1.3.0'
    implementation 'androidx.fragment:fragment:1.1.0' // InApp
    //Mandatory for React Native SDK v0.3.9 and above add the following -
    implementation 'com.android.installreferrer:installreferrer:2.2'
    implementation 'com.google.android.gms:play-services-appset:16.0.0'
    implementation 'com.airbnb.android:lottie:3.7.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'

    //Shorcut-Widget-Dependencies-Below
    // implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
    // implementation 'com.google.code.gson:gson:2.8.8'
    implementation 'com.google.android.flexbox:flexbox:3.0.0'
    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.google.android.gms:play-services-location:18.0.0'
    implementation 'com.squareup.retrofit2:retrofit:2.7.2'
    implementation 'com.squareup.retrofit2:converter-gson:2.7.2'
    // implementation 'com.squareup.okhttp3:okhttp:3.6.0'
    //Shorcut-Widget-Dependencies-Above

    // addUnimodulesDependencies()

    implementation project(':react-native-fs')
    implementation(project(':react-native-maps')){
        exclude group: 'com.google.android.gms', module: 'play-services-base'
        exclude group: 'com.google.android.gms', module: 'play-services-maps'
    }  
    implementation 'com.google.android.gms:play-services-base:10.0.1'
    implementation 'com.google.android.gms:play-services-maps:10.0.1'
    implementation ("androidx.appcompat:appcompat:1.3.1") {
        version {
            strictly '1.3.1'
        }
    }
}

I am unable to figure out the issue please help

gradle.properties

# Project-wide Gradle settings.

# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.

# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html

# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
# Default value: -Xmx512m -XX:MaxMetaspaceSize=256m
org.gradle.jvmargs=-Xmx4g
#  -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8

# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true

# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Use this property to specify which architecture you want to build.
# You can also override it from the CLI using
# ./gradlew <task> -PreactNativeArchitectures=x86_64
reactNativeArchitectures=armeabi-v7a,arm64-v8a,x86,x86_64

# Use this property to enable support to the new architecture.
# This will allow you to use TurboModules and the Fabric render in
# your application. You should enable this flag either if you want
# to write custom TurboModules/Fabric components OR use libraries that
# are providing them.
newArchEnabled=false

# Use this property to enable or disable the Hermes JS engine.
# If set to false, you will be using JSC instead.
hermesEnabled=true

MYAPP_UPLOAD_STORE_FILE=india-excel-key.keystore
MYAPP_UPLOAD_KEY_ALIAS=india-excel
MYAPP_UPLOAD_STORE_PASSWORD=Indi@excel!sep()
MYAPP_UPLOAD_KEY_PASSWORD=Indi@excel!sep()
cortinico commented 1 week ago

I'm unsure about why you're using window..

You should be fine with just:

if (__DEV__) {
    console.log('I am in debug');
}
sahad00 commented 1 week ago

@cortinico I tried using the above code but still it gives me false on debug build leading to PRODUCTION = true on debug build based on the below code

let isProduction =__DEV__ ? false : true;
sahad00 commented 1 week ago

@cortinico I want to keep this DEV true disabling all developer option for debug build

cortinico commented 1 week ago

You'll have to provide a reproducer using https://github.com/react-native-community/reproducer-react-native as we can't help you further otherwise

hoxyq commented 1 week ago

@sahad00 You mentioned that you are using this command:

"build:android": "react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res",

It specifies --dev false, this is why the value of isProduction is true: you are building an app in production mode.

cortinico commented 1 week ago

It specifies --dev false, this is why the value of isProduction is true: you are building an app in production mode.

True. @sahad00 why are you using a custom bundle command and not the default toolchain?

sahad00 commented 1 week ago

@cortinico

  if I manually set this to true then things are good,
@Override
        public boolean getUseDeveloperSupport() {
            return true;
        }
sahad00 commented 1 week ago

@cortinico its something like release build on debug configs , we usually have it to be tested

cortinico commented 1 week ago

getUseDeveloperSupport should be configured as such:

https://github.com/react-native-community/template/blob/37144414df684e676b467726915dd225786be414/template/android/app/src/main/java/com/helloworld/MainApplication.kt#L27

sahad00 commented 1 week ago

@cortinico

@Override
        public boolean getUseDeveloperSupport() {
            return BuildConfig.DEBUG;
        }

this is not working properly when I install on device but setting it manually to true works

sahad00 commented 1 week ago
10-10 17:55:26.503 12239 12239 D MainApplication: BuildConfig.DEBUG: true
10-10 17:55:26.510 12239 12239 W Looper  : PerfMonitor longMsg : seq=3 plan=17:55:24.700 late=0ms wall=1806ms h=android.app.ActivityThread$H w=110 procState=-1
10-10 17:55:26.516 12239 12323 I PowerHalWrapper: PowerHalWrapper.getInstance 
10-10 17:55:26.516 12239 12239 I MSYNC3-VariableRefreshRate: Variable refreshrate is disabled
10-10 17:55:26.523 12239 12327 D libMEOW : meow new tls: 0xe6a37d00
10-10 17:55:26.524 12239 12327 D libMEOW : meow reload base cfg path: na
10-10 17:55:26.524 12239 12327 D libMEOW : meow reload overlay cfg path: na
10-10 17:55:26.526 12239 12327 E QT      : [QT]file does not exist
10-10 17:55:26.527 12239 12327 D libMEOW : applied 1 plugins for [com.swific.registerbook.debug]:
10-10 17:55:26.527 12239 12327 D libMEOW :   plugin 1: [libMEOW_gift.so]: 0xf27bb800
10-10 17:55:26.527 12239 12327 D libMEOW : rebuild call chain: 0xe6a09c40
10-10 17:55:26.528 12239 12327 D libMEOW : meow delete tls: 0xe6a37d00

I tried logging with add logcat but the value is cool I am not sure why its buggy

cortinico commented 1 week ago

I tried logging with add logcat but the value is cool I am not sure why its buggy

Without a reproducer we can't help further

sahad00 commented 3 days ago

@cortinico will make one