app\src\main\cpp\CMakeLists.txt:C/C++调试|arm64-v8a:配置失败
app\src\main\cpp\CMakeLists.txt : C/C++ debug|arm64-v8a : Configuration failed
我在jnilibs
文件夹下复制了必要的.so 文件,并使用jni 接口在android ndk 项目中配置了以下文件。当 运行 项目时,它抛出如下所示的错误。
build.gradle
defaultConfig{
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
abiFilters 'arm64-v8a'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
这是我的CMakeLists.txt
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
cmake_minimum_required(VERSION 3.4.1)
# Sets the minimum version of CMake required to build the native library.
#Import header file
include_directories(src/main/jniLibs/include)
#Declare the import file to change the directory variable ARM_DIR, which uses the relative directory with the system, because using the relative path does not seem to work
set(ARM_DIR C:/Users/Username/AndroidStudioProjectsFolder/ExamplAppName/app/src/main/jniLibs)
#Add an example of so library.
add_library(avdevice
SHARED
IMPORTED)
set_target_properties(avdevice
PROPERTIES IMPORTED_LOCATION
${ARM_DIR}/arm64-v8a/libavdevice.so)
#Adding other so libraries in the same format as above
#Link library
target_link_libraries(
native-lib
avdevice
avformat
${log-lib} )
# 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.
# native-lib
#
# # Sets the library as a shared library.
# SHARED
#
# # Provides a relative path to your source file(s).
# native-lib.cpp )
#
## 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.
# native-lib
#
# # Links the target library to the log library
# # included in the NDK.
# ${log-lib} )
它的原生-lib.cpp文件在cpp/
目录下:
#include <jni.h>
#include <string>
extern "C"
{
#include "libavformat/avformat.h"
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_examples_examplappname_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
av_register_all();
return env->NewStringUTF(hello.c_str());
}
.java
源代码:
static {
System.loadLibrary("avdevice");
System.loadLibrary("avformat");
System.loadLibrary("native-lib");
}
TextView tv = findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
错误:
**Starting Gradle Daemon...
Gradle Daemon started in 4 s 270 ms
KotlinDslScriptsParameter(correlationId=22803889351800, scriptFiles=[]) => StandardKotlinDslScriptsModel(scripts=[], commonModel=CommonKotlinDslScriptModel(classPath=[], sourcePath=[], implicitImports=[]), dehydratedScriptModels={}) - took 0.128 secs
app\src\main\cpp\CMakeLists.txt : C/C++ debug|arm64-v8a : Configuration failed.
app\src\main\cpp\CMakeLists.txt : C/C++ debug|arm64-v8a : CMake Error at app\src\main\cpp\CMakeLists.txt:21 (target_link_libraries):
Cannot specify link libraries for target "native-lib" which is not built by
this project
app\src\main\cpp\CMakeLists.txt : C/C++ debug|arm64-v8a : Configuration failed.
executing external native build for cmake
app\src\main\cpp\CMakeLists.txt
CONFIGURE SUCCESSFUL in 3m 33s**
此处明显的问题涉及 CMake 文件中的行:
target_link_libraries(
native-lib
avdevice
avformat
${log-lib} )
它引用库(native-lib
目标和 ${log-lib}
库变量)甚至在它们被定义之前。这肯定会导致 CMake 报错。您应该取消注释 CMake 文件的其余部分,并且 link 您导入的库目标为 existing target_link_libraries()
调用中的 native-lib
:
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
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 )
target_link_libraries( # Specifies the target library.
native-lib
# Link your imported library targets here also.
avdevice
avformat
# Links the target library to the log library
# included in the NDK.
${log-lib} )
我在jnilibs
文件夹下复制了必要的.so 文件,并使用jni 接口在android ndk 项目中配置了以下文件。当 运行 项目时,它抛出如下所示的错误。
build.gradle
defaultConfig{
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
abiFilters 'arm64-v8a'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
这是我的CMakeLists.txt
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
cmake_minimum_required(VERSION 3.4.1)
# Sets the minimum version of CMake required to build the native library.
#Import header file
include_directories(src/main/jniLibs/include)
#Declare the import file to change the directory variable ARM_DIR, which uses the relative directory with the system, because using the relative path does not seem to work
set(ARM_DIR C:/Users/Username/AndroidStudioProjectsFolder/ExamplAppName/app/src/main/jniLibs)
#Add an example of so library.
add_library(avdevice
SHARED
IMPORTED)
set_target_properties(avdevice
PROPERTIES IMPORTED_LOCATION
${ARM_DIR}/arm64-v8a/libavdevice.so)
#Adding other so libraries in the same format as above
#Link library
target_link_libraries(
native-lib
avdevice
avformat
${log-lib} )
# 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.
# native-lib
#
# # Sets the library as a shared library.
# SHARED
#
# # Provides a relative path to your source file(s).
# native-lib.cpp )
#
## 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.
# native-lib
#
# # Links the target library to the log library
# # included in the NDK.
# ${log-lib} )
它的原生-lib.cpp文件在cpp/
目录下:
#include <jni.h>
#include <string>
extern "C"
{
#include "libavformat/avformat.h"
}
extern "C" JNIEXPORT jstring JNICALL
Java_com_examples_examplappname_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++";
av_register_all();
return env->NewStringUTF(hello.c_str());
}
.java
源代码:
static {
System.loadLibrary("avdevice");
System.loadLibrary("avformat");
System.loadLibrary("native-lib");
}
TextView tv = findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
错误:
**Starting Gradle Daemon...
Gradle Daemon started in 4 s 270 ms
KotlinDslScriptsParameter(correlationId=22803889351800, scriptFiles=[]) => StandardKotlinDslScriptsModel(scripts=[], commonModel=CommonKotlinDslScriptModel(classPath=[], sourcePath=[], implicitImports=[]), dehydratedScriptModels={}) - took 0.128 secs
app\src\main\cpp\CMakeLists.txt : C/C++ debug|arm64-v8a : Configuration failed.
app\src\main\cpp\CMakeLists.txt : C/C++ debug|arm64-v8a : CMake Error at app\src\main\cpp\CMakeLists.txt:21 (target_link_libraries):
Cannot specify link libraries for target "native-lib" which is not built by
this project
app\src\main\cpp\CMakeLists.txt : C/C++ debug|arm64-v8a : Configuration failed.
executing external native build for cmake
app\src\main\cpp\CMakeLists.txt
CONFIGURE SUCCESSFUL in 3m 33s**
此处明显的问题涉及 CMake 文件中的行:
target_link_libraries(
native-lib
avdevice
avformat
${log-lib} )
它引用库(native-lib
目标和 ${log-lib}
库变量)甚至在它们被定义之前。这肯定会导致 CMake 报错。您应该取消注释 CMake 文件的其余部分,并且 link 您导入的库目标为 existing target_link_libraries()
调用中的 native-lib
:
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp )
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 )
target_link_libraries( # Specifies the target library.
native-lib
# Link your imported library targets here also.
avdevice
avformat
# Links the target library to the log library
# included in the NDK.
${log-lib} )