AndroidStudio JNI development demo on Mac
Configuration
- Download NDK and CMake
- Configure the ndk path in local.properties
- Add the ndk path to the class path of the system-(export PATH = PATH: PATH:PATH: ANDROID_NDK_HOME /)
Development
- Add in defaultConfig in app/build.gradle:
ndk {
moduleName "hello"
abiFilters "armeabi", "armeabi-v7a"
}
- Create native methods in Java class
public class Md5Utils {
static {
System.loadLibrary("hello");//gradle中 ndk中的moduleName
}
public static native String sign(String source);
}
- Cd the location in the terminal to the java folder, execute
javah -jni 包名+类名(Md5Utils)
- The above command will produce the Md5Utils.h file under the Java file (the file naming rule is: replace. In the package name with _, and then add the class name .h)
- Create a jni folder under the main folder, copy the produced .h file to the jni folder, then create a .c/.cpp file under the jni folder, and then import the .h file in the .c/.cpp file ,code show as below:
#include <jni.h>
#include <stdlib.h>
#include<string.h>
#include "com_example_demos_Md5Utils.h"
char* Jstring2Cstr(JNIEnv* env, jstring strs){
char* rtn = NULL;
jclass classString = (*env)->FindClass(env, "java/lang/String");
jstring strenCode = (*env)->NewStringUTF(env,"UTF-8");
jmethodID mid = (*env)->GetMethodID(env,classString,"getBytes","(Ljava/lang/String;)[B");
jbyteArray barr = (jbyteArray) (*env)->CallObjectMethod(env,strs,mid,strenCode);
jsize alen = (*env)->GetArrayLength(env,barr);
jbyte* ba = (*env)->GetByteArrayElements(env,barr,JNI_FALSE);
if(alen > 0){
rtn = (char*)malloc(alen+1);
memcpy(rtn,ba,alen);
rtn[alen]=0;
}
(*env)->ReleaseByteArrayElements(env,barr,ba,0);
return rtn;
}
jstring JNICALL Java_com_example_demos_Md5Utils_sign
(JNIEnv* env, jclass cls, jstring strs){
char* str = "::this hello is from jni";
char* cstr = Jstring2Cstr(env,strs);
strcat(cstr,str);
jstring string = (*(*env)).NewStringUTF(env, cstr);
return string;
}
- Create Android.mk in the jni folder, the content is as follows
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello
LOCAL_SRC_FILES := Md5Utils.c
include $(BUILD_SHARED_LIBRARY)
- Create Application.mk in the root directory of the project with the following content:
# Build all machine code.
APP_ABI := ALL
-
In the terminal, cd to the jni directory, and execute the ndk-build command (if it prompts command not found, splice the ndk path before ndk-build: ndkPath/ndk-build)
-
After the command is executed successfully, the libs folder will be generated under the main file, and there will be the produced .so file inside.
-
Create a jniLibs folder under the app folder, and add sourceSets.main.jniLibs.srcDirs = ['jniLibs'] to specify the lib path of the so file in the defaultConfig in app/build.gradle, and copy the so file generated by the above name to This directory.
-
Then you can call the native method in the java code to implement the java call c code.