How to use c++ in react-native?

I am trying to use my c++ code in react-native.
I also searched and found how to use the site below
https://thebhwgroup.com/blog/react-native-jni

I wrote the code along the site

android/app/src/main/java/com/thebhwgroup/demo

HelloWorldModule.java

//HelloWorldModule.java
package com.thebhwgroup.demo;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

public class HelloWorldModule extends /*ReactContextBaseJavaModule*/{
    public native String helloWorldJNI();

    static{
        System.loadLibrary("hello_world_jni");
    }

    public HelloWorldModule(ReactApplicationContext reactContext){
        super(reactContext);
    }

    @Override
    public String getName(){    //React-Native에서 Module을 식별하는데 사용하는 문자열 return
        return "HelloWorld";    //JS에서 React.NativeModules.HelloWorld로 참조된다.
    }

    @ReactMethod
    public void helloWorld(Promise promise){    //JAVA에서 JS로 데이터 반환하는 방법 (다른 방법도 있음)
                                                //React-Native 문서에 있음
        try {
            String hello = helloWorldJNI();
            promise.resolve(hello);
        }catch (Exception e){
            promise.reject("ERR", e);
        }
    }
}

MyReactPackage.java

//MyReactPackage.java

//HelloWorldModule.java를 React-Native에 등록하려면 Package를 추가해주어야 하기 때문에 필요한 파일

package com.thebhwgroup.demo;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MyReactPackage implements ReactPackage{
//    @Override
//    public List<Class<? extends JavaScriptModule>> createJSModules(){
//        return Collections.emptyList();
//    }

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext){
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(ReactApplicationContext reactContext){    //HelloWorldModule에 생성되는 모듈과 함께 등록되는 부분
        List<NativeModule> modules = new ArrayList<>();

        modules.add(new HelloWorldModule(reactContext));

        return modules;
    }
}

adroid/app/src/main/java/com/thebhwgroup/

MainApplication.java

  @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new MainReactPackage(),
                    new MyReactPackage()
            );
        }

Identifies the Module in React-native through the getName of HelloWorldModule.java and is referenced in JS. It also returns data through the helloWorld method.

I created a file like below to use JNI.
android/app/src/main/jni/

Android.mk

LOCAL_SRC_FILES := hello_world.c
LOCAL_MODULE := hello_world_jni

Application.mk

APP_ABI = all

hello_world.c

#include<jni.h>

jstring Java_com_thebhwgroup_demo_HelloWorldModule_helloWorldJNI(JNIEnv *env, jobject thiz){
    return (*env)->NewStringUTF(env, "Hello World!");
}

The C file was written in JNI format by referring to the JNI official document and site.

android/app/

build.gradle

android {
    ndkVersion rootProject.ext.ndkVersion

    compileSdkVersion rootProject.ext.compileSdkVersion

    defaultConfig {
        applicationId "com.thebhwgroup"
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"

        ndk {
            abiFilters "armeabi-v7a", "x86"
            moduleName "hello_world_jni"
            ldLibs "log"
            cFlags "-std=c99"
        }
    }
}

Problem

I run my react-native project use react-native run-android but this app is build Failed.

Erro is.
error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
and android/app/build.gradle
build error in import com.android.build.OutputFile in android/app/build.gradle

There seems to be no problem with my environment variables.
What am I doing wrong?

Source: Windows Questions C++

LEAVE A COMMENT