React-native: Unable to load script from assets 'index.android.bundle'.make sure you bundle is packaged correctly

Created on 24 May 2019  路  10Comments  路  Source: facebook/react-native

When trying to build and run release build on android after all the signing certs are setup I am getting this error. It took me a few days to get this resolved. The default android:bundle script created by react-native init is:
react-native bundle --dev false --platform android --entry-file index.js --bundle-output ./android/app/build/intermediates/assets/debug/index.android.bundle --assets-dest ./android/app/build/intermediates/res/merged/debug

However, when you try to run react-native -run-android --variant=release, it's looking for the index.android.bundle in android/app/src/main/assets which doesn't exist

I found this medium post about running this command for bundling

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

This command puts the index.android.bundle in the correct place and I can then run the release version on my connected device.

So, I don't know if this is a bug or by design since I am not an android developer. Which bundle script should we use for sending the apk to playstore?

React Native version:
info
React Native Environment Info:
System:
OS: macOS 10.14.6
CPU: (8) x64 Intel(R) Core(TM) i5-8259U CPU @ 2.30GHz
Memory: 378.34 MB / 16.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 10.14.1 - ~/.nvm/versions/node/v10.14.1/bin/node
Yarn: 1.13.0 - ~/.yarn/bin/yarn
npm: 6.4.1 - ~/.nvm/versions/node/v10.14.1/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2
Android SDK:
API Levels: 23, 25, 26, 27, 28
Build Tools: 23.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 27.0.1, 27.0.3, 28.0.3
System Images: android-27 | Google APIs Intel x86 Atom
IDEs:
Android Studio: 3.4 AI-183.6156.11.34.5522156
Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
npmPackages:
react: 16.8.3 => 16.8.3
react-native: 0.59.8 => 0.59.8

Bug Android Locked 馃摝Bundler

Most helpful comment

Probably due to React Native 0.59.x doesn't support com.android.tools.build:gradle:3.4+ (https://github.com/facebook/react-native/issues/24968, https://github.com/facebook/react-native/issues/24854) but was fixed in 0.60 via https://github.com/facebook/react-native/commit/30348f789946dc99f5ccd02c85c8decbdb9ac29b
For the time being until 0.60 released or the fix is cherrypicked to 0.59.x please use com.android.tools.build:gradle:3.3.2 for now

All 10 comments

Probably due to React Native 0.59.x doesn't support com.android.tools.build:gradle:3.4+ (https://github.com/facebook/react-native/issues/24968, https://github.com/facebook/react-native/issues/24854) but was fixed in 0.60 via https://github.com/facebook/react-native/commit/30348f789946dc99f5ccd02c85c8decbdb9ac29b
For the time being until 0.60 released or the fix is cherrypicked to 0.59.x please use com.android.tools.build:gradle:3.3.2 for now

@billtlee - medium posts about these issues are great !

I experienced same issue in the following environment with "com.android.tools.build:gradle:3.3.2" on classpath :

React Native Environment Info:
System:
OS: macOS 10.14.5
CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
Memory: 1.04 GB / 16.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 8.15.1 - ~/.nvm/versions/node/v8.15.1/bin/node
Yarn: 1.15.2 - ~/.nvm/versions/node/v8.15.1/bin/yarn
npm: 6.4.1 - ~/.nvm/versions/node/v8.15.1/bin/npm
SDKs:

  iOS SDK:
    Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2
  Android SDK:
    API Levels: 23, 26, 27, 28
    Build Tools: 27.0.3, 28.0.3
    System Images: android-23 | Google APIs Intel x86 Atom_64, android-28 | Google Play Intel x86 Atom
IDEs:
  Android Studio: 3.4 AI-183.5429.30.34.5452501
  Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
npmPackages:
  react: 16.6.3 => 16.6.3 
  react-native: 0.58.6 => 0.58.6

Simply, when I run application in release variant I got 'index.android.bundle' error. Probably because it's missing something or just the filesystem path is broken in some way (as @billtlee suggested).

So when I run command:

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/

in src/main/assets/
I can see multiple drawable-* folders and index.android.bundle file have been created.

Although some blogs suggests that I need to delete drawable-** folders
I can successfully run react-native run-android --variant=release even without deleting them.
(tested on real device)

Hope that it will be fixed soon.
thx

2nd EDIT:
We also needed to downgrade gradle definition in android/app/gradle/wrapper/gradle-wrapper.properties (we used gradle-4.10.2-all.zip )
This issue was referenced from another github thread, where guys found that gradle 5 was not supported.

@majirosstefan suggestion does work but unless you want to run that command every time you change your code, I'd suggest downgrading your gradle version back to 3.3.x as @guhungry suggested.

Upgrading from 0.57.8 to 0.59.8.
I get the same error (using com.android.tools.build:gradle:3.3.2 in build.gradle), but in my case there is no index.android.bundle being generated.
Running a find . -name "index.android.bundle" -print returns nothing

Here is the react-native info output:

> react-native info                                                            
info 
  React Native Environment Info:
    System:
      OS: macOS 10.14.5
      CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
      Memory: 126.35 MB / 16.00 GB
      Shell: 5.3 - /bin/zsh
    Binaries:
      Node: 12.1.0 - /usr/local/bin/node
      Yarn: 1.13.0 - /usr/local/bin/yarn
      npm: 6.9.0 - /usr/local/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
      Android SDK:
        API Levels: 23, 24, 25, 26, 27
        Build Tools: 23.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.3, 28.0.0, 28.0.0, 28.0.1, 28.0.2, 28.0.3
        System Images: android-25 | Google APIs Intel x86 Atom, android-25 | Google Play Intel x86 Atom, android-26 | Google APIs Intel x86 Atom, android-27 | Google APIs Intel x86 Atom
    IDEs:
      Android Studio: 3.4 AI-183.6156.11.34.5522156
      Xcode: 10.1/10B61 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.8.3 => 16.8.3 
      react-native: 0.59.8 => 0.59.8 
    npmGlobalPackages:
      create-react-native-app: 1.0.0
      react-native-cli: 2.0.1
      react-native-git-upgrade: 0.2.7
      react-native-rename: 2.2.2

Please upgrade to RN 0.59.9 which supports all versions of Android Gradle Plugin. Before 0.59.9, JS bundle is not in an APK and crash when you use Android Gradle Plugin 3.4.x.

@dulmandakh please re-open this issue. I'm still experiencing exactly the same issue after upgrading to 0.59.9 as you suggested

> react-native info
info 
  React Native Environment Info:
    System:
      OS: macOS 10.14.5
      CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
      Memory: 450.77 MB / 16.00 GB
      Shell: 5.3 - /bin/zsh
    Binaries:
      Node: 12.4.0 - /usr/local/bin/node
      Yarn: 1.16.0 - /usr/local/bin/yarn
      npm: 6.9.0 - /usr/local/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
      Android SDK:
        API Levels: 23, 24, 25, 26, 27, 28
        Build Tools: 23.0.1, 25.0.2, 25.0.3, 26.0.1, 26.0.2, 26.0.3, 27.0.3, 28.0.0, 28.0.0, 28.0.1, 28.0.2, 28.0.3
        System Images: android-25 | Google APIs Intel x86 Atom, android-25 | Google Play Intel x86 Atom, android-26 | Google APIs Intel x86 Atom, android-27 | Google APIs Intel x86 Atom
    IDEs:
      Android Studio: 3.4 AI-183.6156.11.34.5522156
      Xcode: 10.1/10B61 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.8.3 => 16.8.3 
      react-native: 0.59.9 => 0.59.9 
    npmGlobalPackages:
      create-react-native-app: 1.0.0
      react-native-cli: 2.0.1
      react-native-git-upgrade: 0.2.7
      react-native-rename: 2.2.2

I tried to change build.gradle (in the project.ext.react section) from

bundleInDebug: false,

to

bundleInDebug: true,

So, with this change, the bundle is being generated:

> ls -l $(find . -name "index.android.bundle" -print)
-rw-r--r--  1 lukie  staff  6268568 Jun 21 12:30 ./android/app/build/generated/assets/react/debug/index.android.bundle
-rw-r--r--  1 lukie  staff  6268568 Jun 21 12:30 ./android/app/build/intermediates/assets/debug/index.android.bundle

but the error is the same

Screen Shot 2019-06-19 at 10 40 01 AM

metro _is_ running:

 ps -aef|grep start
  501 90978 90946   0 12:19PM ttys000    0:06.05 node node_modules/react-native/local-cli/cli.js start --reset-cache
  501 94150 78910   0 12:32PM ttys000    0:00.00 grep --color start

@dudeinthemirror what gradle version are you using?

Thanks for your reply @kgnadinger.
So, in the android/gradle/wrapper/gradle-wrapper.properties I have:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

In the android/build.gradle I have:

dependencies {
        classpath('com.android.tools.build:gradle:3.4.0')
        classpath('com.google.gms:google-services:3.1.1')
    }

I also migrated the project to AndroidX.

NOTE: I have another project with the exact same gradle settings, also migrated to AndroidX which works fine. The difference is that, in that case I have only the default variant defined in android/app/build.gradle

    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
        }
    }

whereas the project that doesn't work has 3 variants defined:

buildTypes {
        debug {
            applicationIdSuffix "debug"
            signingConfig signingConfigs.debug 
            resValue "string", "deployment_key", "" 
        }
        staging {
            applicationIdSuffix "staging"
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.staging
            resValue "string", "deployment_key", "bleep"
            matchingFallbacks = ['release']
        }
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
            resValue "string", "deployment_key", "bleep"
        }
    }

and I run this by doing:

react-native run-android --variant=debug --appIdSuffix 'debug'

again, this all worked fine in the previous react-native version (0.57.8) before the upgrade.

UPDATE: I tried running:

react-native run-android --variant=release

This time, I don't get the Red Screen Of Death, but the app crashes upon start.
adb logcat '*:E' reports:

06-22 11:43:51.521 13369 13454 E AndroidRuntime: FATAL EXCEPTION: create_react_context
06-22 11:43:51.521 13369 13454 E AndroidRuntime: Process: com.invisionapp.ifa, PID: 13369
06-22 11:43:51.521 13369 13454 E AndroidRuntime: java.lang.RuntimeException: Unable to load script. Make sure you're either running a Metro server (run 'react-native start') or that your bundle 'index.android.bundle' is packaged correctly for release.
06-22 11:43:51.521 13369 13454 E AndroidRuntime:    at com.facebook.react.bridge.CatalystInstanceImpl.jniLoadScriptFromAssets(Native Method)
06-22 11:43:51.521 13369 13454 E AndroidRuntime:    at com.facebook.react.bridge.CatalystInstanceImpl.loadScriptFromAssets(CatalystInstanceImpl.java:218)
06-22 11:43:51.521 13369 13454 E AndroidRuntime:    at com.facebook.react.bridge.JSBundleLoader$1.loadScript(JSBundleLoader.java:31)
06-22 11:43:51.521 13369 13454 E AndroidRuntime:    at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:247)
06-22 11:43:51.521 13369 13454 E AndroidRuntime:    at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:1152)
06-22 11:43:51.521 13369 13454 E AndroidRuntime:    at com.facebook.react.ReactInstanceManager.access$900(ReactInstanceManager.java:123)
06-22 11:43:51.521 13369 13454 E AndroidRuntime:    at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:943)
06-22 11:43:51.521 13369 13454 E AndroidRuntime:    at java.lang.Thread.run(Thread.java:764)
06-22 11:43:51.586  2796  2796 E pageboostd: Received HALT command code 2

This might help someone having the same issue: this is the workaround that I found for now:

cd android
./gradlew clean && ./gradlew assemble$FLAVOR --stacktrace
 cp ./app/build/generated/assets/react/$flavor/index.android.bundle ./app/src/main/assets/index.android.bundle
react-native run-android

where $FLAVOR is e.g. Debug and $flavor is debug

I have this in android/build.gradle

dependencies {
        classpath('com.android.tools.build:gradle:3.4.0')
       . . .
    }

and this in gradle-wrapper.properties:

. . .
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
. . .
Was this page helpful?
0 / 5 - 0 ratings

Related issues

jlongster picture jlongster  路  3Comments

phongyewtong picture phongyewtong  路  3Comments

DreySkee picture DreySkee  路  3Comments

lazywei picture lazywei  路  3Comments

WG-Com picture WG-Com  路  3Comments