react-native: React Native 0.62 Universal APK crashed on x86 CPU Android

Description

I tried to build APK for Android after upgrading my project from 0.61.5 to 062, however the build universal APK crashed.

I then tried to built the APK on a fresh React Native 0.62 project and similar thing happens.

I tried the APK on several emulators. On emulators with x86 CPU it crashed but on x86_64 the APK can run without problem.

I don’t know if it is a bug in React Native or I somehow messed up my environments. I would appricate any help, thank you!

React Native version:

yarn run v1.22.4
$ D:\Codes\tryUniversalAPK\node_modules\.bin\react-native info
info Fetching system and libraries information...
System:
    OS: Windows 10 10.0.18363
    CPU: (12) x64 AMD Ryzen 5 2600 Six-Core Processor
    Memory: 19.23 GB / 31.95 GB
  Binaries:
    Node: 13.11.0 - C:\Users\Indra\AppData\Local\Temp\yarn--1585793456760-0.2610294802603219\node.CMD
    Yarn: 1.22.4 - C:\Users\Indra\AppData\Local\Temp\yarn--1585793456760-0.2610294802603219\yarn.CMD
    npm: 6.13.7 - C:\Program Files\nodejs\npm.CMD
    Watchman: Not Found
  SDKs:
    Android SDK: Not Found
  IDEs:
    Android Studio: Version  3.6.0.0 AI-192.7142.36.36.6308749
  Languages:
    Python: 2.7.17 - C:\Python27\python.EXE
  npmPackages:
    @react-native-community/cli: Not Found
    react: 16.11.0 => 16.11.0
    react-native: 0.62.0 => 0.62.0
  npmGlobalPackages:
    *react-native*: Not Found
Done in 1.32s.

Steps To Reproduce

1 Initiate New Project

  • Open terminal
  • npx react-native init tryUniversalAPK
  • cd tryUniversalAPK

2 Create an x86 CPU/ABI based emulator

  • Open Android Virtual Device Manager
  • Create virtual device with x86 CPU
  • Launch the emulator

3 Build (crashed) and Native android log

  • Open new terminal
  • adb logcat *:S ReactNative:V ReactNativeJS:V
  • Open the terminal with tryUniversalAPK directory
  • run yarn android --variant=release
  • The app will crash
  • Open the terminal with adb logcat running, it shows these errors
    --------- beginning of main
    --------- beginning of system
    04-02 08:32:42.891  8782  8782 D ReactNative: ReactInstanceManager.ctor()
    04-02 08:32:42.939  8782  8782 D ReactNative: ReactInstanceManager.createReactContextInBackground()
    04-02 08:32:42.939  8782  8782 D ReactNative: ReactInstanceManager.recreateReactContextInBackgroundInner()
    04-02 08:32:42.939  8782  8782 D ReactNative: ReactInstanceManager.recreateReactContextInBackgroundFromBundleLoader()
    04-02 08:32:42.939  8782  8782 D ReactNative: ReactInstanceManager.recreateReactContextInBackground()
    04-02 08:32:42.939  8782  8782 D ReactNative: ReactInstanceManager.runCreateReactContextOnNewThread()
    --------- beginning of crash
    04-02 08:34:32.749  9021  9021 D ReactNative: ReactInstanceManager.ctor()
    04-02 08:34:32.764  9021  9021 D ReactNative: ReactInstanceManager.createReactContextInBackground()
    04-02 08:34:32.765  9021  9021 D ReactNative: ReactInstanceManager.recreateReactContextInBackgroundInner()
    04-02 08:34:32.765  9021  9021 D ReactNative: ReactInstanceManager.recreateReactContextInBackgroundFromBundleLoader()
    04-02 08:34:32.765  9021  9021 D ReactNative: ReactInstanceManager.recreateReactContextInBackground()
    04-02 08:34:32.765  9021  9021 D ReactNative: ReactInstanceManager.runCreateReactContextOnNewThread()
    

4 Modify gradle to build APK on separate build per CPU architecture

  • Open tryUniversalAPK/android/app/build.gradle
  • change
    def enableSeparateBuildPerCPUArchitecture = false
    
    into
    def enableSeparateBuildPerCPUArchitecture = true
    
  • change
    universalApk false
    
    into
    universalApk true
    

5 Rebuild

  • cd tryUniversalAPK/android
  • run ./gradlew clean
  • cd ..
  • run yarn android --variant=release
  • The app able to run without crash
  • I noticed these in the terminal. The APK installed seems to be the app-x86-release.apk
    > Task :app:installRelease
    08:58:13 V/ddms: execute: running am get-config
    08:58:13 V/ddms: execute 'am get-config' on 'emulator-5554' : EOF hit. Read: -1
    08:58:13 V/ddms: execute: returning
    Installing APK 'app-x86-release.apk' on 'Pixel_3_API_28(AVD) - 9' for app:release
    08:58:13 D/app-x86-release.apk: Uploading app-x86-release.apk onto device 'emulator-5554'
    08:58:13 D/Device: Uploading file onto device 'emulator-5554'
    08:58:13 D/ddms: Reading file permision of D:\Codes\tryUniversalAPK\android\app\build\outputs\apk\release\app-x86-release.apk as: rwx------
    08:58:13 V/ddms: execute: running pm install -r -t "/data/local/tmp/app-x86-release.apk"
    08:58:14 V/ddms: execute 'pm install -r -t "/data/local/tmp/app-x86-release.apk"' on 'emulator-5554' : EOF hit. Read: -1
    08:58:14 V/ddms: execute: returning
    08:58:14 V/ddms: execute: running rm "/data/local/tmp/app-x86-release.apk"
    08:58:14 V/ddms: execute 'rm "/data/local/tmp/app-x86-release.apk"' on 'emulator-5554' : EOF hit. Read: -1
    08:58:14 V/ddms: execute: returning
    Installed on 1 device.
    

6 Testing the built APKs

  • Navigate to tryUniversalAPK\android\app\build\outputs\apk\release folder
  • There are five different APKs
  • I uninstalled the tryUniversalAPK app in the android emulator
  • I drag and drop the app-universal-release.apk to the emulator
  • The app crashed
  • Uninstalled the tryUniversalAPK app (this is the app-universal-release.apk) in the emulator
  • I drag and drop the app-x86-release.apk to the emulator
  • The app can run

Expected Results

The universl APK able to run without crash

ZIP file containing the built APKs

https://1drv.ms/u/s!AlfNopb2XbjM8hSS8PbpaMsL5DnN?e=hf9iRq

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 10
  • Comments: 20

Most upvoted comments

I found a solution from here https://github.com/facebook/react-native/issues/25923#issuecomment-594047945

I added this piece of code in android/app/build.gradle and the app can run with yarn android --variant=release and not crashing.


configurations.all {
    resolutionStrategy {
        force "com.facebook.soloader:soloader:0.8.2"
    }
}

tryUniversalAPK/android/app/build.gradle file

Only two lines were changed def enableSeparateBuildPerCPUArchitecture = true and universalApk true

apply plugin: "com.android.application"

import com.android.build.OutputFile

/**
 * The react.gradle file registers a task for each build variant (e.g. bundleDebugJsAndAssets
 * and bundleReleaseJsAndAssets).
 * These basically call `react-native bundle` with the correct arguments during the Android build
 * cycle. By default, bundleDebugJsAndAssets is skipped, as in debug/dev mode we prefer to load the
 * bundle directly from the development server. Below you can see all the possible configurations
 * and their defaults. If you decide to add a configuration block, make sure to add it before the
 * `apply from: "../../node_modules/react-native/react.gradle"` line.
 *
 * project.ext.react = [
 *   // the name of the generated asset file containing your JS bundle
 *   bundleAssetName: "index.android.bundle",
 *
 *   // the entry file for bundle generation. If none specified and
 *   // "index.android.js" exists, it will be used. Otherwise "index.js" is
 *   // default. Can be overridden with ENTRY_FILE environment variable.
 *   entryFile: "index.android.js",
 *
 *   // https://facebook.github.io/react-native/docs/performance#enable-the-ram-format
 *   bundleCommand: "ram-bundle",
 *
 *   // whether to bundle JS and assets in debug mode
 *   bundleInDebug: false,
 *
 *   // whether to bundle JS and assets in release mode
 *   bundleInRelease: true,
 *
 *   // whether to bundle JS and assets in another build variant (if configured).
 *   // See http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Build-Variants
 *   // The configuration property can be in the following formats
 *   //         'bundleIn${productFlavor}${buildType}'
 *   //         'bundleIn${buildType}'
 *   // bundleInFreeDebug: true,
 *   // bundleInPaidRelease: true,
 *   // bundleInBeta: true,
 *
 *   // whether to disable dev mode in custom build variants (by default only disabled in release)
 *   // for example: to disable dev mode in the staging build type (if configured)
 *   devDisabledInStaging: true,
 *   // The configuration property can be in the following formats
 *   //         'devDisabledIn${productFlavor}${buildType}'
 *   //         'devDisabledIn${buildType}'
 *
 *   // the root of your project, i.e. where "package.json" lives
 *   root: "../../",
 *
 *   // where to put the JS bundle asset in debug mode
 *   jsBundleDirDebug: "$buildDir/intermediates/assets/debug",
 *
 *   // where to put the JS bundle asset in release mode
 *   jsBundleDirRelease: "$buildDir/intermediates/assets/release",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in debug mode
 *   resourcesDirDebug: "$buildDir/intermediates/res/merged/debug",
 *
 *   // where to put drawable resources / React Native assets, e.g. the ones you use via
 *   // require('./image.png')), in release mode
 *   resourcesDirRelease: "$buildDir/intermediates/res/merged/release",
 *
 *   // by default the gradle tasks are skipped if none of the JS files or assets change; this means
 *   // that we don't look at files in android/ or ios/ to determine whether the tasks are up to
 *   // date; if you have any other folders that you want to ignore for performance reasons (gradle
 *   // indexes the entire tree), add them here. Alternatively, if you have JS files in android/
 *   // for example, you might want to remove it from here.
 *   inputExcludes: ["android/**", "ios/**"],
 *
 *   // override which node gets called and with what additional arguments
 *   nodeExecutableAndArgs: ["node"],
 *
 *   // supply additional arguments to the packager
 *   extraPackagerArgs: []
 * ]
 */

project.ext.react = [
    enableHermes: false,  // clean and rebuild if changing
]

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

/**
 * Set this to true to create two separate APKs instead of one:
 *   - An APK that only works on ARM devices
 *   - An APK that only works on x86 devices
 * The advantage is the size of the APK is reduced by about 4MB.
 * Upload all the APKs to the Play Store and people will download
 * the correct one based on the CPU architecture of their device.
 */
def enableSeparateBuildPerCPUArchitecture = true

/**
 * Run Proguard to shrink the Java bytecode in release builds.
 */
def enableProguardInReleaseBuilds = false

/**
 * The preferred build flavor of JavaScriptCore.
 *
 * For example, to use the international variant, you can use:
 * `def jscFlavor = 'org.webkit:android-jsc-intl:+'`
 *
 * The international variant includes ICU i18n library and necessary data
 * allowing to use e.g. `Date.toLocaleString` and `String.localeCompare` that
 * give correct results when using with locales other than en-US.  Note that
 * this variant is about 6MiB larger per architecture than default.
 */
def jscFlavor = 'org.webkit:android-jsc:+'

/**
 * Whether to enable the Hermes VM.
 *
 * This should be set on project.ext.react and mirrored here.  If it is not set
 * on project.ext.react, JavaScript will not be compiled to Hermes Bytecode
 * and the benefits of using Hermes will therefore be sharply reduced.
 */
def enableHermes = project.ext.react.get("enableHermes", false);

android {
    compileSdkVersion rootProject.ext.compileSdkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "com.tryuniversalapk"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
    }
    splits {
        abi {
            reset()
            enable enableSeparateBuildPerCPUArchitecture
            universalApk true  // If true, also generate a universal APK
            include "armeabi-v7a", "x86", "arm64-v8a", "x86_64"
        }
    }
    signingConfigs {
        debug {
            storeFile file('debug.keystore')
            storePassword 'android'
            keyAlias 'androiddebugkey'
            keyPassword 'android'
        }
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.debug
        }
        release {
            // Caution! In production, you need to generate your own keystore file.
            // see https://facebook.github.io/react-native/docs/signed-apk-android.
            signingConfig signingConfigs.debug
            minifyEnabled enableProguardInReleaseBuilds
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
        }
    }

    packagingOptions {
        pickFirst "lib/armeabi-v7a/libc++_shared.so"
        pickFirst "lib/arm64-v8a/libc++_shared.so"
        pickFirst "lib/x86/libc++_shared.so"
        pickFirst "lib/x86_64/libc++_shared.so"
    }

    // 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:
            // https://developer.android.com/studio/build/configure-apk-splits.html
            def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
            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 fileTree(dir: "libs", include: ["*.jar"])
    //noinspection GradleDynamicVersion
    implementation "com.facebook.react:react-native:+"  // From node_modules

    implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0"

    debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}") {
      exclude group:'com.facebook.fbjni'
    }

    debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }

    debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}") {
        exclude group:'com.facebook.flipper'
    }

    if (enableHermes) {
        def hermesPath = "../../node_modules/hermes-engine/android/";
        debugImplementation files(hermesPath + "hermes-debug.aar")
        releaseImplementation files(hermesPath + "hermes-release.aar")
    } else {
        implementation jscFlavor
    }
}

// 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: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project)

As it seems the issue is solved with version 0.8.2 of the soloader, which also is used in the 0.63.0-rc.1.

I found this comment helping me fixing the issue for our app: https://github.com/facebook/react-native/issues/25923#issuecomment-594047945

This needs attention! We can’t switch to 62.x in production if it happens on user devices. Basically anyone upgrading is affected by this.

Additional Info from Android Studio Logcat

image


2020-04-02 09:52:53.796 4952-4952/? E/SoLoader: couldn't find DSO to load: libc++_shared.so caused by: dlopen failed: "/data/data/com.tryuniversalapk/lib-main/libc++_shared.so" has unexpected e_machine: 40 (EM_ARM)
2020-04-02 09:52:53.797 4952-4952/? E/SoLoader: couldn't find DSO to load: libfb.so caused by: APK was built for a different platform
2020-04-02 09:52:53.799 4952-4952/? E/SoLoader: couldn't find DSO to load: libjscexecutor.so caused by: couldn't find DSO to load: libfb.so caused by: APK was built for a different platform
2020-04-02 09:52:53.831 4952-4967/? E/SoLoader: couldn't find DSO to load: libhermes.so
    
    --------- beginning of crash
2020-04-02 09:52:53.834 4952-4967/? E/AndroidRuntime: FATAL EXCEPTION: create_react_context
    Process: com.tryuniversalapk, PID: 4952
    java.lang.UnsatisfiedLinkError: couldn't find DSO to load: libhermes.so
        at com.facebook.soloader.SoLoader.doLoadLibraryBySoName(SoLoader.java:789)
        at com.facebook.soloader.SoLoader.loadLibraryBySoName(SoLoader.java:639)
        at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:577)
        at com.facebook.soloader.SoLoader.loadLibrary(SoLoader.java:525)
        at com.facebook.hermes.reactexecutor.HermesExecutor.<clinit>(HermesExecutor.java:20)
        at com.facebook.hermes.reactexecutor.HermesExecutorFactory.create(HermesExecutorFactory.java:29)
        at com.facebook.react.ReactInstanceManager$5.run(ReactInstanceManager.java:997)
        at java.lang.Thread.run(Thread.java:764)

I found a solution from here #25923 (comment) I added this piece of code in android/app/build.gradle and the app can run with yarn android --variant=release and not crashing.

configurations.all {
    resolutionStrategy {
        force "com.facebook.soloader:soloader:0.8.2"
    }
}

It’s not working as well here. React-Native 0.62.2

Finally, I tried this answer and it works now https://github.com/facebook/SoLoader/issues/55#issuecomment-653890808

same issue in react-native@0.62.2