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

时间:2018-10-14 17:55:40

标签: android react-native build crash

在发布模式下(在调试模式下工作正常)在设备上运行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。

5 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:2)

从gradle构建工具3.2.1降级到3.1.4也为我解决了这个问题。我花了几天的时间才弄清楚。

答案 2 :(得分:1)

您未指定所使用的react-native版本。我在0.57.3遇到了同样的问题,其他答案使我走上了正轨:带有android gradle插件版本3.2的android构建系统中存在一个错误,该问题已在0.57.5中解决( {3}}。

错误修复程序为changelog,如果您不想升级RN,手动修复非常简单。

答案 3 :(得分:1)

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

  

react-native软件包--platform android --dev false --entry-file   index.js-捆绑输出   android / app / src / main / assets / index.android.bundle --assets-dest   android / app / src / main / resreact-native软件包--platform android --dev   false-入口文件index.js-捆绑输出   android / app / src / main / assets / index.android.bundle --assets-dest   android / app / src / main / res

对我有用

答案 4 :(得分:1)

此修复程序将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"
]