一、开发环境
Android Studio: 3.3.3
FFMPEG: 3.4
NDK: android-ndk-r16b
二、新建项目
C++支持选择默认就行
AS 3.3.3中CMakeLists.txt在main下cpp文件夹内,建议移动到app目录下,main下新建ffmpeg文件夹,ffmpeg文件夹下新建armeabi-v7a文件夹,将编译好的ffmpeg文件赋值到相应文件夹下
include: FFmpeg的方法的头文件
lib: 生成的so动态链接库
share: 一些FFmpeg的示例程序
app层次build.gradle中进行修改
defaultConfig {
externalNativeBuild {
cmake {
cppFlags ""
}
ndk {
abiFilters "armeabi-v7a"
}
}
//打包apk将ffmpeg的so库打包进libs目录里面
sourceSets{
main{
jniLibs.srcDirs = ['src/main/ffmpeg']
}
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
编写CMakeLists.txt
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# 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.
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/native-lib.cpp)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log)
# set 设置变量 //${CMAKE_SOURCE_DIR} 表示CMakeLists.txt所在的目录
set(JNI_LIBS_DIR ${CMAKE_SOURCE_DIR}/src/main/ffmpeg)
message("JNI_LIBS_DIR:" ${CMAKE_SOURCE_DIR})
# ${ANDROID_ABI}表示各种平台,armeabi或x86
# add_library( xx ) “配置加载动态库的方式 avcodec”动态库的名称,“SHARED ”表示加载的是动态库
# set_target_properties(xx)配置加载动态库的路径
add_library(avutil
SHARED
IMPORTED )
set_target_properties(avutil
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libavutil.so )
add_library(swresample
SHARED
IMPORTED )
set_target_properties(swresample
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libswresample.so )
add_library(swscale
SHARED
IMPORTED )
set_target_properties(swscale
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libswscale.so )
add_library(avcodec
SHARED
IMPORTED )
set_target_properties(avcodec
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libavcodec.so )
add_library(avformat
SHARED
IMPORTED )
set_target_properties(avformat
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libavformat.so )
add_library(avfilter
SHARED
IMPORTED )
set_target_properties(avfilter
PROPERTIES IMPORTED_LOCATION
${JNI_LIBS_DIR}/${ANDROID_ABI}/libavfilter.so )
# 指定ffmpeg的头文件的路径 ,如果这里不指定的话,我们在编写代码时include头文件需要写很长的路径
# ffmpeg本身的头文件之间互相引用就是默认这个路径作为根目录的
# 如果不指定这个目录,编译的时候会在ffmpeg的头文件里面报错,说找不到其他头文件
include_directories(src/main/ffmpeg/include)
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
# 链接so库。这里有个要注意的地方就是:如果我们在编写代码时使用了ANativeWindow相关的方法的话,
# 需要在链接库文件的时候加入android这个库文件,不然会报undefined reference to的错
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
avutil
swresample
swscale
avcodec
avformat
avfilter
android
)
开始调用,新建类,定义native方法,通过System.loadLibrary("库名");先看一下怎么通过java调用C/C++中方法和C/C++调用java方法
/**
* @author : create by kirito
* Time: 2019/4/15
*/
public class Test {
static {
//将默认的MainActivity中的删除
System.loadLibrary("native-lib");
System.loadLibrary("avcodec");
System.loadLibrary("avfilter");
System.loadLibrary("avformat");
System.loadLibrary("avutil");
System.loadLibrary("swresample");
System.loadLibrary("swscale");
}
/**
* java层调用native层方法
*/
public static native int CalculateSum(int a, int b);
}
AS通过下面的控制台进入到项目app/src/main/java/ 目录下,通过命令生成头文件
javah 包名.类名
执行成功后在java目录下会看到.h头文件
复制这个方法到新建项目默认生成的cpp文件中,我这是native-lib.cpp
#include <jni.h>
#include <string>
extern "C" JNIEXPORT jstring JNICALL
Java_com_tp_learning_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
}
/*
* 如果本地代码是C++(.ccp或.cc),需要添加extern "C"
* JNIEXPORT 和JNICALL是固定格式不能省,其中jint是返回类型
* 方法命名规则:Java_包名_类名_方法名,其中Java_必须大写,包名中的.替换成_,包名中的_替换成_1
* 使用javah生成Native方法对应的Native函数声明,
* 几乎所有的Native函数的第一个参数永远是JNIEnv指针,而第二个参数永远是jobject或jclass中的一个
* JNIEnv *env:JNI接口指针,通过这个JNIEnv* 指针,就可以对Java端的代码进行操作
* jclass jclass1:当所声明Native方法是静态方法时,对应参数jclass,因为静态方法不依赖对象实例,而依赖于类,所以参数中传递的是一个jclass类型。
* 如果声明的Native方法时非静态方法时,那么对应参数是jobject
* 剩下的就是java层方法参数
*
*/
extern "C"
JNIEXPORT jint JNICALL Java_com_tp_learning_Test_CalculateSum
(JNIEnv *env, jclass jclass1, jint a, jint b){
jint sum = a + b;
return sum;
}
编写完成之后,build->make,编译错误一般都是路径问题,没有错误编写测试代码
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity:";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int sum = Test.CalculateSum(2, 3);
Log.d(TAG, String.valueOf(sum));
}
}
04-15 16:43:20.835 18477-18477/com.tp.learning D/MainActivity:: sum = 5
native调用java中方法
java中定义一个简单方法 getHello()
public class Test {
static {
System.loadLibrary("native-lib");
System.loadLibrary("avcodec");
System.loadLibrary("avfilter");
System.loadLibrary("avformat");
System.loadLibrary("avutil");
System.loadLibrary("swresample");
System.loadLibrary("swscale");
}
/**
* java call native
*/
public static native int CalculateSum(int a, int b);
/**
* native call java
*/
public String getHello(){
return "Hello ---";
}
/**
* get string "Hello"
*/
public static native String getHelloStr();
}
native中定义方法callGetHello用来调用java中的方法
jstring callGetHello(JNIEnv *env,jobject jobject1){
//加载要调用的方法所在类的class对象,FindClass参数是方法所在类的包名/类名
jclass jc = env->FindClass("com/tp/learning/Test");
//得到对应的方法对象,第二个参数是要调用的方法名,第三个是要调用方法的参数类型和返回类型
jmethodID jmethodID1 = env->GetMethodID(jc, "getHello", "()Ljava/lang/String;");
//得到类对象
jobject obj = env->AllocObject(jc);
//调用方法
jstring str = (jstring) env->CallObjectMethod(obj, jmethodID1);
return str;
}
extern "C"
JNIEXPORT jstring JNICALL Java_com_tp_learning_Test_getHelloStr
(JNIEnv *env, jclass jclass1){
return callGetHello(env,jclass1);
}
MainActivity中添加代码测试
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity:";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int sum = Test.CalculateSum(2, 3);
Log.d(TAG, "sum = "+String.valueOf(sum));
String str = Test.getHelloStr();
Log.d(TAG,"get string hello :"+str);
}
}
运行结果
04-15 17:01:10.445 21646-21646/com.tp.learning D/MainActivity:: sum = 5
04-15 17:01:10.445 21646-21646/com.tp.learning D/MainActivity:: get string hello :Hello ---
未完待续