#include #include ...">

"Unable to solve the corresponding JNI function" Android Studio

Native code native.c

#include <string.h>
#include <stdio.h>
#include <jni.h>

jstring Java_com_lab5_oli_myapplication_MainActivity_helloWorld(JNIEnv* env,jobject obj)
{
    return (*env)->NewStringUTF(env,"Hello world");
}

Android.mk file

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE:=ocrex
LOCAL_SRC_FILES:=ndkTest.c

include $(BUILD_SHARED_LIBRARY)

Application.mk File

APP_ABI := all

code in MainActivity

public native String helloWorld();
static{
    System.loadLibrary("ocrex");
}

The method is recognized as declared in native code (note in the sidebar)

+4
source share
1 answer

First, if you use Android 2.2 and higher, use Cmake, because the default build tool for Android Studio for native libraries is CMake. But if you need ndk-build studio, it still supports ndk-build.

1) Add JNIEXPOT and JNICALL to your own method and make sure com_lab5_oli_myapplication is the package name of the MainActivity class.

#include <string.h>
#include <stdio.h>
#include <jni.h>

JNIEXPORT jstring  JNICALL Java_com_lab5_oli_myapplication_MainActivity_helloWorld(JNIEnv* env,jobject obj)
{
    return (*env)->NewStringUTF(env,"Hello world");
}

2) Android.mk , ++ - native.c, Android.mk ndkTest.c.

LOCAL_SRC_FILES:=ndkTest.c
//change it to 
LOCAL_SRC_FILES:=native.c

, gradle . 1) Android 2.2 , , Link ++ gradle. ndk-build, Android.mk, Cmake, CmakeLists. 2) gradle, . externalNativeBuild build.gradle cmake ndkBuild:   cmake

 externalNativeBuild {

    // Encapsulates your CMake build configurations.
    cmake {

      // Provides a relative path to your CMake build script.
      path "CMakeLists.txt"
    }
  }

ndk-build

externalNativeBuild {

    // Encapsulates your CMake build configurations.
    ndkBuild {

      // Provides a relative path to your to the Android.mk build script.
      path "Android.mk"
    }
  }

cmake ndk android this .

+3

Source: https://habr.com/ru/post/1672757/


All Articles