Imported library doesn't build in Android Studio 2.2 - android

I try to add a imported library to my project, and link it to another Library.
Here is some part of my code from CMakeLists.txt:
add_library(native-lib SHARED ${sources})
add_library(imported-lib SHARED IMPORTED)
set_target_properties(imported-lib PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_LIST_DIR}/PathToLibs/${ANDROID_ABI}/imported-lib.so)
In my native-lib, I create an object of a Class which is placed in imported-lib but the app starts crash. (if I debug or run the app then make project works fine)
In the Gradle Console i get an Info: [org.gradle.api.Task] externalNativeBuildDebug: not building target imported-lib because there was no build command for it
How can I build it?

The shared lib currently need to packed into apk manually, one way is to route jniLibs to your shared lib directory. Hope later version could pack it automatically. One example is here,https://github.com/googlesamples/android-ndk/tree/master/hello-libs, it imports one shared lib, one static lib; shared lib is packed to APK with script in app/build.gradle:jniLibs.srcDirs = ['../distribution/gperf/lib']

As a workaround for me is so in the build.gradle. The 'make' must be in PATH.
externalNativeBuild {
cmake {
...
arguments "-GAndroid Gradle - Unix Makefiles",
"-DCMAKE_MAKE_PROGRAM=make"
}
}

Related

CMake imported library not included in Gradle aar output

In an Android project that loads a C library (mylib.so, which includes a JNI API already), I have a CMakeLists.txt that import the prebuilt library, as per the documentation, like so:
cmake_minimum_required(VERSION 3.10.2)
project(ImportMyLib)
set(MYLIB_PREBUILT_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/build/arm64/src")
add_library(mylib SHARED IMPORTED)
set_target_properties(mylib
PROPERTIES IMPORTED_LOCATION
"${MYLIB_PREBUILT_PATH}/libmylib.so")
I did print ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/build/arm64/src and checked that ${CMAKE_CURRENT_SOURCE_DIR}/../../../../src/build/arm64/src/libmylib.so does exist.
However, libmylib.so is not included in the resulting .aar, and I don't understand why.
My build.gradle does include the CMakeLists.txt above, like so:
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
This is similar to this question, except that it seems there that there was a need to add an IMPORTED target (because of the ExternalProject_Add), whereas here I'm trying to import an already built library.
What am I missing?
You can explicitly request libmylib.so to be included:
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
targets "mylib" # matches add_library(mylib SHARED IMPORTED)
}
}
I had missed this part of the documentation:
If you want Gradle to package prebuilt native libraries that are not used in any external native build, add them to the src/main/jniLibs/ABI directory of your module.
Versions of the Android Gradle Plugin prior to 4.0 required including CMake IMPORTED targets in your jniLibs directory for them to be included in your app.
[...]
If you are using Android Gradle Plugin 4.0, move any libraries that are used by IMPORTED CMake targets out of your jniLibs directory to avoid this error.
Moving mylib.so to src/main/jniLibs/ABI does work indeed.

Missing opencv core .so library in .aar

I'm currently trying to compile OpenCV using Gradle and integrate it within my Android project, but I'm facing an issue and I don't know how to resolve it.
I have created a native library module within my project and created a few gradle tasks for downloading and unzipping the source code within the project. Then, using the CMakeLists.txt file provided in the OpenCV project, I import it using "add_subdirectory" within my application CMakeList.txt file.
With this, the OpenCV compilation complete correctly and generates the two required libraries, libopencv_core.so and libopencv_imgproc.so in the .cxx/RELEASE/buildHash/arm64-v8a/opencv/lib/arm64-v8a/ folder
However, only my own application .so library and the libopencv_imgproc.so are included in the final .aar file. The libopencv_core.so is missing. If I check in the Gradle build folder, located in build/intermediates/cxx/RELEASE/buildHash/obj/arm64-v8a/, I can see the same issue as in the .aar.
The funny thing is, if I only require to use the libopencv_core.so, it will be integrated within the final .aar correctly, and I don't understand why.
Here is my CMakeList.txt file:
cmake_minimum_required(VERSION 3.18.1)
project("myapp")
add_library(
myapp
SHARED
myapp.cpp )
find_library(
log-lib
log )
# OpenCV source code and CMakeList.txt are located in the opencv folder
add_subdirectory(opencv)
# Library opencv_core and opencv_imgproc are generated by the OpenCV CMakeList.txt
target_link_libraries(myapp opencv_core opencv_imgproc ${log-lib} )
target_include_directories(myapp PUBLIC
opencv/modules/core/include
opencv/modules/imgproc/include
)
include_directories (${CMAKE_BINARY_DIR})
My question is, what am I missing ? Is there something I must specify to force the inclusion of the libopencv_core.so ?
PS: Why I'm not simply integrating the binaries instead of compiling them ? Because the app is targeted for F-Droid, which requires to whole code to be compiled from the sources, so this is not an option for me.
I have finally managed to copy the libopencv_core.so in the correct folder and generate a complete aar.
I just had to add the following to my application CMakeLists.txt:
set_target_properties( opencv_core PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY} )
I don't really get why I must add this line, but it's working !

Adding an updated shared native library (.so file) to an Android Studio app leads to an UnsatisfiedLinkError

I've an android app written in Android Studio 3.0.1 and with compileSdkVersion 26. This app depends on a module written in native code (c++). The native code in the module depends on a third party shared library (.so) file. In the Cmake file this third party library is included using the standard method, e.g:
add_library( my-module-lib
SHARED
src/main/cpp/file1.cpp
src/main/cpp/file2.cpp )
add_library( libthird_party_library SHARED IMPORTED )
set_target_properties( libthird_party_library PROPERTIES IMPORTED_LOCATION ${pathToProject}/src/main/jniLibs/${ANDROID_ABI}/libthird_party_library.so)
target_link_libraries( my-module-lib libthird_party_library )
My module is then added as dependency to the main app and the native code is loaded using:
System.loadLibrary("my-module-lib");
This all works fine, however I need to update the third party library. I thought this was just as simple as replacing the old .so files for the new ones. But this leads to an UnsatisfiedLinkError:
java.lang.UnsatisfiedLinkError: dlopen failed: library "libthird_party_library.so.2" not found
at java.lang.Runtime.loadLibrary0(Runtime.java:989)
at java.lang.System.loadLibrary(System.java:1567)
It should be noted that android now searches for a ".so.2" file instead of a ".so" file. So I would think that for some reason the old one is still included somewhere, and because of name collision the new one is renamed to "*.2". So I tried these things to get rid of it:
1) [not working] Delete everything, recompile everything
Just deleting everything, that is:
the .externalNativeBuild folder for my module
the build folder for my module
the build folder for my app
And rebuild everything, that is:
Build > Clean Project
Build > Make Module "my module"
Build > Refresh Linked C++ Projects
Build > Rebuild Project
But this still leads to the same UnsatisfiedLinkError
2) [not working] Changing the name of the .so file
Changing the name of the updated library to "libthird_party_library_NEW.so" and changing the cmake file:
set_target_properties( libthird_party_library PROPERTIES IMPORTED_LOCATION ${pathToProject}/src/main/jniLibs/${ANDROID_ABI}/libthird_party_library_NEW.so)
Same error
3) [working, not a good solution] Rename the old ".so" file and include new one
One "solution" which does work is renaming the old ".so" files using refactor > rename to something like "libthird_party_library_OLD.so" and then copying the new third party libraries using the standard name "libthird_party_library.so". But this is of course not a very nice solution, because the old non needed library will be included in the apk.
It turned out to be a problem with linking a versioned shared library in android studio. Similar as in this question and as described here.
The problem is that the internal version number of "libthird_party_library.so" is actually "libthird_party_library.so.2". This can be found by running objdump under linux on the .so file, that is:
objdump -p libthird_party_library.so | grep so
which outputs:
libthird_party_library.so: file format elf64-little
NEEDED libm.so
NEEDED libc.so
NEEDED libdl.so
SONAME libthird_party_library.so.2
required from libdl.so:
required from libm.so:
required from libc.so:
Since the file name as seen by android is "libthird_party_library.so" and android tries to load "libthird_party_library.so.2" it can obviously not find the required library.
Solution
An obvious solution would be to change the filename to "libthird_party_library.so.2", but this doesn't work because android studio only includes libraries ending on .so in the apk (aaargh).
The solution is to change the internal version number to "libthird_party_library_2.so", renaming the file to "libthird_party_library_2.so" and changing the Cmake file to reflect this change.
1) Changing the internal version number can be done under linux with:
rpl -R -e libthird_party_library.so.2 libthird_party_library_2.so libthird_party_library.so
Where the first argument is the internal version number, the second the one we need to change it to and the third the filename. MAKE SURE THE LENGTH OF THE OLD INTERNAL VERSION NUMBER IS THE SAME AS THE NEW ONE!!
2) Change the filename to "libthird_party_library_2.so" using your favorite tool
3) Change this line in the CMake file to
set_target_properties( libthird_party_library PROPERTIES IMPORTED_LOCATION ${pathToProject}/src/main/jniLibs/${ANDROID_ABI}/libthird_party_library_2.so)
Hope this helps!

Android NDK including a 3rd party prebuilt shared library with Gradle and CMake

I'm struggling to include a prebuilt shared library in my android project
The library in question is libusb, which the NDK part of my android project requires.
Everything is compiling and linking OK, i.e. the project is building successfully, but on installing the apk on my device the app is crashing.
The relevant error msg from monitor is:
java.lang.UnsatisfiedLinkError: dlopen failed: library "libusb1.0.so" not found
what i've tried so far is adding the following to my app/build.gradle:
sourceSets{
main {
// let gradle pack the shared library into apk
jniLibs.srcDirs = '/home/me/third-party/libusb-1.0.21/android/libs/'
}
in CMakeLists.txt i've added:
set(libusb_DIR $ENV{HOME}/third-party/libusb-1.0.21/android/libs)
set(libusb_LIB usb1.0)
link_directories( ${libusb_DIR}/${ANDROID_ABI}/ )
target_link_libraries( ${libusb_LIB} )
I've even tried creating a app/src/main/jniLibs dir and manually copying the armeabi-v7a version of the shared lib, libusb1.0.so, in there.
Still getting same error message in Monitor after the apk has been installed..
Give a try to this one, instead of taking the path from env you should try ${CMAKE_SOURCE_DIR}
set(usb_DIR ${CMAKE_SOURCE_DIR}/../../../../libs)
add_library(libusb SHARED IMPORTED)
set_target_properties(libusb PROPERTIES IMPORTED_LOCATION
${usb_DIR}/libs/${ANDROID_ABI}/libusb1.0.so)
target_link_libraries(${libusb})

C/C++ with Android Studio version 2.2

With Android Studio 2.2, and the new C++ support they added; can I now write and compile inside android studio, or do I need to compile and import my libraries separately
Short answer: Yes, you can.
Here is what you can do 1
1) In Android Studio, right click on your module ==> New ==> Package
2) name the package (folder) cpp (or you can name it jni)
3) you will see the cpp directory on the left.
4) You can create .cpp, .h and other files within that folder.
Nowm you have to tell gradle how to build that.
You need install CMake. 2
1) Go to Preferences ==> Android SDK ==> SDK Tools ==> CMake
2) Select that and click Apply and Ok
Now, you need to add a CMakeLists.txt file to your project.
Path: my_project/app/CMakeLists.txt
This is what the file should look like:
# https://developer.android.com/studio/projects/add-native-code.html#create-cmake-script
# Minimum version of CMake
cmake_minimum_required(VERSION 3.4.1)
# adding CEC library
# add_library structure: add_library(lib_name lib_type_STATIC_or_SHARED source_file_path)
add_library(my_lib_name SHARED src/main/jni/my_cpp_file.cpp)
# include_directories is to provide the path to you native lib code
# include_directories structure: include_directories(native_lib_folder_path)
include_directories(src/main/jni/)
# adding Android log library
# find_library is used to find NDK API libraries (built in NDK libs)
# find_library structure: find_library(name_you_want_to_call_the_lib lib_name_in_ndk_api)
find_library(log-lib log)
# linking log lib to our native lib
# once you find the library, you have to link that library with your native library
# target_link_libraries structure: target_link_libraries(you_native_lib lib_found_using_find_library)
target_link_libraries(my_lib_name ${log-lib})
And final step: add the following to your build.gradle:
externalNativeBuild {
cmake {
path 'CMakeLists.txt'
}
}
You should be able to build it now.
th3pat3l's answer is works fine, but the official documentation for how to add C++ to a project is a little different. Here it is:
https://developer.android.com/studio/projects/add-native-code.html#create-sources
The main difference is the use of file->new->package. The package concept is for adding a java package and has a side effect of creating a folder.
You can do the same thing more directly by switching to project view and just creating the folder where you want it in the directory.

Categories

Resources