无法为 android 运行 react native release build

Posted

技术标签:

【中文标题】无法为 android 运行 react native release build【英文标题】:Unable to run react native release build for android 【发布时间】:2018-10-22 18:31:42 【问题描述】:

我已经在模拟器和手机上多次运行react-native run-android 来开发应用程序,但现在当我创建发布版本时,apk 已成功构建,但当我尝试在物理设备上运行时它立即崩溃,没有错误日志。在模拟器上运行相同的发布版本工作正常,所以我不确定这里的问题是什么,并且如果没有任何崩溃日志很难调试。

这是我的project build.gradle 文件

buildscript 
    repositories 
        google()
        jcenter()
    
    dependencies 
        classpath 'com.android.tools.build:gradle:3.1.2'
        classpath 'com.google.gms:google-services:3.2.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    




allprojects 
    repositories 


        google()
        mavenCentral()
        maven  url 'https://jitpack.io' 

        mavenLocal()
        jcenter()
        maven 
            // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
            url "$rootDir/../node_modules/react-native/android"
        
    

app build.gradle

android 
    compileSdkVersion 27
    buildToolsVersion '27.0.3'

    defaultConfig 
        applicationId "in.calc"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 27
        versionName "3.0.0-beta-1"
        /*ndk 
            abiFilters "armeabi-v7a", "x86"
        */
    

    signingConfigs 
        release 
            if (project.hasProperty('MYAPP_RELEASE_STORE_FILE')) 
                storeFile file(MYAPP_RELEASE_STORE_FILE)
                storePassword MYAPP_RELEASE_STORE_PASSWORD
                keyAlias MYAPP_RELEASE_KEY_ALIAS
                keyPassword MYAPP_RELEASE_KEY_PASSWORD
            
        
    

    splits 
        abi 
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk true  // If true, also generate a universal APK
            // include "armeabi-v7a", "x86"
            include "armeabi-v7a", "arm64-v8a"
        
    
    buildTypes 
        release 
            signingConfig signingConfigs.release
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        
    
    // 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, 'arm64-v8a': 2]
            def abi = output.getFilter(OutputFile.ABI)
            if (abi != null)   // null for the universal-debug, universal-release variants
                output.versionCodeOverride = versionCodes.get(abi) * 100000 + variant.versionCode
            
        
    


dependencies 

    implementation(project(':react-native-admob')) 
        exclude group: 'com.google.android.gms'
    

    implementation(project(':react-native-firebase')) 
        transitive = false
    

    implementation("com.google.android.gms:play-services-gcm:15.0.1") 
        force = true
    

    implementation("com.google.android.gms:play-services-ads:15.0.1") 
        force = true
    

    implementation("com.google.android.gms:play-services-base:15.0.1") 
        force = true
    

    implementation("com.google.firebase:firebase-core:15.0.2") 
        force = true
    

    implementation("com.google.firebase:firebase-messaging:15.0.2") 
        force = true
    

    implementation project(':react-native-video-exoplayer')
    implementation project(':react-native-share')
    implementation project(':react-native-fetch-blob')
    implementation project(':react-native-orientation')
    implementation project(':react-native-youtube')
    implementation project(':react-native-vector-icons')
    implementation fileTree(dir: "libs", include: ["*.jar"])
    implementation "com.android.support:appcompat-v7:27.1.1"
    implementation "com.facebook.react:react-native:+"  // From node_modules


// 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 plugin: 'com.google.gms.google-services'

package.json


  "name": "calc",
  "version": "0.0.1",
  "private": true,
  "scripts": 
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "build-ios": "react-native bundle --entry-file index.js --platform ios --dev false --bundle-output ios/prudent/main.jsbundle --assets-dest ios",
    "build-android": "react-native bundle --entry-file index.js --platform android --dev false  --bundle-output android/main.jsbu‌​ndle",
    "test": "jest"
  ,
  "dependencies": 
    "axios": "^0.18.0",
    "moment": "^2.22.1",
    "react": "^16.3.1",
    "react-native": "0.55.4",
    "react-native-admob": "^2.0.0-beta.5",
    "react-native-auto-height-image": "^1.0.0",
    "react-native-elements": "^1.0.0-beta5",
    "react-native-fetch-blob": "^0.10.8",
    "react-native-firebase": "4.1.0",
    "react-native-htmlview": "^0.12.1",
    "react-native-orientation": "^3.1.3",
    "react-native-share": "^1.0.27",
    "react-native-vector-icons": "^4.6.0",
    "react-native-youtube": "^1.1.0",
    "react-navigation": "^1.5.12",
    "react-native-video": "^2.0.0"
  ,
  "devDependencies": 
    "babel-jest": "22.4.3",
    "babel-preset-react-native": "4.0.0",
    "jest": "22.4.3",
    "react-test-renderer": "16.3.1"
  ,
  "jest": 
    "preset": "react-native"
  

任何帮助将不胜感激。

【问题讨论】:

【参考方案1】:

对于任何寻找答案的人,我在寻找答案时发现了多种原因。我的问题是gradle.properties 中缺少android.useDeprecatedNdk=true

除此之外,我还遇到了 abi 拆分的问题,在这篇文章 Mixing 32- and 64-bit Dependencies in Android 中有更好的解释。

基本上,一旦遇到 64 位库,android 就会将 32 位库抛出窗口,不幸的是,对于 react-native 来说,大部分是 32 位的,这就是整个应用程序崩溃的原因。因此,诀窍是排除任何可能包含在 apk 中的 64 位库,这会迫使 android 回退到使用 32 位系统。一旦谷歌决定强制执行64 bit support for apps by 2019,这将导致一些问题。

有助于调试上述情况的链接

(1)(2)(3)(4)(5)(6)

【讨论】:

以上是关于无法为 android 运行 react native release build的主要内容,如果未能解决你的问题,请参考以下文章

SyntaxError - node_modules/react-native/Libraries/polyfills/error-guard.js:缺少分号。 (14:4) 在 react nati

[react-native]Getting Started for Android on Windows

[react-native]Getting Started for Android on Windows

React Native Android入门实战及深入源码分析系列——React Native源码编译

无法为 ios 运行 react 本机项目

无法运行 react-native run-android?