link dependent android ndk cmake project to main android ndk library project - android

I have 2 modules in this project: app and libdatacpp
libdatacpp is mainly a C++ project but uses some java code
app is mainly a java project, but uses some c++ code
When building the project, I get c++ linker errors for undefined functions from libdatacpp, because the app project doesn't link libDataCpp.o
In my cmake for app, I am linking against libDataCpp like this:
target_link_libraries(
native-lib
DataCpp)
I also link against the other project in the app module's build.gradle like this:
dependencies {
api project(':libdatacpp')
How can I get the app module to automatically link against my libdatacpp module?

If you intend to provide libdatacpp native library for other AndroidStudio projects through .aar, then you need to use prefab feature.
But if these native library is part of your own project, then I don't see the need to put it in the .aar, just put its stuff (sources and CMakeLists.txt) in a subfolder and add it to build by using add_subdirectory (datacpp) in your main CMake.

Related

Android AAR native library with header only prefab

I've been trying to create a Android AAR native library with header only prefab without success. We distribute other native libraries we build in Android Studio as prefabs in AARs and include them in other Android native applications. We'd like to do the same with a header-only library. We use Android Studio and cmake for the native part.
The docs clearly states:
Each dependency can expose at most one Prefab package, which comprises one or more modules. A Prefab module is a single library, which could be either a shared, static, or header only library.
One obvious workaround would be to build a dummy library and include it in the AAR so that prefabPublishing would work (it breaks when there no library file).
Another would perhaps be to add to the AAR (zip) a manually created prefab directory but I read somewhere that this could be a problem with signing.
Yet another would perhaps be to create the perfab directory structure manually, which would be ok, and included in verbatim in the AAR (if that would work?).
We use Android Studio 4.1.1 and gradle plugin 4.1 and create our AARs as described in the docs referenced below.
buildFeatures {
prefabPublishing true
}
prefab {
mylibrary {
headers "src/main/cpp/include"
}
}
https://developer.android.com/studio/build/native-dependencies?buildsystem=cmake
https://google.github.io/prefab/
Thanks,
/Johan
Since Android Gradle Plugin 7.1 you can define headerOnly true.

Android Studio is bundling stubbed library into the APK

I am using Android Studio to build a native C++ project. There is a dependency library (let's call it lib.so) which is required to be linked with the JNI library to resolve few of its symbols. However, I do not want this library to be bundled into the APK as this is a stubbed library and to be used only for the linking purpose. A proper lib.so with proper symbol definitions is already present on the Android device where I want to run the built APK.
I am importing the stubbed library lib.so to be linked with JNI library as:
add_library(lib SHARED IMPORTED)
set_target_properties(lib.so PROPERTIES IMPORTED_LOCATION "location_of_lib.so")
add_dependencies(native-jni lib})
target_link_libraries(native-jni lib)
This way the APK compilation is successful but the library is getting bundled into the APK.
I am using AS 4.0.1, NDK r19c, CMake 3.17. The same project used to work as expected, i.e. not bundle the library lib.so with the APK but somehow it started bundling it.
Any leads to debug this issue would be appreciated.
Try to use find_library for that. Put your prebuilt lib.so (bad name at least liba.so) to some path and add it to CMAKE_FIND_ROOT_PATH:
list(APPEND CMAKE_FIND_ROOT_PATH ${PREBUILT_LIBS_DIR}/${ANDROID_PLATFORM}/${ANDROID_ABI}/a)
example how it could look:
~/my-proj/prebuilt-libs/android-28/x86/a/lib/liba.so
Then find library and link it:
find_library(lib-a a)
target_link_libraries(native-jni lib-a)

Android build c++ with dependency

I am working on an Android application with NDK. My c++ code has some dependencies from c++ open source libraries, so I can download the source code of the dependencies and package them in my app. I have created my CMakeList files, but I don't know how to include the external library.
I would like to build the dependency to be able to use it in my own project, so the flow would be as follows:
cd external/library/path
./configure
make
build my app and link the built dependency
Is this possible? If so, how can this be done? Sorry if this is a dumb question, I'm new on C++ and Cmake.
The way I solved this was by cross compiling the libraries and adding them as static libraries. You can cross compile using the clang binaries included in the ndk directory, using the one that targets your app min sdk.

Equivalent of 'compileOnly' in Android.mk

I have a pre-built java library as a compile time dependency for Android library project(AAR). So while building it in Android Studio, 'compileOnly' is used so that the same gets linked in runtime when deployed with an APK.
However, I should also write an equivalent Android.mk for the library project. I am not able to find any reference to include a prebuilt java library in Android.mk. Can someone help me on this part.
I tried using LOCAL_PREBUILT_JAVA_LIBRARIES attribute, but the system threw an error:
error: dont use LOCAL_PREBUILT_JAVA_LIBRARIES anymore LOCAL_PATH=xxx.
This kind of dependency linking may look strange. Let me give some insight into it also. Basically I am building an application that has one small part being developed by a third party. The setter interface APIs for the third party are shipped in by me in the form of a .jar file so that they use the same as a compile time dependency and build an AAR out of it.
Now the third party project is to be included in my project build(AOSP). This brings a dependency that their module should be compiled for the AAR to get generated and my project uses that AAR to generate the APK.
For the AAR to get compiled and built, I need to link the prebuilt java interface library that is shipped in by me(mentioned in the first step).
Need an equivalent of 'compileOnly' used in build.gradle.

Android unittest googletest

I use Android Studio to build simple application with c++ support. Now I want to write unittest for functions in my native lib. How can I include google test in my CMakeLists.txt to build and run my tests on Android device?
When I put
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
add_executable(foo foo.cc)
target_link_libraries(foo ${GTEST_BOTH_LIBRARIES})
add_test(AllTestsInFoo foo)
in CMakeLists.txt I've go an error that GTest is not found.
I saw this and this questions. And the documentation, but they all are about .mk files not about CMakeLists.txt
How should I find googletest library on android device and link my test project with it?
Maybe i should write my own CMakeLists.txt for sourses located in
${ANDROID_NDK}/sources/third_party/googletest
to apply paches and build this library by myself?

Categories

Resources