peteclark3
peteclark3

Reputation: 558

React Native Android - how to run in debug mode solely on device?

I'm trying to run my react native android app in debug mode on our devices, without the need for a dev server running on a dev box. I'm using the debug variant/scheme on both platforms to facilitate the "staging" version of our app, since I've read that react native is not super friendly with build modes other than release and debug.

On the iOS side, I was able to modify the AppDelegate.m as follows... basically, if it's a debug build and it was built by buddybuild, don't look for a dev machine for the js bundle, look to the device:

#ifdef DEBUG
NSDictionary *infoPlistDict = [[NSBundle mainBundle] infoDictionary];
if (infoPlistDict[@"BUDDYBUILD_BUILD_ID"]) {
  // deployed via buddybuild, don't use localhost
  jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
} else {
  jsCodeLocation = [NSURL URLWithString:@"http://localhost:8081/index.ios.bundle?platform=ios&dev=true"];
}
#else
  jsCodeLocation = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif

... I'm looking for a way to do this on android. I have the buddybuild bit of code I need, so no worries there, I just want to know how to programmatically change the location of the js bundle on android as I do above for iOS in AppDelegate.m. Right now, all code related to the location of the js bundle on android seems to be in the react-native package itself and I'd rather not hack that to get it to work.

If I can provide any more information, please let me know.

Upvotes: 5

Views: 9357

Answers (2)

Norman
Norman

Reputation: 561

I would like to summarize the solution to bundle everything within a debug build. Especially the hint in point two did the trick for me. Thanks @Tspoon.

1. Android Studio

  • go to Preferences -> Build, Execution, Deployment -> Compiler
  • uncheck "Configure on Demand"

2. build.gradle

  • within your app/build.gradle file do the following:

    project.ext.react = [
       // whether to bundle JS and assets in debug mode
       bundleInDebug: true,
    
       // whether to bundle JS and assets in release mode
       bundleInRelease: true,
    ]
    
    apply from: "../../node_modules/react-native/react.gradle"
    
  • NOTE that it is important to define the react settings above the apply from ... statement, otherwise it will not work

3. Create APK

  • create your dev apk as usual with ./gradlew assembleDebug within your android folder

Upvotes: 4

Tspoon
Tspoon

Reputation: 3800

On Android, you'll want to modify android/app/build.gradle.

Before the apply from: "../../node_modules/react-native/react.gradle" line, you'll want to tell the packager to bundle the js assets and include them when deploying your APK.

For example:

project.ext.react = [
    bundleInBuddybuild: true,
]

Where the syntax is bundleIn${productFlavor}${buildType}

Or if you just want to do it for for debug builds:

project.ext.react = [
    bundleInDebug: true,
]

Upvotes: 2

Related Questions