I recently updated Android Gradle Plugin to version 7.0.0 (Gradle version 7.0.2).
Since I did this update, my native library continues to be compiled regularly, but no .so files are generated in my final apk.
In fact, running the app the exception is thrown:
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/it.Ettore.raspcontroller-2/base.apk"],nativeLibraryDirectories=[/data/app/it.Ettore.raspcontroller-2/lib/x86, /data/app/it.Ettore.raspcontroller-2/base.apk!/lib/x86, /system/lib, /vendor/lib]]] couldn't find "libf-native-lib.so"
By downgrading to Android Gradle Plugin version 4.2.2 (Gradle version 6.7.1), everything works fine.
Could it be an Android Gradle Plugin bug or is it my mistake?
build.gradle :
android {
defaultConfig {
externalNativeBuild {
cmake {
cFlags "-fvisibility=hidden"
cppFlags "-fvisibility=hidden"
}
}
ndk {
moduleName "f-native-lib"
}
sourceSets.main {
jni.srcDirs = ['src/main/c']
}
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
ndk {
debugSymbolLevel 'SYMBOL_TABLE'
}
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
CMakeList.txt:
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
f-native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/c/mydir/myfile.c
)
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
)
find_library( # Sets the name of the path variable.
z-lib
# Specifies the name of the NDK library that you want CMake to locate.
z
)
target_link_libraries( # Specifies the target library.
f-native-lib
# Links the target library to the log library included in the NDK.
${log-lib}
${z-lib}
)
Activity:
static {
System.loadLibrary("f-native-lib");
}
Had the same issue, gradle plugin version 7.0.2 fixed this
Are you using tasks.whenTaskAdded in your gradle files? There is an issue with gradle plugin 7.0.0. Refer this. Use 7.0.2 or greater for the resolution.
Related
I am creating a project in android studio with using opencv library. my ndk is r23 and my cmake is 3.18.1.
this is my graddle.build(app):
plugins {
id 'com.android.application'
}
android {
namespace 'com.example.slam3'
compileSdk 33
defaultConfig {
applicationId "com.example.slam3"
minSdk 29
targetSdk 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags '-std=c++11'
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.18.1'
}
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.0'
implementation 'com.google.android.material:material:1.8.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation project(path: ':openCV')
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
i use CMakeLists.txt for using a simple native lib in my project. my CMakeLists.txt:
set(pathToProject C:/Users/HYPER_STOCK_TABRIZ/AndroidStudioProjects/Slam3)
set(pathToOpenCV C:/Users/HYPER_STOCK_TABRIZ/AndroidStudioProjects/OpenCV-android-sdk)
#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.18.1)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(${pathToOpenCV}/sdk/native/jni/include)
# Declares and names the project.
project("slam3")
# 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.
slam3
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp)
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION ${pathToProject}/app/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.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(slam3 ${log-lib} lib_opencv)
When i run project in device i see this error:
C/C++: ld: error: found local symbol '__bss_start' in global part of symbol table in file C:/Users/HYPER_STOCK_TABRIZ/AndroidStudioProjects/Slam3/app/src/main/jniLibs/arm64-v8a/libopencv_java3.so
i test this
but still i has the same error. how can i solve it?
Windows 7.
Android Studio Chipmunk 2021.2.1 Patch 2.
CMake.
Debugging "java only" apps works ok on the Nexus 5X.
Created the JNI C++ example (see https://developer.android.com/studio/projects/add-native-code#new-project).
CMakeLists.txt and gradle files all as generated.
App runs ok on Nexus 5X via USB.
Starting with debugger fails with the message:
Debugger process finished with exit code -1073741515 (0xC0000135). A library required by the native debugger might be missing on your system.
Which library "might be missing" and on which "system" Android Studio or Nexus 5X?
build.gradle (app):
plugins {
id 'com.android.application'
}
android {
compileSdk 32
defaultConfig {
applicationId "com.example.testcpp2"
minSdk 21
targetSdk 32
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ''
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
externalNativeBuild {
cmake {
path file('src/main/cpp/CMakeLists.txt')
version '3.18.1'
}
}
buildFeatures {
viewBinding true
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
CMakeLists.txt:
cmake_minimum_required(VERSION 3.18.1)
# Declares and names the project.
project("testcpp2")
# 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.
testcpp2
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
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 this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
testcpp2
# Links the target library to the log library
# included in the NDK.
${log-lib})
Thanks.
I am trying to use these libraries in my Android project but I am getting many of these similar kind of errors. I have all the tools installed (NDK, LLDB and CMake) but I am still not able to use the library.
After trying to run the project, all the C++ functions in the header files are unrecognized and compiler shows Error:error: undefined reference to 'std::string::c_str() const' and similar other undefined reference errors of C++ functions. Find the complete log of errors here.
Here is 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 )
# 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.
include_directories(src/main/cpp/include/ src/main/cpp/include/alglib3 src/main/cpp/include/device src/main/cpp/include/gsl src/main/cpp/include/base)
add_library ( gsl STATIC IMPORTED)
add_library ( gslcblas STATIC IMPORTED)
add_library ( alglib STATIC IMPORTED)
add_library ( crystalport STATIC IMPORTED)
set_target_properties( crystalport PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/cpp/libs/armeabi-v7a/libcrystalport_android.a)
set_target_properties( gsl PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/cpp/libs/armeabi-v7a/libgsl.a)
set_target_properties( gslcblas PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/cpp/libs/armeabi-v7a/libgslcblas.a)
set_target_properties( alglib PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/src/main/cpp/libs/armeabi-v7a/libalglib.a)
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
alglib
crystalport
gslcblas
gsl
)
Here is my build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "sadboy.circadian"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -fexceptions"
}
}
ndk {
abiFilters "armeabi-v7a"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
repositories{
maven {url "https://jitpack.io"}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:support-annotations:27.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Using gnustl_* seems to be able to build:
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -fexceptions"
arguments "-DANDROID_STL=gnustl_static"
}
}
Also using extern "C" to avoid runtime error in my native-lib.cpp file.
I try import openCV with contrib on Android. I referenced this site:
http://www.cnblogs.com/fx-blog/p/8206737.html
I failed to build opencv with contrib for Android, so I downloaded opencv with contrib sdk. I imported module, set dependency and write CMakeLists and build.gradle
Here is my CMakeLists and build.gradle
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)
set(pathOPENCV E:/opencv/opencv3-android-sdk-with-contrib-master/OpenCV-android-sdk)
set(pathPROJECT C:/Users/sms28/Downloads/artoolkit-android-studio-example- master/MyApplication7)
set(pathLIBOPENCV_JAVA C:/Users/sms28/Downloads/artoolkit-android-studio-example- master/MyApplication7/app/src/main/JniLibs/${ANDROID_ABI}/libopencv_java3.so)
set(CMAKE_VERBOSE_MAKEFILE on)
# set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(${pathOPENCV}/sdk/native/jni/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(libopencv_java3 SHARED IMPORTED )
set_target_properties(libopencv_java3 PROPERTIES
IMPORTED_LOCATION "${pathLIBOPENCV_JAVA}")
add_library( # Sets the name of the library.
xfeatures2d
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
src/main/cpp/xfeatures2d_init.cpp
src/main/cpp/sift.cpp
src/main/cpp/surf.cpp
src/main/cpp/freak.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 this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
xfeatures2d android log libopencv_java3
# Links the target library to the log library
# included in the NDK.
${log-lib} )
build.gradle (app)
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.sms28.myapplication"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11", "-frtti", "-fexceptions"
abiFilters 'x86', 'x86_64','armeabi-v7a', 'arm64-v8a'
}
}
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':openCVLibrary340dev')
}
And I build it, but I got errors:
click and show error image
I need to add a c project which can be compiled with gcc as follows
gcc -I/usr/include/epic5.1 -I/usr/include/i386-linux-gnu/epic5.1 -I./smproject/ -o code code.c ./smproject/smlib.so -lepic5.1
I have moved the code.c files content to my Android NDK .cpp file (src/main/cpp/native-lib.cpp) and also moved all files inside smproject directory to src/main/cpp/smproject/ directory
Here is my CMakeList.txt content
# 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.
include_directories( /usr/include/epic5.1 )
include_directories( /usr/include/i386-linux-gnu/epic5.1 )
include_directories( src/main/cpp )
set_target_properties( delorean PROPERTIES IMPORTED_LOCATION
src/main/cpp/smlib.so
)
add_library( smlib STATIC IMPORTED )
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 this
# build script, pre-built third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
smlib
)
I tried to follow Android NDK, CMake with other libraries but that didn't work for me it started throwing gradle error
tried following in build.gradle file
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "in.etpg.sampleapp"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-I/usr/include/epic5.1 -I/usr/include/i386-linux-gnu/epic5.1 -lepic5.1 -frtti -fexceptions"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
Errors
Error:cannot find -lepic5.1
Error:error: linker command failed with exit code 1 (use -v to see
Warning:warning: -lepic5.1: 'linker' input unused
Error:A problem occurred configuring project ':app'.
> executing external native build for cmake /Users/laptop.user/AndroidStudioProjects/SampleApp/app/CMakeLists.txt
You simply need to add something to your module build.gradle, like this
ndk {
moduleName "code"
cFlags "-I/usr/include/epic5.1 -I/usr/include/i386-linux-gnu/epic5.1 -lepic5.1 -frtti -fexceptions"
ldLibs "log"
}
and place the source files in main/jni.
Perhaps, you need to change cFlags to cppFlags, but I recommend that you search for more information about this, the suggestion is based on the fact that you have -fnoexceptions flag, which is for c++.
You need of course libepic5.1.so binary to use it for compilation, if you can add it's source code it will work.