1.新建一個空專案(環境 android studio4.0 ndk 20.0.+)

2.點擊File->project structure... 進行ndk路徑的配置

3.在app->src->main下面新建一個jniLibs目錄

4.把ffpmeg編譯出來的so庫匯入進去

5.在app->src->main下面新建一個cpp檔案夾,在檔案夾下面新建CMakeLists.txt
6.把ffmpeg的include放到cpp檔案夾下

7.接下來先編輯下CMakeLists.txt
cmake_minimum_required(VERSION 3.10.2)
#設定FFmpeg頭檔案的路徑,共享庫,include里面h檔案呼叫其他h檔案用到的
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-58.so)
#2.
add_library(
avdevice
SHARED
IMPORTED
)
set_target_properties(avdevice PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavdevice-58.so)
#3.
add_library(
avfilter
SHARED
IMPORTED
)
set_target_properties(avfilter PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavfilter-7.so)
#4.
add_library(
avformat
SHARED
IMPORTED
)
set_target_properties(avformat PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavformat-58.so)
#5.
add_library(
avutil
SHARED
IMPORTED
)
set_target_properties(avutil PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libavutil-56.so)
#6.
add_library(
postproc
SHARED
IMPORTED
)
set_target_properties(postproc PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libpostproc-55.so)
#7.
add_library(
swresample
SHARED
IMPORTED
)
set_target_properties(swresample PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libswresample-3.so)
#8.
add_library(
swscale
SHARED
IMPORTED
)
set_target_properties(swscale PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libswscale-5.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
)
8.配置build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.hh.ffmpegdemo2"
minSdkVersion 17
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
abiFilters 'armeabi-v7a' //指定cpu的架構
}
}
ndk {
abiFilters "armeabi-v7a"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt" //指定CMakeLists.txt的路徑,這里表示是在app目錄下
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.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

9.在app->src->main下面新建一個native-lib.cpp(這個檔案和CMakeLists.txt那個要一致)的c++檔案,然后點擊build->Refresh Linked C++ Projects

10.native-lib.cpp添加測驗代碼,其實com_hh_ffmpegdemo2_JNIUtils是你要要呼叫的類的路徑和類名
#include <jni.h> #include <string> extern "C" { #include "include/libavcodec/avcodec.h" } extern "C" JNIEXPORT jstring JNICALL Java_com_hh_ffmpegdemo2_JNIUtils_stringFromJNI( JNIEnv *env, jobject) { std::string hello = "Hello from C++"; return env->NewStringUTF(avcodec_configuration()); }

11.接下來進行編譯,編譯完之后在下圖路徑得到libnative-lib.so庫


12.把so庫放在我們之前建好的jniLibs目錄下,在JNIUtils.java里面引入


13.呼叫方法運行


轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/277361.html
標籤:其他
上一篇:1.1.2Andorid Automotive 鏡像刷寫流程
下一篇:利用DEV C++除錯程式

