【问题标题】:React Native app crash: Unable to load script from assets 'index.android.bundle'React Native 应用程序崩溃:无法从资产“index.android.bundle”加载脚本
【发布时间】:2018-10-14 17:55:39
【问题描述】:

在设备上以发布模式运行 React Native 应用程序时,我在启动时遇到崩溃(调试模式工作正常)。主要错误似乎是:

AndroidRuntime:java.lang.RuntimeException:无法从资产“index.android.bundle”加载脚本。确保您的捆绑包已正确打包,或者您正在运行打包服务器。

我已经找到了关于这个错误的各种线程,但是有关于旧版本的,并且提供的解决方案都没有工作。

日志如下:

12-16 19:20:28.581 29088 29109 E AndroidRuntime: FATAL EXCEPTION: Thread-3
12-16 19:20:28.581 29088 29109 E AndroidRuntime: Process: net.cozic.joplin, PID: 29088
12-16 19:20:28.581 29088 29109 E AndroidRuntime: java.lang.RuntimeException: Unable to load script from assets 'index.android.bundle'. Make sure your bundle is packaged correctly or you're running a packager server.
12-16 19:20:28.581 29088 29109 E AndroidRuntime:        at com.facebook.react.bridge.CatalystInstanceImpl.jniLoadScriptFromAssets(Native Method)
12-16 19:20:28.581 29088 29109 E AndroidRuntime:        at com.facebook.react.bridge.CatalystInstanceImpl.loadScriptFromAssets(CatalystInstanceImpl.java:216)
12-16 19:20:28.581 29088 29109 E AndroidRuntime:        at com.facebook.react.bridge.JSBundleLoader$1.loadScript(JSBundleLoader.java:32)
12-16 19:20:28.581 29088 29109 E AndroidRuntime:        at com.facebook.react.bridge.CatalystInstanceImpl.runJSBundle(CatalystInstanceImpl.java:243)
12-16 19:20:28.581 29088 29109 E AndroidRuntime:        at com.facebook.react.ReactInstanceManager.createReactContext(ReactInstanceManager.java:1114)
12-16 19:20:28.581 29088 29109 E AndroidRuntime:        at com.facebook.react.ReactInstanceManager.access$900(ReactInstanceManager.java:116)
12-16 19:20:28.581 29088 29109 E AndroidRuntime:        at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:913)
12-16 19:20:28.581 29088 29109 E AndroidRuntime:        at java.lang.Thread.run(Thread.java:761)
12-16 19:20:28.582  1695  1707 W ActivityManager:   Force finishing activity net.cozic.joplin/.MainActivity

这是我的 app/build.gradle 文件:

apply plugin: "com.android.application"

import com.android.build.OutputFile

apply from: "../../node_modules/react-native/react.gradle"

def enableSeparateBuildPerCPUArchitecture = false
def enableProguardInReleaseBuilds = false

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion rootProject.ext.buildToolsVersion

    defaultConfig {
        applicationId "net.cozic.joplin"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 2097414
        versionName "1.0.178"
        ndk {
            abiFilters "armeabi-v7a", "x86"
        }
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk false  // If true, also generate a universal APK
            include "armeabi-v7a", "x86"
        }
    }
    signingConfigs {
        release {
            if (project.hasProperty('JOPLIN_RELEASE_STORE_FILE')) {
                storeFile file(JOPLIN_RELEASE_STORE_FILE)
                storePassword JOPLIN_RELEASE_STORE_PASSWORD
                keyAlias JOPLIN_RELEASE_KEY_ALIAS
                keyPassword JOPLIN_RELEASE_KEY_PASSWORD
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            signingConfig signingConfigs.release
        }
    }
    // applicationVariants are e.g. debug, release
    applicationVariants.all { variant ->
        variant.outputs.each { output ->
            // For each separate APK per architecture, set a unique version code as described here:
            // http://tools.android.com/tech-docs/new-build-system/user-guide/apk-splits
            def versionCodes = ["armeabi-v7a":1, "x86":2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null) {  // null for the universal-debug, universal-release variants
                output.versionCodeOverride =
                        versionCodes.get(abi) * 1048576 + defaultConfig.versionCode
            }
        }
    }
}

dependencies {
    implementation project(':react-native-firebase')
    implementation (project(':react-native-camera')) {
        exclude group: "com.google.android.gms"
    }
    implementation project(':react-native-file-viewer')
    implementation project(':react-native-securerandom')
    implementation project(':react-native-fs')
    implementation project(':react-native-image-picker')
    implementation project(':react-native-vector-icons')
    implementation project(':react-native-fs')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.facebook.react:react-native:+"  // From node_modules
    implementation project(':react-native-sqlite-storage')
    implementation project(':rn-fetch-blob')
    implementation project(':react-native-document-picker')
    implementation project(':react-native-image-resizer')
    implementation project(':react-native-share-extension')
    implementation project(':react-native-version-info')
    implementation "com.facebook.react:react-native:+"

    implementation "com.google.android.gms:play-services-base:16.0.1" // For Firebase
    implementation "com.google.firebase:firebase-core:16.0.4" // For Firebase
    implementation "com.google.firebase:firebase-messaging:17.3.4" // For Firebase
    implementation 'me.leolin:ShortcutBadger:1.1.21@aar' // For Firebase - this line if you wish to use badge on Android

    compile ("com.android.support:support-v4:26.0.1") {
        force = true //<-- force dependency resolution to 26.0.1 in my case
    }
}

// Run this once to be able to run the application with BUCK
// puts all compile dependencies into folder libs for BUCK to use
task copyDownloadableDepsToLibs(type: Copy) {
    from configurations.compile
    into 'libs'
}

apply from: "../../node_modules/react-native-vector-icons/fonts.gradle"
apply plugin: 'com.google.gms.google-services' // For Firebase

还有android/build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext {
        buildToolsVersion = "27.0.3"
        minSdkVersion = 16
        compileSdkVersion = 27
        targetSdkVersion = 26
        supportLibVersion = "27.1.1"
    }
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0' // Upgraded from 3.1.4 to 3.2.0 for Firebase
        classpath 'com.google.gms:google-services:4.0.1' // For Firebase

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        mavenLocal()
        google()
        jcenter() // Was added by me - still needed?
        maven {
            url "https://maven.google.com"
        }
        maven {
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        }
    }
}


subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android")) {
            android {
                compileSdkVersion 26
                buildToolsVersion "27.0.3"
            }
        }
    }
}


task wrapper(type: Wrapper) {
    gradleVersion = '4.4'
    distributionUrl = distributionUrl.replace("bin", "all")
}

知道可能是什么问题吗?

编辑:我无法将 gradle 构建工具从 3.2.0 降级到 3.1.4,因为 Firebase 模块需要 3.2.0。

【问题讨论】:

  • Tareq,这个问题中的解决方案是针对调试版本的,而在我的情况下,它是仅在发布模式下发生的崩溃。无论如何,我已经尝试在那里应用一些解决方案,但它们都不起作用(可能是因为它们适用于旧版本的 RN)。
  • 你能分享你的 package.json 文件吗?您还使用 Expo 或 ignite 进行项目生成吗?
  • 我不使用 Expo,它是一个被弹出的应用程序。包文件在那里:github.com/laurent22/joplin/blob/master/ReactNativeClient/…

标签: android react-native build crash


【解决方案1】:

您应该尝试将 gradle 构建工具从 3.2.1 降级到 3.1.4

【讨论】:

    【解决方案2】:

    从 gradle build tool 3.2.1 降级到 3.1.4 也为我解决了这个问题。我花了几天时间才弄明白。

    【讨论】:

      【解决方案3】:

      修复方法是将 RN 升级到 0.57.5 或将 gradle 降级到 3.1.4。

      对于那些想要一个非常简洁的解决方法而无需升级/降级的人:

      在您的app/build.gradle 中添加jsBundleDirRelease

      project.ext.react = [
          entryFile: "index.js",
          jsBundleDirRelease: "$buildDir/intermediates/merged_assets/release/mergeReleaseAssets/out"
      ]
      

      【讨论】:

      • 我在 0.57.7 但仍然需要添加这个
      【解决方案4】:

      您没有指定您使用的是哪个版本的 react-native。我对0.57.3 有同样的问题,其他答案让我走上了正轨:android gradle 插件版本 3.2 的 android 构建系统中存在一个错误,该错误已在0.57.5 (changelog) 中解决。

      错误修复是here,如果您不想升级 RN,手动集成非常简单。

      【讨论】:

      • 我已经尝试过了,但现在我收到一条新的错误消息,网上的解决方案建议降级到 RN 0.57.2。我认为您可能有解决方案,但 RN 没有,并且 - 一如既往 - 期望开发人员在发布时玩悠悠球。
      • 不知道另一个错误是什么,很难说。您的其他错误是否也与构建有关,或者其他错误?我遇到了这个问题,因为我有构建风格,但你似乎没有,看着你的build.gradle
      • 最终通过升级到 0.57.8 使其工作。感谢您为我指明正确的方向。
      【解决方案5】:

      尝试运行下面给出的命令:

      react-native bundle --platform android --dev false --entry-file index.js --捆绑输出 android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/resreact-native bundle --platform android --dev 错误 --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res

      对我有用

      【讨论】:

        猜你喜欢
        • 2018-07-11
        • 1970-01-01
        • 1970-01-01
        • 2017-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多