一、配置Android Studio的External Tools。
编辑窗口保持在刚刚新建的写有Native方法的Class上。然后File->Setting->Tools->External Tools->Add External Tools。
image.png
点击加号。
二、在新添加的界面配置生成头文件信息
如图:
image.png
下面几个参数照抄也可以(windows):
Program: $JDKPath$/bin/javah //前提配置过JDK,或者AS中有openJDK Parameters:-classpath . -jni -encoding UTF-8 -d $ModuleFileDir$/src/main/cpp/include $FileClass$ Working directory: $ModuleFileDir$\src\main\java
在MacOS上配置有些不同:
Program: /Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/bin/javah //program路径我使用的绝对路径,因为使用AS默认路径找不到javah Parameters:-classpath . -jni -encoding UTF-8 -d $SourcepathEntry$ $FileClass$ Working directory: $SourcepathEntry$
点击确定即可。
三、生成头文件
写好native方法的java文件,随便找了一点:
public class NativeAPI { public static native String stringFromJNI(); public static native String urlprotocolinfo(); public static native String avformatinfo(); public static native String avcodecinfo(); public static native String avfilterinfo(); }
在NativeAPI文件右键->External Tools->javah
image.png
它生成到哪里了呢?就是配置javah时候的Parameters
中-d指定的位置:
image.png
完成!
下面顺便给出CMakeList.txt示例
cmake_minimum_required(VERSION 3.4.1) #编译 add_library( # Sets the name of the library. FlyNative # Sets the library as a shared library. SHARED # Provides a relative path to your source file(s). src/main/jni/com_flyscale_testaes_AESHelper.cpp) #链接 target_link_libraries( # Specifies the target library. FlyNative # Links the target library to the log library included in the NDK. ${log-lib} )