1

I want to use some debug code in my app but I don't want this code to be in the release build. I'm using the following check for this:

    if(__DEV__) {
        this.setState({username: 'Niel', password: 'Test'});
    }

If I generate my release build. Will the check still be in the build or will the dead code be removed? If not, is there a way to accomplish this?

Niel
  • 131
  • 1
  • 11

2 Answers2

2

DEV is used to detect if your code is running in development or release. So if you want a part of your code to be not executed in release, you may use such way:

if(_DEV_) {
//code is executed only in development
}else{
//code is executed only in release mode
}

to ensure if everything works as you want, you may run your app in release mode by react-native run-android --variant=release

good luck!

Ali Bakhtiar
  • 178
  • 11
  • 1
    Completely missed my point. I already understand what this code does, I'm asking if the code will still be packaged in the release build. I don't want leftover developer credentials in my release build. – Niel Mar 26 '19 at 08:30
  • Sorry if I didn't get you correctly! If I got correctly now, you want to know if your code can be de-compiled from apk and can be seen by others.. I think it can be possible. look at this post : https://stackoverflow.com/questions/3593420/is-there-a-way-to-get-the-source-code-from-an-apk-file – Ali Bakhtiar Mar 26 '19 at 10:17
  • No problem! I'm wondering if React Native is clever enough to leave the credentials out of the APK if I use the __DEV__ boolean. – Niel Mar 27 '19 at 10:24
  • @Niel Nope you shouldn't use those in your js if they are sensitive like that, you should get it from your server somehow, it will not be in your apk like you may think by the way, a js bundled file will contain your react native code. – Steve Moretz May 08 '21 at 21:35
-3

Of course, the code will NOT be in the production released bundle.

You can just check the production bundle, and these dead codes are eliminated.

Jess
  • 620
  • 1
  • 7
  • 18
  • 2020 update: This is absolutely incorrect. There's nothing in Metro or Babel removing "dead code" from inside __DEV__ conditions. You can easily confirm this by building your app for production and inspecting the generated `main.jsbundle` file. Your code will still be available in the jsbundle, but it wont execute as __DEV__ is falsey on production builds. – VulfCompressor Jul 08 '20 at 01:43