簡體   English   中英

如何在 React Native 中調用原生 Android 模塊庫的鏈接

[英]How to call a Link a Native Android Module Library in React Native

我一直在嘗試使用Android 庫模塊,但是每當我將其添加為依賴項時,我的React Native項目都無法構建:

build.gradle

dependencies {
    . . .
    implementation project(path: ':TestLibrary')
    . . . .
}

本機 Class:

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();
    }
}  

我試圖調用的模塊庫 Class

package com.testlibrary;

public class TestLibClass {
    public Integer testLibClassMethod() {
        return 123;
    }
}

我如何 go 關於調用 Android 模塊庫?

謝謝大家。

以下是每當我調用npx react-native run-android時的 StackTrace :

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.

它告訴您問題所在 - 您的依賴項需要 SDK 版本 32,您正在針對 31 進行編譯。修復您的清單(或 React Native 的等效項)以將 compileSdkVersion 指定為 32。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM