I use Android Studio 2.2's cmake to build native code, in the native code I invoked the ffmpeg api, so the ffmpeg library should be packaged. My CMakelists.txt is as below:
cmake_minimum_required(VERSION 3.4.1)
include_directories(libs/arm/include)
link_directories(libs/arm/lib/)
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).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/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
# Links the target library to the log library
# included in the NDK.
${log-lib} )
add_library(avcodec-57 SHARED IMPORTED)
set_target_properties(avcodec-57 PROPERTIES IMPORTED_LOCATION C:/Users/tony/Desktop/MyApplication/app/libs/arm/lib/libavcodec-57.so)
target_link_libraries(native-lib avcodec-57)
target_link_libraries(native-lib avformat-57)
target_link_libraries(native-lib avutil-55)
target_link_libraries(native-lib avfilter-6)
In such case, I can make project successfully, but when I install the apk to emulator and run, it failed and show that "libavcodec-57.so" isn't found.
Then I use tool (analyze apk) to check the apk, found that the ffmpeg library isn't packaged.
I found a way that works for me, not sure it helps you but it might. I'm using Android Studio 2.2, and ran into your problem too.
I created a jar-file, with the prebuilt libraries in it:
lib
--|armeabi
--|--|libMyLIb.so
etc.
by simply creating a folder lib with that contents somewhere, and the executing the command
zip -r myjar.zip lib && mv myjar.zip myjar.jar
Next, I put the jar file in here:
app/libs/myjar.jar
And added these lines to the CMakeLists.txt that builds a native .so-library inside Android Studio. That is, I started with an empty project off the template for calls to native code (the default libnative-lib.so):
# already there:
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
# my addition:
add_custom_command(TARGET native-lib POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_directory
"${PROJECT_SOURCE_DIR}/libs"
$<TARGET_FILE_DIR:native-lib>)
And magically, now if I build the apk, the contents of my jar end up in the final apk. Don't ask me why this works, really, I have no clue, it was accidental.
What this means for me, is that I compile the empty libnative-lib.so, for the only purpose of tricking Android Studio into including my jar.
Perhaps someone finds a cleaner solution, and can point out where my solution is a ridiculous loop that resulted out of misunderstanding gradle and cmake...
I had the exact same problem.
Cmake does not automatically pack third library into the apk , you have to do it yourself.
Here is an exemple with libavcodec and libavutil from ffmpeg.
1- Copy your pre-built lib into app/libs/[abi]/
Exemple : app/libs/armeabi-v7a/libavcodec.so
2- Copy include into app/libs/include
Then in your cmakelist.txt add the libraries you need
find_library( log-lib log )
set(ffmpeg_DIR ../../../../libs) #set path to libs folder
add_library( libavutil SHARED IMPORTED )
set_target_properties( libavutil PROPERTIES IMPORTED_LOCATION ${ffmpeg_DIR}/${ANDROID_ABI}/libavutil.so )
add_library( libavcodec SHARED IMPORTED )
set_target_properties( libavcodec PROPERTIES IMPORTED_LOCATION ${ffmpeg_DIR}/${ANDROID_ABI}/libavcodec.so )
include_directories(libs/include) #add include dir. don't know why ../ not needed
add_library( native-lib SHARED src/main/cpp/native-lib.cpp )
target_link_libraries( native-lib libavcodec libavutil ${log-lib} )
Finally in your build.gradle set jniLibsfolder :
sourceSets.main {
jniLibs.srcDirs = ['libs']
}
Setting jniLibs.srcDir was the key for me to be able to bundle the libs into the apk.
Note that i used libs folder but you can probably use any folder you want to store your pre-built libs.
Found a working sample on github (not mine) : https://github.com/imchenjianneng/StudyTestCase
I suffered the same problem.
Gradle doesn't packaging .so files into apk while I filled CMakeLists.txt correctly, but finally I resolved it.
Add the JniLibs path into sourceSets in local build.gradle as this sample code:
https://github.com/googlesamples/android-ndk/blob/master/hello-libs/app/build.gradle
which is #Gerry mentioned in the comment.
I did:
copy .so libraries into src/main/JniLibs/${ANDROID_ABI}.
ex) mobile/src/main/JniLibs/armeabi-v7a/libavcodec.so
edit CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
# project path (absolute), change it to yours.
set(projectDir C:/Users/Administrator/AndroidStudioProjects/TestApp1)
# headers
include_directories(${projectDir}/mobile/src/main/JniLibs/${ANDROID_ABI}/include)
# sample ndk lib
add_library( native-lib SHARED src/main/cpp/native-lib.cpp )
# FFMPEG libraries
add_library( lib_avcodec SHARED IMPORTED )
set_target_properties( lib_avcodec PROPERTIES IMPORTED_LOCATION ${projectDir}/mobile/src/main/JniLibs/${ANDROID_ABI}/libavcodec.so)
# ...
# (omitted) same codes with lib_avdevice, lib_avfilter, lib_avformat, lib_avutil, lib_swresample, and lib_swscale each.
# ...
target_link_libraries( # Specifies the target library.
native-lib
lib_avcodec
lib_avdevice
lib_avfilter
lib_avformat
lib_avutil
lib_swresample
lib_swscale
)
in build.gradle (app)
build.gradle
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "your-application-Id"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
}
}
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
abiFilters 'armeabi', 'armeabi-v7a'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
# ADD THIS BLOCK.
sourceSets {
main {
// let gradle pack the shared library into apk
jniLibs.srcDirs = ['src/main/JniLibs']
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
productFlavors {
}
}
hope it helps you.
p.s. I used FFMPEG libraries that built myself.
Related
I have one prebuilt library of CPP(abc.so) and some CMake files like the config.camke, target. cmake and the header file like a.hpp,b.hpp I want it configured with my current android project, I have successfully created the NDK project and successfully configured the native-lib.CPP library and able to print the string into an android by that library method, Now I want to know how to implement the prebuilt library and its CMake files in my current CmakeLists.txt file and what should be the project structure for the native library and CMake files and header files.
Here is my CmakeLists.txt file.
cmake_minimum_required(VERSION 3.18.1)
# Declares and names the project.
project(“myproject”)
add_library( # Sets the name of the library.
myproject
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp)
include_directories(src/main/cpp/include)
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.
myproject
# Links the target library to the log library
# included in the NDK.
${log-lib})
Currently I have the following structure.
src/main/cpp/jniLibs/
abc.so
src/main/cpp/cmake/
config.cmake
target.cmake
src/main/cpp/include/
a.hpp
b.hpp
src/main/cpp/
native-lib.cpp
And in the gradle it is
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.18.1'
}
}
So what should be the configuration for the above structure?
I want to compile libs that are bundled in my project. And I run into 2 issues.
First of one that Cmake does not seems to detect/include that directory.
Second one is after bundled directory is detected/included instead of android toolchain a system's one is used to compile libs.
As workaround to 1st issue I added if(ANDROID) to add that directory so it can be included.
if(EXISTS "${CMAKE_SOURCE_DIR}/libs/CMakeLists.txt")
message(STATUS "Using bundled libraries located at ${CMAKE_SOURCE_DIR}/libs")
if(ANDROID)
add_subdirectory(libs)
else()
include(libs/CMakeLists.txt)
endif()
else()
So for me expected result should follow like this include libs/CMakeLists.txt and build libs using toolchain provided by NDK
If you are trying to use non-ndk prebuilt library for your native component, then add those library details into CMake as mentioned below.
add_library( imported-lib
SHARED
IMPORTED )
set_target_properties( # Specifies the target library.
imported-lib
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.
imported-lib/src/${ANDROID_ABI}/libimported-lib.so )
https://developer.android.com/studio/projects/configure-cmake#add-other-library
For building native library using cmake system.
define CMakeLists.txt in your module(local source code to be used for generating lib).
add_library(xyz STATIC
folder-name/xyz.cpp)
target_include_directories(xyz PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/..)
define CMakeLists.txt in your android app module cpp folder.
cmake_minimum_required(VERSION 3.4.1)
/* using existing ndk lib , if you want you can remove this */
build native_app_glue as a static lib.
add_library(native_app_glue STATIC
${ANDROID_NDK}/sources/android/native_app_glue/android_native_app_glue.c)
Import the CMakeLists.txt for the glm library
add_subdirectory(glm)
/* require if u r adding header files location */
target_include_directories(game PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/data
${ANDROID_NDK}/sources/android/native_app_glue)
add lib dependencies
target_link_libraries(
native_app_glue
xyz
)
define app module build.gradle
android {
compileSdkVersion 28
defaultConfig {
applicationId 'com.google.sample.tunnel'
minSdkVersion 14
targetSdkVersion 28
versionCode 1
versionName '1.0'
}
externalNativeBuild {
cmake {
version '3.10.2'
path 'src/main/cpp/CMakeLists.txt' // location of second(app module) CMakeLists.txt
}
}
}
Also check CMake build commands to verify what all parameters are used during build.
Check this file
app.externalNativeBuild\cmake\debug\x86\cmake_build_command.txt
So this is how it builds libcurl it does configure it 1st then compile using wrong toolchain. I did found something here https://developer.android.com/ndk/guides/other_build_systems but it does export for 1 toolchain per build. Mine is using 2 abi's
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK.
abiFilters 'armeabi-v7a', 'arm64-v8a'
}
As you can see it's using ExternalProject_Add instead.
#-----------------------------------------------------------------
# Build bundled cURL library
#-----------------------------------------------------------------
if(BUNDLED_CURL AND (BUILD_CLIENT OR BUILD_SERVER))
ExternalProject_Add(
bundled_curl
SOURCE_DIR ${CMAKE_CURRENT_LIST_DIR}/curl
CONFIGURE_COMMAND ./configure --prefix=${CMAKE_CURRENT_BINARY_DIR}/libs/curl
--enable-shared=no --enable-static=yes
--enable-http --enable-ftp --disable-file
--disable-ldap --disable-ldaps --disable-rtsp
--enable-proxy --disable-dict --disable-telnet
--disable-tftp --disable-pop3 --disable-imap
--disable-smb --disable-smtp --disable-gopher
--without-ssl --without-libssh2 --without-nghttp2
--without-gssapi --with-zlib
--disable-ares --enable-threaded-resolver
--enable-ipv6 --enable-unix-sockets
--without-libidn2 --disable-manual
--disable-sspi --enable-libgcc
--without-libmetalink --without-libpsl
--without-librtmp ${CROSS_COMPILE32_FLAGS}
PREFIX ${CMAKE_CURRENT_BINARY_DIR}/libs/curl
BUILD_COMMAND make
INSTALL_COMMAND make install
BUILD_IN_SOURCE 1
)
set(CURL_BUNDLED_LIBRARY "${CMAKE_CURRENT_BINARY_DIR}/libs/curl/lib/libcurl.a")
set(CURL_BUNDLED_INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/libs/curl/include")
endif()
When trying to build a signed APK, it fails with ~100 lines repeating:
Library/Android/sdk/ndk-bundle/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: PLT offset too large, try linking with --long-plt
I've added --long-plt to the arguments:
externalNativeBuild {
cmake {
...
arguments '-DANDROID_STL=c++_static', '-Wl,--long-plt'
cppFlags "-frtti -fexceptions", "-DANDROID_TOOLCHAIN=clang", "-DANDROID_STL=c++_shared"
}
}
But it doesn't seem to change anything.
It works with non-signed (debug) apk generation and works with arm64-v8a.
I'm dealing with >1GB of native code, so I'm guessing that's the main reason.
It seems there is almost no documentation or search result regarding this.
Does --long-plt need to be put somewhere else? If not, is there another setting that can be changed? Or would splitting the code into separate libraries help?
Here's the CMakeLists.txt for reference:
string(REPLACE "." "/" JAVA_GEN_SUBDIR ${JAVA_GEN_PACKAGE})
set(JAVA_GEN_DIR ${Project_SOURCE_DIR}/src/main/java/${JAVA_GEN_SUBDIR}/../../../../../generated)
# configure import libs
set(distribution_DIR ${PROJECT_SOURCE_DIR}/distribution)
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Note: One could use a 'GLOB' here, but newly added source files won't auto-regen build files
# After adding files, you'd need to 'touch' the CMakeLists.txt to re-gen
# SWIG required for build. Easy install is "brew install swig"
#Site: http://swig.org
find_package(SWIG REQUIRED)
include(${SWIG_USE_FILE})
# Remove old java files, in case we don't need to generate some of them anymore
#file(REMOVE_RECURSE ${JAVA_GEN_DIR})
# Ensure file recognized as C++ (otherwise, exported as C file)
set_property(SOURCE src/main/cpp/${LIBRARY_NAME}.i PROPERTY CPLUSPLUS ON)
# Setup SWIG flags and locations
set(CMAKE_SWIG_FLAGS -c++ -package ${JAVA_GEN_PACKAGE})
set(CMAKE_SWIG_OUTDIR ${JAVA_GEN_DIR})
# Export a wrapper file to Java, and link with the created C++ library
swig_add_module(${LIBRARY_NAME}_Wrapper java ${SWIG_I_FILE})
swig_link_libraries(${LIBRARY_NAME}_Wrapper ${LIBRARY_NAME})
# Include a location to the header files
include_directories(
src/main/cpp
${NativeLibPath}
${LUAPATH}
)
set(LUAPATH ${NativeLibPath}/lua)
file(GLOB ALL_SOURCE_FILES
"src/main/cpp/*.cpp"
"${NativeLibPath}/*.cpp"
"${LUAPATH}/*.c"
)
# 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.
${LIBRARY_NAME}
# Sets the library as a shared library.
SHARED
# Provides the list of files to compile.
${ALL_SOURCE_FILES} )
target_include_directories(${LIBRARY_NAME} PRIVATE)
# 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.
${LIBRARY_NAME}
android
# Links the target library to the log library
# included in the NDK.
${log-lib}
)
I agree there is not so much documentation on internet, even if we can find some.
First, try to modify your configuration file:
externalNativeBuild {
cmake {
...
arguments '-DANDROID_STL=c++_static'
cppFlags "-frtti -fexceptions", "-DANDROID_TOOLCHAIN=clang", "-DANDROID_STL=c++_shared", "-Wl,--long-plt"
}
}
Let me know, if it changes anything?
I'm trying to implement code from
here
When trying to compile it, I get these errors:
Error:(339) undefined reference to `ANativeWindow_fromSurface'
Error:(340) und
Error:(349) undefined reference to `ANativeWindow_setBuffersGeometry'
Error:(351) undefined reference to `ANativeWindow_lock'
Error:(353) undefined reference to `ANativeWindow_release'
Error:(406) undefined reference to `ANativeWindow_unlockAndPost'
Error:(407) undefined reference to `ANativeWindow_release'
Error:error: linker command failed with exit code 1 (use -v to see invocation)
Information:BUILD FAILED
Information:Total time: 2.012 secs
Information:8 errors
Information:0 warnings
Information:See complete output in console
ANativeWindow gets from 2 c++ .h files, one is
#include <android/native_window.h>
and other
#include <android/native_window_jni.h>
Now, my CMake file looks like this:
cmake_minimum_required(VERSION 3.4.1)
# OpenCV stuff
include_directories(D:\\opencv-3.2.0-android-sdk\\OpenCV-android-sdk\\sdk\\native\\jni\\include)
add_library( lib_opencv SHARED IMPORTED )
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
add_compile_options(-std=c++11)
# 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).
src/main/cpp/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 the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
-ljnigraphics
lib_opencv
# Links the target library to the log library
# included in the NDK.
${log-lib} )
And my gradle file regarding c++ code:
defaultConfig {
minSdkVersion globalConfiguration.getAt("androidMinSdkVersion")
targetSdkVersion globalConfiguration.getAt("androidTargetSdkVersion")
applicationId globalConfiguration.getAt("androidApplicationId")
versionCode globalConfiguration.getAt("androidVersionCode")
versionName globalConfiguration.getAt("androidVersionName")
testInstrumentationRunner globalConfiguration.getAt("testInstrumentationRunner")
testApplicationId globalConfiguration.getAt("testApplicationId")
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions -Werror -fexceptions -std=c++11"
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
}
}
multiDexEnabled = true
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
I would appreciate if someone would help me with this
You need to link against libandroid.
target_link_libraries( # Specifies the target library.
native-lib
-ljnigraphics
-landroid # Add this.
lib_opencv
# Links the target library to the log library
# included in the NDK.
${log-lib} )
I use Android Studio 2.2 and cmake to build jni file.
I want to show log in jni file but get error message "undefined reference to `__android_log_write".
My CMakeLists.txt file is :
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).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/native-lib.cpp )
add_library( # Sets the name of the library.
test-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
# Associated headers in the same location as their source
# file are automatically included.
src/main/cpp/test-lib.cpp )
include_directories( src/main/jni/ )
# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included 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 the
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
test-lib
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
And my two jni files are the same as below without function name
JNIEXPORT jstring JNICALL Java_com_cyweemotion_www_jnitest_MainActivity_stringFromJNI
(JNIEnv *env, jobject){
__android_log_write(ANDROID_LOG_ERROR, "Tag", "Error here");
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());
};
My build.gradle(Module:app) is
android {
compileSdkVersion 23
buildToolsVersion "24.0.3"
defaultConfig {
minSdkVersion 19
targetSdkVersion 24
versionCode 2
versionName '1.02'
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.config
}
debug {
jniDebuggable false
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
productFlavors {
}
}
According to the android document:Add C and C++ Code to Your Project. I think I can use log api.
What's wrong in my code or my setting ?
Update:
I found it is not problem in my first jni library(Update code).
It only occurs error in the second library.
ex: target_link_libraries(test-lib, native-lib, ...), native-lib is the second library to be loaded.
So native-lib can't use log api.
Now I only can do is to remove native-lib. However I really want to know why ?
I finally found I should separated to do the link.
target_link_libraries( # Specifies the target library.
test-lib
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib} )