当前位置: 首页 > 知识库问答 >
问题:

React Native应用程序崩溃:无法从资产“index.android.bundle”加载脚本

东郭良弼
2023-03-14

在设备上运行 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")
}

知道问题出在哪里吗?

编辑:由于Firebase模块需要3.2.0,所以我无法将渐变构建工具从3.2.0降级到3.1.4。

共有3个答案

颜华池
2023-03-14

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

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

在您的应用程序/构建中。gradle添加jsBundleDirRelease

project.ext.react = [
    entryFile: "index.js",
    jsBundleDirRelease: "$buildDir/intermediates/merged_assets/release/mergeReleaseAssets/out"
]
秦昊穹
2023-03-14

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

喻高寒
2023-03-14

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

 类似资料:
  • 我是初学者。我已经使用“反应本机运行Android”命令在Ubuntu上运行了反应本机项目。我在模拟器上收到错误“无法从资产“index.android.bundle”加载脚本。确保您的包包装正确,或者您正在运行包服务器。”

  • 在使用./gradlew assembledebug构建apk后,我得到了这个错误。如果我用./gradlew assembleRelease构建apk,我会在手机上安装应用程序时出现错误。当我用react-native run-android在虚拟设备中运行相同的应用程序时,应用程序运行良好,没有任何麻烦。有什么想法吗?

  • 我已经使用 命令在 Windows 上运行了一个 React Native 项目。我在模拟器中收到以下错误: 无法从资产' index.android.bundle '加载脚本。请确保您的软件包已正确打包,或者您正在运行软件包服务器。 我试过这些链接,但不管用。 无法从 Windows 上的 assets index.android.bundle 加载脚本。

  • 似乎地铁捆扎机没有装货...它正在打开和关闭,在关闭之前,我看不到里面有任何东西在运行。因此,模拟器没有运行应用程序。 运行后我收到错误: react-原生初始化项目 react-原生运行-android package.json Genymods模拟器S8 Galaxy

  • 我已确保bundler和模拟器正在运行。 如果我用这封信 它可能工作,但当我在模拟器中按rr时,刷新不会发生。我还尝试了abd的反转。对我来说什么都不起作用。 单击此处查看错误屏幕。

  • 我刚刚在Google Play上构建并部署了一个应用程序。当我通过Android Studio运行它时,它运行得很好,但现在当我从Google Play下载它时,它崩溃了。因为这是我第一次,我甚至不知道如何查看从Google Play下载的应用程序的崩溃报告/堆栈跟踪。我感谢所有的帮助。 更新 所以我得到了APK的堆栈跟踪。它告诉我我的类无法创建我的类。我不知道为什么它会出现这个错误。一切正常。看