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

[Question][Android] switch bundle depends on build environment #2039

Closed
rnike opened this issue Feb 20, 2021 · 5 comments
Closed

[Question][Android] switch bundle depends on build environment #2039

rnike opened this issue Feb 20, 2021 · 5 comments
Labels

Comments

@rnike
Copy link

rnike commented Feb 20, 2021

Hi, we are starting to use react-native-code-push in our project(Android/iOS), I'm confused about the difference between changing the bundle URL on the Android and iOS platform.

In the iOS part, the document recommend using the DEBUG pre-processor macro to dynamically switch between using the packager server and CodePush

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
  #if DEBUG
    return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  #else
    return [CodePush bundleURL];
  #endif
}

But in the android part, this is not mentioned, and the implementation is as below

...
// 1. Import the plugin class.
import com.microsoft.codepush.react.CodePush;

public class MainApplication extends Application implements ReactApplication {

    private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
        ...

        // 2. Override the getJSBundleFile method in order to let
        // the CodePush runtime determine where to get the JS
        // bundle location from on each app start
        @Override
        protected String getJSBundleFile() {
            return CodePush.getJSBundleFile();
        }
    };
}

If we want Android to behave as same as iOS "only get bundle from codepush if isn't debug", should we need to implement it by ourselves like something below?

        @Override
        protected String getJSBundleFile() {
            if(BuildConfig.DEBUG == Boolean.TRUE){
              // react-native will get bundle from 'index.android.bundle' if getJSBundleFile returns null
              return null;
            }else{
              return CodePush.getJSBundleFile();
            }
        }

Or this is handled by CodePush.getJSBundleFile already?

Environment

  • react-native-code-push version: 7.0.0
  • react-native version:0.63.3
@rnike rnike changed the title [Android] switch bundle depends on build environment [Question][Android] switch bundle depends on build environment Feb 20, 2021
@rnike
Copy link
Author

rnike commented Feb 22, 2021

Since there is no response, I've run into the source code and found out that we surely need to implement the "if statement" to determine whether it should get the bundle file from CodePush or from the default path on Android.

However, I've implemented it differently from the document, here's what I did:

iOS

- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
  #define hasCodePushKey ([[ReactNativeConfig envFor:@"CODE_PUSH_KEY"] length]!=0)

  if(hasCodePushKey){
    return [CodePush bundleURL];
  }else{
    return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
  }
}

Android

    @Override
    protected String getJSBundleFile() {
      boolean hasCodePushKey = !BuildConfig.CODE_PUSH_KEY.isEmpty();

      if(hasCodePushKey){
        return CodePush.getJSBundleFile();
      }else{
        // react-native will get bundle from 'index.android.bundle' if getJSBundleFile returns null
        return null;
      }
    }

Enable it by the environment variable is more convenient for us to integrate with CI/CD, hope this helps anyone who is also confusing about it.

@rnike rnike closed this as completed Feb 23, 2021
@rdsedmundo
Copy link

rdsedmundo commented Jun 16, 2021

@rnike I think you should keep this issue open so an official response can be made and maybe the docs updated. Also faced the same problem today.

@rnike
Copy link
Author

rnike commented Jun 17, 2021

@rdsedmundo Sure, I'll keep it open

@ghost
Copy link

ghost commented Dec 13, 2021

This issue has been automatically marked as stale because it has not had any activity for 60 days. It will be closed if no further activity occurs within 15 days of this comment.

@ghost
Copy link

ghost commented Dec 28, 2021

This issue will now be closed because it hasn't had any activity for 15 days after stale. Please feel free to open a new issue if you still have a question/issue or suggestion.

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

No branches or pull requests

2 participants