gpt4 book ai didi

android - 如何在使用 ReactNative 最新 AndroidX 版本的 Android 应用程序中使用 Unity 作为库时解决 "libmain.so not found"

转载 作者:行者123 更新时间:2023-12-02 15:18:26 25 4
gpt4 key购买 nike

我们有一个基于react-native-unity-view集成了Unity的ReactNative应用程序很长一段时间以来,它一直成功地作为一个图书馆。然而,在 ReactNative 关于 AndroidX 和最新 SDK 更改的最新更新之后,我们遇到了问题“libmain.so 未找到”。第一次尝试显示 Unity View 时会发生此异常。

有一个类似的issue因此,请不要标记为重复,因为我们的方法可能会有所不同,特别是因为我们在这里提供了更多详细信息。

GitHub 上有一个正在运行且仍在运行的示例(基于 ReactNative 0.57):https://github.com/f111fei/react-native-unity-demo 。只要这个项目基于 ReactNative 0.57 及其适当的 gradle 设置,一切就可以正常工作。但是,一旦我们升级到最新的 ReactNative 版本(例如,甚至是 0.60),它为我们提供了一个与旧版本非常不同的 android 文件夹),就会出现应用程序崩溃的问题。 (我们还尝试从头开始设置所有内容,以排除此问题是由于升级造成的可能性。使用 RN 0.57 一切仍然工作正常,使用 RN 0.60 及更高版本则不行)

异常情况如下:

E Unity   : Failed to load 'libmain.so', the application will terminate.
D AndroidRuntime: Shutting down VM
E AndroidRuntime: FATAL EXCEPTION: main
E AndroidRuntime: Process: com.rnunitydemo, PID: 16887
E AndroidRuntime: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.rnunitydemo-bKGyotdcwjVnBxuR9zLE4Q==/base.apk"],nativeLibraryDirectories=[/data/app/com.rnunitydemo-bKGyotdcwjVnBxuR9zLE4Q==/lib/arm64, /data/app/com.rnunitydemo-bKGyotdcwjVnBxuR9zLE4Q==/base.apk!/lib/arm64-v8a, /system/lib64, /product/lib64]]] couldn't find "libmain.so"

首先,我将列出最新版本中发生崩溃的 android 文件夹和 gradle 文件。之后,我将列出一切正常的文件:

不工作(较新)版本

./android/build.gradle:

buildscript {
ext {
buildToolsVersion = "28.0.3"
minSdkVersion = 16
compileSdkVersion = 28
targetSdkVersion = 28
}
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:3.4.2")
}
}

allprojects {
repositories {
mavenLocal()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url("$rootDir/../node_modules/react-native/android")
}
maven {
// Android JSC is installed from npm
url("$rootDir/../node_modules/jsc-android/dist")
}

google()
jcenter()
maven { url 'https://jitpack.io' }
}
}

./android/settings.gradle:

rootProject.name = 'rnunitydemo'

apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings)

include ":UnityExport"
project(":UnityExport").projectDir = file("./UnityExport")


include ':app'

./android/app/build.gradle:

apply plugin: "com.android.application"

import com.android.build.OutputFile

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

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

def enableSeparateBuildPerCPUArchitecture = false

def enableProguardInReleaseBuilds = false

def jscFlavor = 'org.webkit:android-jsc:+'

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.rnunitydemo"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
}
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // 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 {
signingConfig signingConfigs.debug
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:
// 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"])
implementation "com.facebook.react:react-native:+" // From node_modules

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

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)

./android/UnityExport/build.gradle:

// GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN

buildscript {
repositories {
google()
jcenter()
}

dependencies {
classpath("com.android.tools.build:gradle:3.4.2")
}
}

allprojects {
repositories {
google()
jcenter()
flatDir {
dirs 'libs'
}
}
}

apply plugin: 'com.android.library'


dependencies {
api fileTree(include: ['*.jar'], dir: 'libs')
}

android {
compileSdkVersion 28
buildToolsVersion '28.0.2'

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

defaultConfig {
minSdkVersion 16
targetSdkVersion 28
ndk {
abiFilters 'armeabi-v7a', 'x86'
}
multiDexEnabled true
versionCode 1
versionName '0.1'
}

lintOptions {
abortOnError false
}

aaptOptions {
noCompress = ['.unity3d', '.ress', '.resource', '.obb']
ignoreAssetsPattern = "!.svn:!.git:!.ds_store:!*.scc:.*:!CVS:!thumbs.db:!picasa.ini:!*~"
}

buildTypes {
debug {
minifyEnabled false
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
signingConfig signingConfigs.debug
jniDebuggable true
}
release {
minifyEnabled false
useProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-unity.txt'
signingConfig signingConfigs.debug
}
}

packagingOptions {
doNotStrip '*/armeabi-v7a/*.so'
doNotStrip '*/x86/*.so'
}


}

一旦启动 Unity,上述所有这些文件都会导致崩溃(“libmain.so 未找到”)。

工作(旧)版本

在我们之前的版本中,可以找到on GitHub ,一切顺利:

./android/build.gradle:

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.1.4'
}
}

allprojects {
repositories {
mavenLocal()
jcenter()
google()
maven {
// All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
url "$rootDir/../node_modules/react-native/android"
}
flatDir {
dirs project(':UnityExport').file('libs')
}
}
}

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

./android/settings.gradle:

rootProject.name = 'rnunitydemo'
include ':react-native-unity-view'
project(':react-native-unity-view').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-unity-view/android')

include ":UnityExport"
project(":UnityExport").projectDir = file("./UnityExport")

include ':app'

./android/app/build.gradle:

apply plugin: "com.android.application"

import com.android.build.OutputFile

project.ext.react = [
entryFile: "index.js"
]

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

def enableSeparateBuildPerCPUArchitecture = false

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

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

defaultConfig {
applicationId "com.rnunitydemo"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
ndk {
abiFilters "armeabi-v7a", "x86"
}
}

lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}

splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false // If true, also generate a universal APK
include "armeabi-v7a", "x86"
}
}

signingConfigs {
release {
// storeFile file(MYAPP_RELEASE_STORE_FILE)
// storePassword MYAPP_RELEASE_STORE_PASSWORD
// keyAlias MYAPP_RELEASE_KEY_ALIAS
// keyPassword MYAPP_RELEASE_KEY_PASSWORD
}
}

buildTypes {
debug {
manifestPlaceholders = [
"DEBUGGABLE": "true"
]
}
release {
manifestPlaceholders = [
"DEBUGGABLE": "false"
]
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 {
compile project(':react-native-unity-view')
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
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'
}

./android/UnityExport/build.gradle:

(same as above)

official Unity2D-Forums中还有一些与此问题相关的其他报告,但没有任何可行的解决方案。根据comment我们知道在两个 build.gradle 文件中使用了完全相同的 abiFilter 设置(这来 self 们的应用程序和来自 UnityExport 的 build-gradle),但事实并非如此。也没有解决问题。

有人可以帮忙吗?

** 更新 **

开发机系统信息:

System:
OS: macOS Mojave 10.14.6
CPU: (16) x64 Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz
Memory: 112.35 MB / 32.00 GB
Shell: 5.0.11 - /usr/local/bin/bash

SDKs:
iOS SDK:
Platforms: iOS 13.2, DriverKit 19.0, macOS 10.15, tvOS 13.2, watchOS 6.1
Android SDK:
API Levels: 23, 25, 26, 27, 28, 29
Build Tools: 23.0.1, 25.0.0, 25.0.1, 25.0.2, 26.0.1, 27.0.3, 28.0.3, 29.0.0, 29.0.2
System Images: android-29 | Google APIs Intel x86 Atom
Android NDK: 20.0.5594570
IDEs:
Android Studio: 3.5 AI-191.8026.42.35.5900203
Xcode: 11.2.1/11B500 - /usr/bin/xcodebuild
Binaries:
Node: 10.17.0 - /usr/local/opt/node@10/bin/node
Yarn: 1.16.0 - /usr/local/bin/yarn
npm: 6.11.3 - /usr/local/opt/node@10/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
npmPackages:
react: 16.9.0 => 16.9.0
react-native: 0.61.4 => 0.61.4
npmGlobalPackages:
react-native-cli: 2.0.1

最佳答案

我现在可以自己回答了。该问题是由与 ARM64 不兼容的 Unity 导出引起的,除非手动应用多个设置。方法如下:

在构建 Android 之前,我们需要调整“播放器设置”中的一些设置以使 ARM64 可用:

  1. 将“脚本后端”从 Mono 切换到 ILCPP,这使我们能够至:
  2. 在“目标架构”下选择“ARM64”

enter image description here

如果您在构建时遇到“找不到 NDK”之类的错误,请确保 Unity 已下载并安装了自己的 NDK 副本:

enter image description here

记住所有这些将使我们能够链接到这些库,而不会找不到“libmain.so”。

关于android - 如何在使用 ReactNative 最新 AndroidX 版本的 Android 应用程序中使用 Unity 作为库时解决 "libmain.so not found",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58877735/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com