React Native调用Android原生库模块构建失败,求解决方法
React Native集成Android Library Module构建失败问题
问题描述
我在React Native项目中引入Android Library Module并添加依赖后,项目构建失败,请教如何正确调用该库模块。
相关代码
项目依赖配置(app/build.gradle)
dependencies { . . . implementation project(path: ':TestLibrary') . . . . }
React Native原生模块类
package com.my_native_class; public class MyNativeClass extends ReactContextBaseJavaModule { public MyNativeClass(ReactApplicationContext reactContext) { super(reactContext); } @NonNull @Override public String getName() { return "MyNativeClass"; } @ReactMethod(isBlockingSynchronousMethod = true) public Integer myNativeMethod() { return new TestLibClass().testLibClassMethod(); } }
库模块中的类(TestLibrary模块内)
package com.testlibrary; public class TestLibClass { public Integer testLibClassMethod() { return 123; } }
构建错误日志
执行npx react-native run-android时的报错:
BUILD FAILED in 36s error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Error: Command failed: ./gradlew app:installDebug -PreactNativeDevServerPort=8081 FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:checkDebugAarMetadata'. > A failure occurred while executing com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction > One or more issues found when checking AAR metadata values: Dependency 'androidx.appcompat:appcompat-resources:1.5.0' requires 'compileSdkVersion' to be set to 32 or higher. Compilation target for module ':app' is 'android-31' Dependency 'androidx.appcompat:appcompat:1.5.0' requires 'compileSdkVersion' to be set to 32 or higher. Compilation target for module ':app' is 'android-31' Dependency 'androidx.emoji2:emoji2-views-helper:1.2.0' requires 'compileSdkVersion' to be set to 32 or higher. Compilation target for module ':app' is 'android-31' Dependency 'androidx.emoji2:emoji2:1.2.0' requires 'compileSdkVersion' to be set to 32 or higher. Compilation target for module ':app' is 'android-31' * Try: > Run with --stacktrace option to get the stack trace. > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Get more help at https://help.gradle.org BUILD FAILED in 36s at makeError (/home/MyAwesomeApp/node_modules/execa/index.js:174:9) at /home/MyAwesomeApp/node_modules/execa/index.js:278:16 at processTicksAndRejections (internal/process/task_queues.js:95:5) at async runOnAllDevices (/home/MyAwesomeApp/node_modules/@react-native-community/cli-platform-android/build/commands/runAndroid/runOnAllDevices.js:109:5) at async Command.handleAction (/home/MyAwesomeApp/node_modules/@react-native-community/cli/build/index.js:192:9) info Run CLI with --verbose flag for more details.
解决方案
1. 统一Compile SDK版本
错误核心是主App模块的compileSdkVersion为31,但引入的TestLibrary依赖了需要SDK 32+的AndroidX库。修改项目根目录下的android/build.gradle,将compileSdkVersion升级到32或更高:
buildscript { ext { buildToolsVersion = "33.0.0" minSdkVersion = 21 compileSdkVersion = 32 // 改为32或更高版本 targetSdkVersion = 33 // 其他配置保留 } // 其余代码不变 }
2. 同步库模块版本(可选)
打开TestLibrary/build.gradle,确保其compileSdkVersion引用根项目的统一配置,避免版本不一致:
android { compileSdkVersion rootProject.ext.compileSdkVersion // 其余配置不变 }
3. 清理缓存并重新构建
执行以下命令清理缓存后重新构建:
./gradlew clean npx react-native run-android
4. JS层调用验证
构建成功后,在React Native的JS代码中可以这样调用原生模块:
import { NativeModules } from 'react-native'; const { MyNativeClass } = NativeModules; // 调用同步方法 const nativeResult = MyNativeClass.myNativeMethod(); console.log('原生方法返回值:', nativeResult); // 输出123
内容的提问来源于stack exchange,提问作者Program-Me-Rev
相关产品推荐
相关产品推荐

