新建C++工程
新建
两个externalNativeBuild
一个sourceSets(指定so路径)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
android { compileSdkVersion 29 buildToolsVersion "29.0.3" defaultConfig { ... externalNativeBuild { cmake { cppFlags "-std=c++11 -frtti -fexceptions" abiFilters 'armeabi-v7a' } } sourceSets { main { jniLibs.srcDirs = [ 'src/main/jniLibs' ] } } } ... externalNativeBuild { cmake { path "src/main/cpp/CMakeLists.txt" version "3.10.2" } } } |
复制so和include文件
编写CMakeLists.txt
以下是默认值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
cmake_minimum_required(VERSION 3.4 . 1 ) add_library( native -lib SHARED native -lib.cpp #nativ-lib2.cpp 如果有其他cpp文件可以一并打包到 native -lib中) #查找系统的log库,并赋值给变量log-lib find_library( log-lib log) #将上面log-lib变量里的库连接到 native -lib中 target_link_libraries( native -lib ${log-lib}) |
CMakeLists中添加FFmpeg头文件路径
1
2
3
4
|
#设置FFmpeg头文件的路径 include_directories( include#因为和CMakeLists.txt在同一级,所以直接写include ) |
CMakeLists中添加libavcodec.so
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
#定义一个变量avcodec add_library( avcodec SHARED IMPORTED ) #给avcodec这个变量赋值 set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavcodec.so) #将avcodec混合编译到 native -lib中 target_link_libraries( native -lib ${log-lib} avcodec ) |
CMakeLists中添加全部的so
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
cmake_minimum_required(VERSION 3.4 . 1 ) #设置FFmpeg头文件的路径 include_directories( include#因为和CMakeLists.txt在同一级,所以直接写include ) add_library( native -lib SHARED native -lib.cpp) find_library( log-lib log) # 1 .定义一个变量avcodec add_library( avcodec SHARED IMPORTED ) #给avcodec这个变量赋值 set_target_properties(avcodec PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavcodec- 57 .so) # 2 . add_library( avdevice SHARED IMPORTED ) set_target_properties(avdevice PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavdevice- 57 .so) # 3 . add_library( avfilter SHARED IMPORTED ) set_target_properties(avfilter PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavfilter- 6 .so) # 4 . add_library( avformat SHARED IMPORTED ) set_target_properties(avformat PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavformat- 57 .so) # 5 . add_library( avutil SHARED IMPORTED ) set_target_properties(avutil PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavutil- 55 .so) # 6 . add_library( postproc SHARED IMPORTED ) set_target_properties(postproc PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libpostproc- 54 .so) # 7 . add_library( swresample SHARED IMPORTED ) set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libswresample- 2 .so) # 8 . add_library( swscale SHARED IMPORTED ) set_target_properties(swscale PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libswscale- 4 .so) #将avcodec混合编译到 native -lib中 target_link_libraries( native -lib ${log-lib} avcodec# 1 avdevice# 2 avfilter# 3 avformat# 4 avutil# 5 postproc# 6 swresample# 7 swscale# 8 ) |
编写测试函数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
#include <jni.h> #include <string> extern "C" { #include "libavcodec/avcodec.h" } extern "C" JNIEXPORT jstring JNICALL Java_com_example_myffmpegcmd_MainActivity_stringFromJNI( JNIEnv *env, jobject /* this */ ) { std::string hello = "Hello from C++" ; return env->NewStringUTF(avcodec_configuration()); } |
总结
到此这篇关于Android Studio引入FFmpeg的文章就介绍到这了,更多相关Android Studio引入FFmpeg内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/yu540135101/article/details/104914545