JNI笔记
- 背景
- Demo代码
- JNI.java
- MainActivity.java
- Android.mk
- Application.mk
- com_stone_javacallc_JNI.h
- javacallc.c
- build.gradle
背景
Demo代码
代码结构
JNI.java
package com.stone.javacallc;/*** Created by stoneWang* Created on 2024/1/16* java调用C*/
public class JNI {{System.loadLibrary("javacallc");}/*** 让C代码做加法运算,把结果返回** @param x* @param y* @return*/public native int add(int x, int y);/*** 从Java传入字符串,C代码进行拼接** @param s I am from java* @return I am from java add I am from C*/public native String sayHello(String s);/*** 让C代码给每个元素加上10** @param intArray* @return*/public native int[] increaseArrayEles(int[] intArray);
}
MainActivity.java
package com.stone.javacallc;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;
import android.util.Log;
import android.view.View;public class MainActivity extends AppCompatActivity {private JNI jni;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);jni = new JNI();}public void add(View view) {int result = jni.add(99, 1);Log.e(MainActivity.class.getSimpleName(), "result:" + result);}public void string(View view) {String result = jni.sayHello("I am from java ");Log.e(MainActivity.class.getSimpleName(), "result:" + result);}public void array(View view) {}public void checkpw(View view) {}}
Android.mk
LOCAL_PATH :=$(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=javacallc
LOCAL_SRC_FILES :=javacallc.c
include $(BUILD_SHARED_LIBRARY)
Application.mk
App_ABI:=all
com_stone_javacallc_JNI.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_stone_javacallc_JNI */#ifndef _Included_com_stone_javacallc_JNI
#define _Included_com_stone_javacallc_JNI
#ifdef __cplusplus
extern "C" {
#endif
/** Class: com_stone_javacallc_JNI* Method: add* Signature: (II)I*/
JNIEXPORT jint JNICALL Java_com_stone_javacallc_JNI_add(JNIEnv *, jobject, jint, jint);/** Class: com_stone_javacallc_JNI* Method: sayHello* Signature: (Ljava/lang/String;)I*/
JNIEXPORT jstring JNICALL Java_com_stone_javacallc_JNI_sayHello(JNIEnv *, jobject, jstring);/** Class: com_stone_javacallc_JNI* Method: increaseArrayEles* Signature: ([I)[I*/
JNIEXPORT jintArray JNICALL Java_com_stone_javacallc_JNI_increaseArrayEles(JNIEnv *, jobject, jintArray);#ifdef __cplusplus
}
#endif
#endif
javacallc.c
//
// Created by wanglei on 2024/1/16.
//
# include "com_stone_javacallc_JNI.h"
# include "string.h"/*** 把一个jstring转换成一个c语言的char* 类型.*/
char* _JString2CStr(JNIEnv* env, jstring jstr) {char* rtn = NULL;jclass clsstring = (*env)->FindClass(env, "java/lang/String");jstring strencode = (*env)->NewStringUTF(env,"GB2312");jmethodID mid = (*env)->GetMethodID(env, clsstring, "getBytes", "(Ljava/lang/String;)[B");jbyteArray barr = (jbyteArray)(*env)->CallObjectMethod(env, jstr, mid, strencode); // String .getByte("GB2312");jsize alen = (*env)->GetArrayLength(env, barr);jbyte* ba = (*env)->GetByteArrayElements(env, barr, JNI_FALSE);if(alen > 0) {rtn = (char*)malloc(alen+1); //"\0"memcpy(rtn, ba, alen);rtn[alen]=0;}(*env)->ReleaseByteArrayElements(env, barr, ba,0);return rtn;
}/**** @param env* @param jobj* @param ji* @param jj* @return*/
jint Java_com_stone_javacallc_JNI_add(JNIEnv *env, jobject jobj, jint ji, jint jj){int result = ji + jj;return result;
};jstring Java_com_stone_javacallc_JNI_sayHello(JNIEnv *env, jobject jobj, jstring jstr){char* fromJava = _JString2CStr(env, jstr);char* fromc = "add I am from C";// 拼接函数strcatstrcat(fromJava, fromc); // 把拼接的结果放在第一参数里面// 将char转为jstringreturn (*env)->NewStringUTF(env, fromJava);
};
build.gradle
apply plugin: 'com.android.application'android {compileSdkVersion 30buildToolsVersion "30.0.2"defaultConfig {applicationId "com.stone.javacallc"minSdkVersion 26targetSdkVersion 30versionCode 1versionName "1.0"testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"ndk{moduleName "javacallc"abiFilters "armeabi", "armeabi-v7a", "arm64-v8a" // cpu类型}}buildTypes {release {minifyEnabled falseproguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}externalNativeBuild {ndkBuild {path "src/main/jni/Android.mk"}}
}dependencies {implementation fileTree(dir: "libs", include: ["*.jar"])implementation 'androidx.appcompat:appcompat:1.2.0'implementation 'androidx.constraintlayout:constraintlayout:2.0.1'testImplementation 'junit:junit:4.12'androidTestImplementation 'androidx.test.ext:junit:1.1.2'androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'}