Android undefined reference with NDK - android

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} )

Related

C/C++ debug|armeabi-v7a : CMake Error at xxxxxx

Only the NDK version 17.2.4988734 can be compiled, and it cannot be compiled if it is replaced by 20.1.5948944
my CMakeLists.txt
ask for help
cmake_minimum_required(VERSION 3.4.1)
SET (CMAKE_C_FLAGS_DEBUG "-g")
SET (CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
set(EXTERN_DIR ../ios/Classes/Clogan)
add_subdirectory(${EXTERN_DIR} clogan.out)
include_directories(${EXTERN_DIR})
link_directories(clogan.out)
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.
add_library(logan SHARED ../ios/Classes/Clogan/clogan_core.c)
target_link_libraries(logan ${log-lib} z clogan)
The error message is as follows:
C/C++ debug|armeabi-v7a : CMake Error at /Users/admin/Library/Android/sdk/ndk/20.1.5948944/build/cmake/android.toolchain.cmake:169 (message):
GCC is no longer supported. See
https://android.googlesource.com/platform/ndk/+/master/docs/ClangMigration.md.
Call Stack (most recent call first):
/Users/admin/Library/Android/sdk/cmake/3.10.2.4988404/share/cmake-3.10/Modules/CMakeDetermineSystem.cmake:94 (include)
CMakeLists.txt
I try to modify
abiFilters 'armeabi-v7a'
but it didn't work

Error while building v8 in android embedded

I tried to import v8 7.6.90 as a static library in my Android NDK project, but it fails at the link process.
Here is my args.gn config for v8 build:
android_unstripped_runtime_outputs = true
v8_use_external_startup_data = false
is_debug = false
symbol_level = 1
target_cpu = "arm"
target_os = "android"
use_goma = false
v8_enable_i18n_support = false
v8_static_library = true
is_component_build = false
v8_monolithic = true
v8_android_log_stdout = true
It did compile and gave me a libv8_monolith.a after compiling, I started to set up my Android project then.
CMakeLists.txt:
cmake_minimum_required(VERSION 3.4.1)
add_library(v8 STATIC IMPORTED)
set_target_properties( v8 PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/libs/${ANDROID_ABI}/libv8_monolith.a)
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).
${CMAKE_SOURCE_DIR}/src/main/cpp/native-lib.cpp)
target_include_directories( native-lib PRIVATE ${CMAKE_SOURCE_DIR}/libs/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.
native-lib
v8
# Links the target library to the log library
# included in the NDK.
${log-lib})
build.gradle:
externalNativeBuild {
cmake {
cppFlags "-std=c++0x"
}
}
ndk {
abiFilters "armeabi-v7a"
}
native-lib.cpp runs the same example in https://chromium.googlesource.com/v8/v8/+/master/samples/hello-world.cc
But ld always complains about the error:
../../src/compiler/graph-visualizer.cc:0: error: undefined reference to 'vtable for std::__1::basic_ios<char, std::__1::char_traits<char> >'
/Users/fredguo/Library/Android/sdk/ndk-bundle/toolchains/llvm/prebuilt/darwin-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: the vtable symbol may be undefined because the class is missing its key function
error: undefined reference to 'v8::platform::NewDefaultPlatform(int, v8::platform::IdleTaskSupport, v8::platform::InProcessStackDumping, std::__ndk1::unique_ptr<v8::TracingController, std::__ndk1::default_delete<v8::TracingController> >)'
I tried to objdump my libv8_monolith.a:
objdump -D app/libs/armeabi-v7a/libv8_monolith.a | grep NewDefault
Disassembly of section .text._ZN2v88platform18NewDefaultPlatformEiNS0_15IdleTaskSupportENS0_21InProcessStackDumpingENSt3__110unique_ptrINS_17TracingControllerENS3_14default_deleteIS5_EEEE:
_ZN2v88platform18NewDefaultPlatformEiNS0_15IdleTaskSupportENS0_21InProcessStackDumpingENSt3__110unique_ptrINS_17TracingControllerENS3_14default_deleteIS5_EEEE:
4: 81 b0 01 2b blhs #442884 <_ZN2v88platform18NewDefaultPlatformEiNS0_15IdleTaskSupportENS0_21InProcessStackDumpingENSt3__110unique_ptrINS_17TracingControllerENS3_14default_deleteIS5_EEEE+0x6C210>
Disassembly of section .rel.text._ZN2v88platform18NewDefaultPlatformEiNS0_15IdleTaskSupportENS0_21InProcessStackDumpingENSt3__110unique_ptrINS_17TracingControllerENS3_14default_deleteIS5_EEEE:
.rel.text._ZN2v88platform18NewDefaultPlatformEiNS0_15IdleTaskSupportENS0_21InProcessStackDumpingENSt3__110unique_ptrINS_17TracingControllerENS3_14default_deleteIS5_EEEE:
Disassembly of section .ARM.exidx.text._ZN2v88platform18NewDefaultPlatformEiNS0_15IdleTaskSupportENS0_21InProcessStackDumpingENSt3__110unique_ptrINS_17TracingControllerENS3_14default_deleteIS5_EEEE:
.ARM.exidx.text._ZN2v88platform18NewDefaultPlatformEiNS0_15IdleTaskSupportENS0_21InProcessStackDumpingENSt3__110unique_ptrINS_17TracingControllerENS3_14default_deleteIS5_EEEE:
Disassembly of section .rel.ARM.exidx.text._ZN2v88platform18NewDefaultPlatformEiNS0_15IdleTaskSupportENS0_21InProcessStackDumpingENSt3__110unique_ptrINS_17TracingControllerENS3_14default_deleteIS5_EEEE:
.rel.ARM.exidx.text._ZN2v88platform18NewDefaultPlatformEiNS0_15IdleTaskSupportENS0_21InProcessStackDumpingENSt3__110unique_ptrINS_17TracingControllerENS3_14default_deleteIS5_EEEE:
Disassembly of section .text._ZN2v811ArrayBuffer9Allocator19NewDefaultAllocatorEv:
_ZN2v811ArrayBuffer9Allocator19NewDefaultAllocatorEv:
Disassembly of section .rel.text._ZN2v811ArrayBuffer9Allocator19NewDefaultAllocatorEv:
.rel.text._ZN2v811ArrayBuffer9Allocator19NewDefaultAllocatorEv:
Disassembly of section .ARM.exidx.text._ZN2v811ArrayBuffer9Allocator19NewDefaultAllocatorEv:
.ARM.exidx.text._ZN2v811ArrayBuffer9Allocator19NewDefaultAllocatorEv:
Disassembly of section .rel.ARM.exidx.text._ZN2v811ArrayBuffer9Allocator19NewDefaultAllocatorEv:
.rel.ARM.exidx.text._ZN2v811ArrayBuffer9Allocator19NewDefaultAllocatorEv:
_ZN2v88platform18NewDefaultPlatformEiNS0_15IdleTaskSupportENS0_21InProcessStackDumpingENSt3__110unique_ptrINS_17TracingControllerENS3_14default_deleteIS5_EEEE
should be
v8::platform::NewDefaultPlatform(int, v8::platform::IdleTaskSupport, v8::platform::InProcessStackDumping, std::__1::unique_ptr<v8::TracingController, std::__1::default_delete<v8::TracingController> >)
However, in the error message, the ld tries to find std::__ndk1::unique_ptr<v8::TracingController, std::__ndk1::default_delete<v8::TracingController> >)
I think they have a different namespace, but how do I solve this problem? I found a similar issue here https://groups.google.com/forum/#!topic/v8-users/Jb1VSouy2Z0, but the solution didn't work for me.
Try modify namespace macro in v8/buildtools/third_party/libc++/trunk/include/__config:
from _LIBCPP_CONCAT(__,_LIBCPP_ABI_VERSION) to _LIBCPP_CONCAT(__ndk,_LIBCPP_ABI_VERSION).
Hope this helps.

needed by '../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so', missing and no known rule to make it

I want to implement AEs and RSA in my project.i downloaded pre built openssl and included in my project. After including openssl in my project i am getting error as above mentioned.i searched on SO some answers mentioned to include set_target_properties in CMake(U can check my cmake below) but still no use.
My CMakeLists.txt
# 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.4.1)
# 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 )
add_library(openssl SHARED IMPORTED)
set_target_properties (openssl PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/lib/libcrypto.so )
# 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.
include_directories(jni/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 )
# 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
${CMAKE_CURRENT_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/lib/libcrypto.a
${CMAKE_CURRENT_SOURCE_DIR}/jniLibs/${ANDROID_ABI}/lib/libssl.a
# Links the target library to the log library
# included in the NDK.
${log-lib} )
and My build.graddle is here
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.manvish.prebuiltcrypto"
minSdkVersion 14
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
abiFilters "x86_64", "armeabi-v7a"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
help me to resolve this issue?
The complete error log is here
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing process /home/manvish/Android/Sdk/cmake/3.6.4111459/bin/cmake with arguments {--build /home/manvish/Gajanand/PrebuiltCrypto/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target native-lib}
ninja: error: '../../../../jniLibs/armeabi-v7a/lib/libcrypto.a', needed by '../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so', missing and no known rule to make it
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 2s
I can guess that you get the cited error because you don't have jniLibs/x86_64/lib/libcrypto.a. Try to use
abiFilters "x86", "armeabi-v7a"
This error happend to me when upgrade targetSDKversion from 21 to 27. The last change of this horrible stage of my life as a Android programmer was this error:
What went wrong:
Execution failed for task ':myProject:externalNativeBuildDebug'.
Build command failed.
Error while executing process C:\Users\myUser\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\workspaces\android\myProject.externalNativeBuild\cmake\debug\armeabi-v7a --target myProject}
ninja: error: '../../../../src/main/libs/armeabi-v7a/libcrypto.so', needed by '../../../../build/intermediates/cmake/debug/obj/armeabi-v7a/libmyProject.so', missing and no known rule to make it
My app uses a own .so which uses funcions of libcrypto.so inside him, for that reason appers myproject.so
Change the old path for the new one, for that, you need to go to External Build Files/CMakeLists.txt in Android Studio (if dont appers in your Android Studio interface, go to your FileSystem and search it inside your project, and compile after realize this change).
If doens't exists, add set_target_properties:
set_target_properties( # Specifies the target library.
crypto-lib
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/src/main/libs/${ANDROID_ABI}/libcrypto.so )
To:
set_target_properties( # Specifies the target library.
crypto-lib
# Specifies the parameter you want to define.
PROPERTIES IMPORTED_LOCATION
# Provides the path to the library you want to import.${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libcrypto.so )
Now, build the project and run the app.
Thing related:
Dont need to add crypto statically (is libcrypto, but his name is crypto).
System.loadLibrary("crypto");
Related info:
Description of Android.mk
Port to BoringSSl (no need to me to work the app)
I hope this help!

Android ndk(cmake): 'undefined reference to `__android_log_write' when using log api in the second jni library

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} )

Android Studio 2.2 doesn't package third party library to apk

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.

Categories

Resources