最近在学习 cmake 就是在安卓中 , 麻烦的要死 , 看了很多的教程 , 发现没有 多少说对打印位置在哪里 , 先说一下版本信息 , 可能你们也不一样
gradle 配置
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.learn.test"
minSdkVersion 25
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ''
abiFilters 'arm64-v8a' , 'x86'
}
}
ndk{
// 打包生成的 APK 文件指挥包含 ARM 指令集的动态库
abiFilters 'arm64-v8a' , 'x86'//不同平台so/*, "arm64-v8a", "x86", "x86_64"*/
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.22.1'
}
}
}
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'
}
重点 CMakeLists.txt 配置
cmake_minimum_required(VERSION 3.22.1)
# 获取 Android 项目的根目录
set(ANDROID_PROJECT_ROOT ${CMAKE_SOURCE_DIR})
# 使用根目录构建路径
set(ANDROID_LIB_DIR ${ANDROID_PROJECT_ROOT}/app/src/main/jniLibs/${ANDROID_ABI})
# 输出路径
message("Android Project Root: ${ANDROID_PROJECT_ROOT}")
message("Android Lib Directory: ${ANDROID_LIB_DIR}")
# 设置 Android NDK 的路径
set(ANDROID_NDK /DevelopmentTool/Sdk/ndk/21.0.6113669)
这个就是一个简单打印 问题是打印内容在哪里? 找了很久终于找到位置了
app/build/intermediates/cxx/Debug/c1z1m6iy/meta/x86/metadata_generation_stderr.txt
你就说这玩意儿有多垃圾 ,
metadata_generation_stderr.txt 名字居然是这个
CMake Warning (dev) in CMakeLists.txt:
No project() command is present. The top-level CMakeLists.txt file must
contain a literal, direct call to the project() command. Add a line of
code such as
project(ProjectName)
near the top of the file, but after cmake_minimum_required().
CMake is pretending there is a "project(Project)" command on the first
line.
This warning is for project developers. Use -Wno-dev to suppress it.
Android Project Root: D:/Androidstudioproject/MyApplication2/app/src/main/cpp
Android Lib Directory: D:/Androidstudioproject/MyApplication2/app/src/main/cpp/app/src/main/jniLibs/x86
CMake Warning:
Manually-specified variables were not used by the project:
CMAKE_EXPORT_COMPILE_COMMANDS
CMAKE_LIBRARY_OUTPUT_DIRECTORY
CMAKE_RUNTIME_OUTPUT_DIRECTORY
这里面就是 message 输出的内容 , 真的很垃圾
目前并不知道拿到路径方式 只能采用这种方式 , 你们可以在网上打印
# 获取 Android 项目的根目录(相对于 CMakeLists.txt 所在目录)
get_filename_component(ANDROID_PROJECT_ROOT ${CMAKE_CURRENT_LIST_DIR}/../../.. REALPATH)
# 构建相对路径
set(RELATIVE_PATH src/main/java/com/learn/test)
# 构建完整路径
set(JAVA_SOURCE_DIR ${ANDROID_PROJECT_ROOT}/${RELATIVE_PATH})
# 在这里可以使用 JAVA_SOURCE_DIR 变量来设置路径
message("Java Source Directory: ${JAVA_SOURCE_DIR}")