I've already seen this question whose answers are way too broad for my case. Clearly this is a Cmakelists.txt only related problem.
I'm trying to integrate a dynamic (.so) native library with an existing Android Application with JNI support.
So basically, this project already includes a shared native library that would use other shared native libraries. In my case it is called libcardios and it will be invoked from native-lib.cpp.
I guess I have a CMakeLists.txt issue here because the linker command simply does not include the reference to my shared library:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("myhelloworldapplication")
# 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.
# the file list must be regenerated each time the repo is updated from bitbucket
# You can use a shell command like this one:
# find . -name '*.cpp' > source_code_list.txt
# then remove the portion of the path you do not want
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
)
# The folder list may have changed /!\
# Make sure to have it in sync with the repo
target_include_directories(native-lib PUBLIC
libcardios/includes
)
# 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 )
find_library( # Sets the name of the path variable.
android-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
android )
add_library(libcardios_lib SHARED IMPORTED)
set_property(TARGET libcardios_lib PROPERTY IMPORTED_LOCATION "libcardios/${CMAKE_ANDROID_ARCH_ABI}/libcardios.so")
# 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}
${android-lib}
${libcardios_lib}
)
Here is the relevant part of the build.gradle file:
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.example.myhelloworldapplication"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-v -std=c++11 -fexceptions"
}
}
ndk {
abiFilters "armeabi-v7a"
}
}
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"
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
And finally the verbose output of the linker command which has no trace of the libcardios library.
"/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld" --sysroot=/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot -z noexecstack -EL --warn-shared-textrel -z now -z relro -X --hash-style=both --enable-new-dtags --eh-frame-hdr -m armelf_linux_eabi -shared -o /Users/omatrot/Documents/sensoria_analyics/simpleandroidappwithlibcardios/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so /Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/arm-linux-androideabi/16/crtbegin_so.o -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/lib64/clang/9.0.8/lib/linux/arm -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/armv7-a/thumb -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/../lib/armv7-a/thumb -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/arm-linux-androideabi/16 -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/arm-linux-androideabi -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/../lib -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/arm-linux-androideabi/../../lib -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/lib/armv7-a/thumb -L/Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib --exclude-libs libgcc.a --exclude-libs libgcc_real.a --exclude-libs libatomic.a --build-id --fatal-warnings --exclude-libs libunwind.a --no-undefined -soname libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o -llog -landroid -latomic -lm -Bstatic -lc++ -Bdynamic -lm -lgcc -ldl -lc -lgcc -ldl /Users/omatrot/Library/Developer/Xamarin/android-sdk-macosx/ndk/21.1.6352462/toolchains/llvm/prebuilt/darwin-x86_64/sysroot/usr/lib/arm-linux-androideabi/16/crtend_so.o
/Users/omatrot/Documents/sensoria_analyics/simpleandroidappwithlibcardios/app/src/main/cpp/native-lib.cpp:16: error: undefined reference to 'CardioS::init()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
Where could be the problem located? Is this an incorrect ordering of the instructions in CMakeLists.txt? It seems to me that ${libcardios_lib} is simply empty...
Any help appreciated.