# 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.18.1)# Declares and names the project.
project("ffmpeg")#######################ffmpeg libs config###########################add h files
include_directories(${CMAKE_SOURCE_DIR}/h/ffmpeg)
include_directories(${CMAKE_SOURCE_DIR}/h/self_jni)#add libs#this way need config "sourceSets {#main {#jniLibs.srcDirs = ['src/main/jni/libs']#}#}" in app build.gradle
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}")#source cpp files
file(GLOB source_files ${CMAKE_SOURCE_DIR}/cpp/*.cpp)#######################ffmpeg libs config########################### 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.
ffmpeg
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).${source_files})# 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)# 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.
target_link_libraries(# Specifies the target library.
ffmpeg
#FFmpeg libs
avcodec
avfilter
avformat
avutil
swresample
swscale
# Links the target library to the log library# included in the NDK.${log-lib})
JNI 有用的基本配置:
1.Log的定义,base.h
//// Created by mingzz_ on 2023/2/7.//#ifndefFFMPEG_BASE_H#defineFFMPEG_BASE_H#include<jni.h>#include<android/log.h>#defineDEBUGtrue#defineLOGI(...)\if(DEBUG)\__android_log_print(ANDROID_LOG_INFO,"mingzz-JNINative",__VA_ARGS__)#defineLOGD(...)\if(DEBUG)\__android_log_print(ANDROID_LOG_DEBUG,"mingzz-JNINative",__VA_ARGS__)#defineLOGW(...)\if(DEBUG)\__android_log_print(ANDROID_LOG_WARN,"mingzz-JNINative",__VA_ARGS__)#defineLOGE(...)\if(DEBUG)\__android_log_print(ANDROID_LOG_ERROR,"mingzz-JNINative",__VA_ARGS__)#endif//FFMPEG_BASE_H
2.jni动态注册
#include"base.h"
jstring sayHello(JNIEnv *env, jclass clazz){
std::string hello ="hello FFmpeg!!!!";return env->NewStringUTF(hello.c_str());}/***********************************************************************************************************
* register native methods
* ********************************************************************************************************//*
* Class and package name
* static const char *classPathName = "com/qualcomm/qti/usta/core/AccelCalibrationJNI";
* *///com.mingzz__.h26x.rtmp.RTMPActivitystaticconstchar*classPathName_1 ="com/ming/ffmpeg/MainActivity";/*
* List of native methods
* {"startCalibration" , "()V", (void *)startCalibration},
* {"getAccelCalFromNative" , "()Ljava/lang/String;", (void *)getAccelCalFromNative},
* {"stopCalibration" , "()V", (void *)stopCalibration},
* */static JNINativeMethod methods_1[]={{"stringFromJNI","()Ljava/lang/String;",(void*) sayHello},};/*
* Register several native methods for one class.
*
*
* */staticintregisterNativeMethods(JNIEnv *envVar,constchar*inClassName, JNINativeMethod *inMethodsList,int inNumMethods){
jclass javaClazz = envVar->FindClass(inClassName);if(javaClazz ==NULL){return JNI_FALSE;}if(envVar->RegisterNatives(javaClazz, inMethodsList, inNumMethods)<0){return JNI_FALSE;}return JNI_TRUE;}/*
* Register native methods for all classes we know about.
*
* Returns JNI_TRUE on success
*
* */staticintregisterNatives(JNIEnv *env){if(!registerNativeMethods(env, classPathName_1, methods_1,sizeof(methods_1)/sizeof(methods_1[0]))){return JNI_FALSE;}return JNI_TRUE;}typedefunion{
JNIEnv *env;void*venv;} UnionJNIEnvToVoid;/*
* This is called by the VM when the shared library is first loaded.
* */
JNIEXPORT jint
JNI_OnLoad(JavaVM *vm,void*reserved){
UnionJNIEnvToVoid uenv;
uenv.venv =NULL;
JNIEnv *env =NULL;if(vm->GetEnv(&uenv.venv, JNI_VERSION_1_4)!= JNI_OK){return-1;}
env = uenv.env;if(registerNatives(env)!= JNI_TRUE){return-1;}LOGI("JNI_OnLoad Register Natives Methods Success!!!");return JNI_VERSION_1_4;}