点击AS的File->Settings或者快捷键Ctrl+Alt+S,打开设置界面,如下图

参数
-d $ModuleFileDir$\src\main\cpp\ -bootclasspath $ModuleSdkPath$\platforms\android-26\android.jar -classpath $ModuleFileDir$\build\intermediates\classes\debug com.app.jni.SdkNative
创建完之后,点OK保存。最后在菜单栏Tools->External Tools->Javah就会自动生成你要的头文件了。
自动生成method和signature工具类
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* JNI动态注册方法生成器
* 这是一个J2SE工具类
*/
public class JniMethodsGenerator {
static List<Method> list;
public static void main(String[] args) {
String path = ".\\src\\main\\cpp\\com_jni_Native.h";//javah生成的头文件路径
try {
list = new ArrayList<>();
FileInputStream in = new FileInputStream(new File(path));
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line = reader.readLine();
Method method = null;
while (line != null) {
if (line.contains("Method:")) {
method = new Method();
method.method = line.substring(line.lastIndexOf(":") + 1).trim();
} else if (line.contains("Signature:")) {
method.signature = line.substring(line.lastIndexOf(":") + 1).trim();
list.add(method);
reader.readLine();//
line = reader.readLine();//JNIEXPORT void JNICALL Java_com_jni_Fjsmthlib_debug
int l = "JNIEXPORT ".length();
method.type = line.substring(l, line.indexOf(" ", l + 1)).trim();
line = reader.readLine();
method.params = line;
}
line = reader.readLine();
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
List<Method> methods = new ArrayList<>();
methods.addAll(list);
while (list.size() > 0) {
Method method = list.remove(0);
String jni = String.format("{\"%s\", \"%s\", (void *) jni_%s},", method.method, method.signature, method.method);
System.out.println(jni);
}
while (methods.size() > 0) {
Method method = methods.remove(0);
System.out.println(String.format("extern %s jni_%s %s", method.type, method.method, method.params.trim()));
}
}
static class Method {
String type;
String method;
String signature;
String params;
}
}
本文链接:https://www.it72.com/12290.htm