附个不错的博客 https://www.jianshu.com/p/87ce6f565d37
在Android Framework中,需要提供一种媒介或 桥梁,将Java层(上层)与C/C++层(下层)有机的联系起来,使得他们互相协调完成某些任务。而充当这种媒介的就是Java本地接口(JNI,Java Native Interface)。JNI提供一些列的接口,允许Java类与C/C++等本地编辑语言(在JNI中,这些语言被称为 本地语言)编写的应用 程序、模块 、库进行交互操作。比如,在Java类中使用C语言库中的函数或在C语言中使用 Java类库,都需要借助JNI。
Android NDK是一个开发工具集,提供一系列工具快速开发C/C++的动态库,并能自动将 .so/.dll 和 Java 应用一起打包到Apk;
NDK提供工具可以方便JNI调用C/C++,而且提供了交叉编译器可以修改.mk文件生成特定CPU平台的动态库,并能将so和java应用一起打包到apk中;简单说就是JNI负责Java与C/C++进行互相操作,NDK提供工具方便在Android平台使用JNI;
extern "C"
JNIEXPORT jstring JNICALL
native_test_ffmpeg(JNIEnv *env,jobject thiz){
return env->NewStringUTF("native_test_ffmpeg");
}
//nativeTest是java的native方法名,native_test_ffmpeg是c/c++的实现方法,"()Ljava/lang/String;"代表此方法输入参数为空,输出参数为String
static JNINativeMethod g_methods[] = {
{"nativeTest","()Ljava/lang/String;",(void*)native_test_ffmpeg}
};
//把c/c++方法和java方法关联起来,不需要按照“Java_包名_类名_方法名”规则来生成方法
jint JNI_OnLoad(JavaVM *vm, void *reserved) {
JNIEnv *env = NULL;
vm->GetEnv((void**)&env,JNI_VERSION_1_6);
jclass pJclass = env->FindClass(JNI_CLASS_PATH);
env->RegisterNatives(pJclass,g_methods,sizeof(g_methods)/sizeof(g_methods[0]));
return JNI_VERSION_1_6;
}
输入参数在"()"内,输出参数在"()"外,多个参数顺序存放,数组用"["标识。例如:
([Student;)[Student; => Student[] xxx(Student[]){}
(I)S => short xxx(int i){}
基本数据类型对应符号如下:
boolean Z
byte B
char C
short S
int I
long L
float F
double D
void V
java类对应:
L包路径/类名
extern "C"
JNIEXPORT jstring JNICALL
Java_com_example_ffmpegtest_MainActivity_stringFromJNI2(JNIEnv *env, jobject thiz) {
//step1 找到类
jclass sJclass = env->FindClass(JNI_CLASS_PATH_2);
//step2 找到方法
jmethodID method_init_id = env->GetMethodID(sJclass, "<init>", "()V");//找到构造方法
jmethodID method_set_id = env->GetMethodID(sJclass, "setYear", "(I)V");//找到构造方法
jmethodID method_get_id = env->GetMethodID(sJclass, "getYear", "()I");//找到构造方法
//step3 new object
jobject obj = env->NewObject(sJclass, method_init_id);
//step4 调用相应方法
env->CallVoidMethod(obj,method_set_id,18);
jint year = env->CallIntMethod(obj, method_get_id);
char temp[] = {0,};
sprintf(temp,"%d",year);
std::string helloF = "hello ffmpeg from C++, year =";
helloF.append(temp);
return env->NewStringUTF(helloF.c_str());
}
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
#项目名称,也就是我安卓项目的名字
project("ffmpegtest")
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
# 方式一(验证不通过)
# 引入FFmpeg的库文件,设置内部的方式引入,指定库的目录是 -L 指定具体的库-l
# 这种方式方便快捷
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L../../../libs/${CMAKE_ANDROID_ARCH_ABI}")
#设置ffmpeg库所在路径的变量
set(FF ${CMAKE_SOURCE_DIR}/libs/${CMAKE_ANDROID_ARCH_ABI})
add_library(avcodec SHARED IMPORTED) #三个参数,第一个,库名称;第二个,SHARED动态库或STATIC静态库;第三个,IMPORTED或xx.cpp
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${FF}/libavcodec.so) #来源,此处是添加外部引入的库libs/libavcodec.so->avcodec
add_library(avformat SHARED IMPORTED)
set_target_properties(avformat PROPERTIES IMPORTED_LOCATION ${FF}/libavformat.so)
add_library(avutil SHARED IMPORTED)
set_target_properties(avutil PROPERTIES IMPORTED_LOCATION ${FF}/libavutil.so)
add_library(swscale SHARED IMPORTED)
set_target_properties(swscale PROPERTIES IMPORTED_LOCATION ${FF}/libswscale.so)
add_library(swresample SHARED IMPORTED)
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${FF}/libswresample.so)
add_library(avfilter SHARED IMPORTED)
set_target_properties(avfilter PROPERTIES IMPORTED_LOCATION ${FF}/libavfilter.so)
add_library( native-lib
SHARED
${CMAKE_SOURCE_DIR}/src/main/cpp/native-lib.cpp)#来源是从native-lib.cpp文件编译
find_library( log-lib log )
find_library( android-lib android )
include_directories(${CMAKE_SOURCE_DIR}/libs/includes)
target_link_libraries( #目标库
native-lib
# 把ffmpeg的动态库以来进来
avformat
avcodec
avutil
avfilter
swscale
swresample
${log-lib}
${android-lib})
project("ffmpegtest")
add_library( native-lib SHARED ${CMAKE_SOURCE_DIR}/src/main/cpp/native-lib.cpp)
add_library(avcodec SHARED IMPORTED)
set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${FF}/libavcodec.so)
find_library( android-lib android )
include_directories(${CMAKE_SOURCE_DIR}/libs/includes)
target_link_libraries( #目标库
native-lib
# 把ffmpeg的动态库以来进来
avformat
avcodec
avutil
avfilter
swscale
swresample
${log-lib}
${android-lib})
原文:https://www.cnblogs.com/yidijimao/p/14077775.html