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.
Related
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
Question Update:
Linker error
I installed Intel OpenCL SDK with Android Support on dev pc.
Difficulty building Android Studio Solution with C++ support using CMakelists.txt
OpenCL & OpenCV in Android Studio using CMakeLists.txt
CMakeLists.txt:
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
#Added 2 path definitions to support 20160825 additions
set(pathToProject C:/Users/eogha/Desktop/HelloOpenCV)
set(pathToOpenCv C:/OpenCV-3.1.0-android-sdk)
set(pathToOpenCL "C:/Program Files (x86)/Intel/OpenCL SDK/5.3")
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
#Addition suggested by Bruno Alexandre Krinski 20160825
include_directories(${pathToOpenCv}/sdk/native/jni/include )
include_directories(${pathToOpenCL}/include)
# 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( native-lib SHARED src/main/cpp/native-lib.cpp )
#Addition suggested by Bruno Alexandre Krinski 20160825
add_library( lib_opencv SHARED IMPORTED )
#Addition suggested by Bruno Alexandre Krinski 20160825
set_target_properties( lib_opencv PROPERTIES IMPORTED_LOCATION
${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so )
add_library( lib_opencl SHARED IMPORTED )
set_target_properties( lib_opencl PROPERTIES IMPORTED_LOCATION "C:/Program
Files (x86)/Intel/OpenCL SDK/5.3/lib/android64/libOpenCL.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.
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( native-lib $\{log-lib} lib_opencv lib_opencl )
Android Studio Error: linker command failed with exit code 1 (use -v to see invocation)
Finally !! The OpenCL Shared Object library seems to be imported into the android project structure. But now I have this linker error. How do I resolve this in Windows 10 ??
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} )
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.