Issue
I have three projects using the Android NDK. The first two build static libraries (one includes the other). I'm trying to include them as part of the build for the third but get various errors depending on how I set up my app in android studio 2.3.
I've read the other questions on this but they all refer to building the libraries as part of the same makefile. I need to be able to build the two as parts of different makes and reference the inner lib from relative paths.
Here's the most basic example I could create.
https://github.com/ginger-mcmurray/android.mk-inner-library-sample
Building the inner library works fine. Building the wrapper library also works, which I can tell by running nm on the .a files created.
PLNA007982-MBP:arm64-v8a ginger.mcmurray$ nm libInnerLibrary.a
inner-library.o:
0000000000000000 n $d.1
0000000000000000 n $d.2
0000000000000000 n $d.3
0000000000000000 n $d.4
0000000000000000 n $d.5
0000000000000000 n $d.6
0000000000000000 r $d.7
0000000000000000 n $d.8
0000000000000000 t $x.0
0000000000000000 T innerLibraryMethod
PLNA007982-MBP:arm64-v8a ginger.mcmurray$ nm libWrapperLibrary.a
wrapper-library.o:
0000000000000000 n $d.1
0000000000000000 n $d.2
0000000000000000 n $d.3
0000000000000000 n $d.4
0000000000000000 n $d.5
0000000000000000 n $d.6
0000000000000000 r $d.7
0000000000000000 n $d.8
0000000000000000 t $x.0
U innerLibraryMethod
0000000000000000 T wrapperLibraryMethod
I've followed the recommendations in all of the answers I can find on here and none of them work. The current version on github does not build. The error is
/Users/ginger.mcmurray/Jay-s-POC/ndk-test/hello-jni/app/src/main/cpp/src/hello-jni.c
Error:(34) undefined reference to 'wrapperLibraryMethod'
wrapperLibraryMethod is defined in libWrapperLibrary.a but not being found by the build.
Solution
Make sure you have both libWrapperLibrary.a and libInnererLibrary.a built. You need copies of both libraries somewhere (e.g. in jniLibs). The following CMakeLists.txt picks the inner-lib from its natural location, and saves me the hassle of copying.
You must add both libraries separately, and specify them explicitly in target_link_libraries
:
cmake_minimum_required(VERSION 3.4.1)
add_library( wrapper-lib
STATIC
IMPORTED )
add_library( inner-lib
STATIC
IMPORTED )
set_target_properties( # Specifies the target library.
wrapper-lib
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
${CMAKE_CURRENT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libWrapperLibrary.a )
set_target_properties( # Specifies the target library.
inner-lib
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
${CMAKE_CURRENT_SOURCE_DIR}/../../../../../inner-library/obj/local/${ANDROID_ABI}/libInnerLibrary.a )
add_library(hello-jni SHARED
src/hello-jni.c)
# Include libraries needed for hello-jni lib
target_link_libraries(hello-jni
wrapper-lib inner-lib
android
log)
- The order of libraries in the
target_link_libraries
may matter. To be on the safe side, I put inner after wrapper. - There is no easy way to import inner-lib objects into the wrapper-lib, but
ndk-build
allows to specify that using one static library (including a prebuilt one) requires also another one. This is irrelevant for your scenario, because there is no way to translate this information from ndk-buildish to CMakeish. - I understand that you prefer to use
ndk-build
to build them separately, just as an exercise, and in real life they will arrive prebuilt from an external source. But if you build at least the wrapper-lib yourself, it could save you a lot of headache if you usendk-build
also for the resulting shared library. Android Studio integrates withndk-build
not worse than withCMake
, but it is insane to mix the two systems with gradle.
Answered By - Alex Cohn
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.