How to add a project under Android Studio to jni (NDKbuild)

Here's how to add jni to the original project. Because ndkbuild has been used on eclipse before, we are familiar with it. Here we just talk about ndkbuild.

After upgrading to Android studio 3.0.1, the supported buildToolsVersion is also a higher version of 26.0.2. The version of gradle is 3.0.1.

So, if we have C or C + + code on hand, how can we add it to the project?

1. Create java files that reference so libraries, and create native methods.

public class MyMeetJni {

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

    public static native String helloJni();

    public static native int sum(int a, int b);

    public static native String getCode(String source);
}


2. In the Android studio environment, use the Terminal tool to input instructions.

javac com/kv/mymeet/MyMeetJni.java
At this time, MyMeetJni.class will be generated in the directory.

Reenter command

javah -jni com.kv.mymeet.MyMeetJni
At this time, com? KV? Mymeet? Mymeetjni. H will be generated in the directory

3. Create jni directory and c file.

For example, create main.c to directly cover the content generated above into main.c. Method to add the variable name of the parameter. Here is an example.

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_kv_mymeet_MyMeetJni */

#ifndef _Included_com_kv_mymeet_MyMeetJni
#define _Included_com_kv_mymeet_MyMeetJni
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_kv_mymeet_MyMeetJni
 * Method:    helloJni
 * Signature: ()Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_kv_mymeet_MyMeetJni_helloJni(JNIEnv * env, jclass jobj) {
    return (*env)->NewStringUTF(env,"hello JNI world");
}

/*
 * Class:     com_kv_mymeet_MyMeetJni
 * Method:    sum
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_com_kv_mymeet_MyMeetJni_sum(JNIEnv * env, jclass jobj, jint a, jint b) {
    return a + b;
}

/*
 * Class:     com_kv_mymeet_MyMeetJni
 * Method:    getCode
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */
JNIEXPORT jstring JNICALL Java_com_kv_mymeet_MyMeetJni_getCode(JNIEnv * env, jclass jobj, jstring source) {
    return  source;
}

#ifdef __cplusplus
}
#endif
#endif


4. Create Android.mk file

Usually, there is a sample directory under the NDK directory, which contains many instances. We select one of the instances, click it to copy Android.mk, and put it under the jni directory of the project, with some modifications as follows.

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE:= myMeetJni
LOCAL_SRC_FILES:= main.c
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_EXPORT_LDLIBS := -llog

include $(BUILD_SHARED_LIBRARY)

5. In Android studio, in Android view, right-click the selected module and click Link C++ project with gradle.

If you choose NDK build mode, specify the Android.mk file path.

That is to say, this code will be added under the builde.gradle file.

    externalNativeBuild {
        ndkBuild {
            path 'src/main/jni/Android.mk'
        }
    }






Tags: Android Gradle Java Eclipse

Posted on Sun, 03 May 2020 12:19:42 -0400 by sasikumar81