1、Android Studio 新建c++工程
image.png
image.png
image.png
2、为了方便编写CMakeLists.txt脚本的文件路径,把CMakeLists.txt从cpp目录下移动到app目录下,同时修改build.gradle(:app)中cmake 路径为:
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.10.2'
}
}
改为
externalNativeBuild {
cmake {
path file('CMakeLists.txt')
version '3.10.2'
}
}
3、在app/src/main下心建文件目录jniLibs/armeabi-v7a,abi目录根据你编译的so而定。我编译的是armeabi-v7a。编译ffmpeg 复制编译好的so到此目录下:
4、复制头文件到cpp目录下:
image.png
5、编译...
报如下错误
Execution failed for task ':app:buildCMakeDebug'.
> Build command failed.
Error while executing process /Users/springtom/Library/Android/sdk/cmake/3.10.2.4988404/bin/ninja with arguments {-C /Users/springtom/Documents/source/MeiKTVPlayer/app/.cxx/Debug/21a2r4x6/arm64-v8a meiplayer}
ninja: Entering directory `/Users/springtom/Documents/source/MeiKTVPlayer/app/.cxx/Debug/21a2r4x6/arm64-v8a'
解决:
在app build的android下添加
ndk{
abiFilters "armeabi-v7a"
}
6、再编译...
报如下错误
2 files found with path 'lib/armeabi-v7a/libavcodec.so' from inputs:
- /Users/springtom/Documents/source/MeiPlayer/app/build/intermediates/merged_jni_libs/debug/out/armeabi-v7a/libavcodec.so
- /Users/springtom/Documents/source/MeiPlayer/app/build/intermediates/cxx/Debug/2r1c3m1y/obj/armeabi-v7a/libavcodec.so
If you are using jniLibs and CMake IMPORTED targets, see
https://developer.android.com/r/tools/jniLibs-vs-imported-targets
解决:这个原因是由于gralde版本问题导致的,这个问题困扰我不少时间,最开始以为是编译的so有问题,走了弯路。
我的gradle版本:
build.gradle(MeiKTVPlayer)
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
gradle-wapper.properties
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-bin.zip
改为:
dependencies {
classpath 'com.android.tools.build:gradle:3.6.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip
7、再编译...
报错
Settings file '/Users/springtom/Documents/source/MeiKTVPlayer/settings.gradle' line: 1
A problem occurred evaluating settings 'MeiKTVPlayer'.
> Could not find method dependencyResolutionManagement() for arguments [settings_8ngoy7e4c0dkpao131h5v8fyd$_run_closure1@7db6a8d2] on settings 'MeiKTVPlayer' of type org.gradle.initialization.DefaultSettings.
原因 由于高版本的gradle 依赖管理放到了setttings.gradle,版本已经改低,依赖管理也应该改回到原来的build.gradle中。
去掉settings.gradle中的依赖管理
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
在build.gradle(MeiKTVPlayer)中添加
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
8、再编译...
报错:
Build file '/Users/springtom/Documents/source/MeiKTVPlayer/app/build.gradle' line: 6
A problem occurred evaluating project ':app'.
> Could not find method compileSdk() for arguments [32] on object of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.
原因:还是降低gradle版本所致
解决:
build.gralde(:app)下
android {
compileSdk 32
defaultConfig {
applicationId "com.aimei.meiktvplayer"
minSdk 21
targetSdk 32
改为:
android {
compileSdkVersion 32
defaultConfig {
applicationId "com.aimei.meiktvplayer"
minSdkVersion 21
targetSdkVersion 32
9、再编译...
报错:
Build file '/Users/springtom/Documents/source/MeiKTVPlayer/app/build.gradle' line: 42
A problem occurred evaluating project ':app'.
> Could not find method buildFeatures() for arguments [build_53peu55ukn4y7po6cm8ujhgkj$_run_closure1$_closure7@182b0cc] on object of type com.android.build.gradle.internal.dsl.BaseAppModuleExtension.
原因 viewbing也需要改成老版本
buildFeatures {
viewBinding true
}
改为:
viewBinding {
enabled = true
}
到此就能编译成功
10、把播放器迁移到(略)
网友评论