Solution: See comments below.
Update: After resetting the emulator completely and building again I no longer get the error about being unable to locate the bundle hash. The logs just say _Loading JS bundle from "assets://index.android.bundle"_ and then the app crashes.
Update 2: This seems to function correctly when building a production build, but not a staging one. They should be effectively the same, so I am not sure what is different at this stage.
I have followed the installation instructions on the Readme and my MainActivity.java looks like this:
package com.mypackage;
import android.app.Application;
import com.facebook.react.ReactApplication;
import com.microsoft.codepush.react.CodePush;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;
import java.util.Arrays;
import java.util.List;
public class MainApplication extends Application implements ReactApplication {
private CodePush _codePush;
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
protected boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}
@Override
protected String getJSBundleFile() {
return _codePush.getJSBundleFile("index.android.bundle");
}
@Override
protected List<ReactPackage> getPackages() {
_codePush = new CodePush(BuildConfig.CODEPUSH_KEY, MainApplication.this, BuildConfig.DEBUG);
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
_codePush
);
}
};
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
}
I have added the codepush.gradle to app/build.gradle
apply from: "../../node_modules/react-native/react.gradle"
apply from: "../../node_modules/react-native-code-push/android/codepush.gradle"
After creating a staging bundle
code-push release-react <appName> Staging
I built a staging APK
cd android && ./gradlew assembleStaging
Which successfully builds a staging build variant (deployment key omitted)
staging {
...
applicationIdSuffix ".staging"
buildConfigField "String", "CODEPUSH_KEY", '"<KEY>"'
}
I then installed it on an Android emulator
./gradlew installStaging
But when I open up the app on the emulator, it immediately crashes. So I viewed the debug log using
code-push debug android
And opened (not resumed) the app again and I see the following output
Loading JS bundle from "assets://index.android.bundle"
Unable to get the hash of the binary's bundled resources - "codepush.gradle" may have not been added to the build definition.
I checked the release history using
code-push deployment ls
And it shows No installs recorded for the Staging deployment.
I am using:
code push CLI 1.12.3-beta
code push 1.8.0-beta
react-native-code-push 1.14.1-beta
react-native 0.29.2
This ended up being an issue with React Native. It seems react.gradle expects all release variants to have the phrase release in them, otherwise it builds it as a development build. This caused my app to crash. By renaming my _staging_ variant to _stagingRelease_ and running
./gradlew assembleStagingRelease
It builds the release APK, but pointed at the _Staging_ CodePush deployment, like I want. It's then just a matter of installing the APK on the device or emulator
./gradlew installStagingRelease
And confirming from the logs and release history that it is indeed retrieving bundles from the Staging deployment.
Thanks for the issue and update @greena13, this is helpful!
@greena13 Hi I have the same error but I didn't understand how to fix it? can you describe your solution please?
@SimchaShats edit your app/build.gradle's buildTypes and ensure any release or staging builds end in release or Release, eg stagingRelease. Otherwise, Gradle will not create the release bundle of your application. There is something incompatible with CodePush when it is built for release and when React still has all its debugging and development configuration turned on.
Can you show your whole build.grdale file, please?
@greena13 It worked like a charm. Thanks @greena13.
Saved my day girl! Thanks a million.
@greena13 can you show us the snippet of build.gradle file.
Most helpful comment
This ended up being an issue with React Native. It seems
react.gradleexpects all release variants to have the phrasereleasein them, otherwise it builds it as a development build. This caused my app to crash. By renaming my _staging_ variant to _stagingRelease_ and runningIt builds the release APK, but pointed at the _Staging_ CodePush deployment, like I want. It's then just a matter of installing the APK on the device or emulator
And confirming from the logs and release history that it is indeed retrieving bundles from the Staging deployment.