Home > front end >  How to call a Link a Native Android Module Library in React Native
How to call a Link a Native Android Module Library in React Native

Time:08-24

I've been tring to use an Android Library Module but my React Native project fails building whenever I add it as a dependancy :

build.gradle

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

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

The Module Library Class I'm trying to call :

package com.testlibrary;

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

How can I go about calling an Android Module Library?

Thank you all in advance.

The following is the StackTrace whenever I call 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.

CodePudding user response:

It's telling you the problem- your dependency requires SDK version 32, you're compiling against 31. Fix your manifest (or React Native's equivalent) to specify compileSdkVersion as 32.

  • Related