Issue
I copied the necessary .so files under the jnilibs
folder, and configured the below files in an android ndk project using jni interface. While running the project, it throws an error as seen below.
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"
}
}
This my 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} )
Its native-lib.cpp file under the cpp/
directory:
#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
source code:
static {
System.loadLibrary("avdevice");
System.loadLibrary("avformat");
System.loadLibrary("native-lib");
}
TextView tv = findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
Error:
**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**
Solution
The apparent issue here concerns the line in CMake file:
target_link_libraries(
native-lib
avdevice
avformat
${log-lib} )
It references libraries (the native-lib
target and ${log-lib}
library variable) before they have even been defined. This will certainly cause CMake to issue an error. You should uncomment the remainder of your CMake file, and link your imported library targets to native-lib
in the existing target_link_libraries()
call:
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} )
Answered By - Kevin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.