1. CMakeLists.txt的配置
文件目录结构如下:
x86_64这一层经测试不加也可以,在CMakeLists.txt里面去掉该路径就可以了。
build.gradle:
经过实验证明,不使用jniLibs作为库名字都是可以的,只要在CMakeLists.txt里面指定了库的路径,能找到就可以。对于CMake的安装,其实版号也不是太重要,使用平台默认的,没有就去SDK里面下载。在里面指定CMakeLists.txt的路径就行,位置在哪无所谓,只要ndk能根据build.gradle中给定的CMakeLists.txt找到它,它里面又有外部动态库、库的依赖、链接信息,就能生成一个native的so文件。
apply plugin: 'com.android.application' android { compileSdkVersion 28 buildToolsVersion '28.0.3' defaultConfig { applicationId "com.example.android21" minSdkVersion 21 targetSdkVersion 28 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" externalNativeBuild { cmake { cppFlags "" } ndk { abiFilters "x86_64" } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } externalNativeBuild { cmake { path "src/main/cpp/CMakeLists.txt" version "3.10.2" } } } dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' testImplementation 'junit:junit:4.13' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' }
2. 总结
该版本ffmpeg使用ndk-r21b构建,对于APILevel-21、APILevel-28都是适用的,由此推断,对于APILevel-29,APILevel-30应该同样适用。
CMakeLists.txt里面添加文件的时候要注意名称规则。这里添加的文件一定要在java里面去load,否则运行不起来。
static { System.loadLibrary("avutil"); System.loadLibrary("avcodec"); System.loadLibrary("native-lib"); }
#include <jni.h> #include <string> extern "C" { #include "include/libavcodec/avcodec.h" #include "include/libavutil/avutil.h" } extern "C" JNIEXPORT jstring JNICALL Java_com_example_android21_MainActivity_stringFromJNI( JNIEnv* env, jobject obj) { // std::string hello = avcodec_configuration(); return env->NewStringUTF(avcodec_configuration()); }
另外要注意的是要把编译的库架构信息匹配起来。so、模拟器,这里都采用x86_64.