I'm developing an Android application for simple Facial recognition.
My Application build and run fine in emulator,
But App can't install in real device,
And we get Gradle error for CMake related task, with error-message:
Task :engine:configureCMakeDebug[arm64-v8a] FAILED
Execution failed for task ':engine:configureCMakeDebug[arm64-v8a]'.
[CXX1410] D:\KAM\code\AET\AET\engine.cxx\Debug\3p465u2i\arm64-v8a\android_gradle_build.json debug|arm64-v8a : expected buildTargetsCommandComponents or ncnn-release-arm64-v8a.buildCommandComponents to exist
CMakeList.txt:
cmake_minimum_required(VERSION 3.4.1)
set(CMAKE_BUILD_TYPE "release")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../../../distribution/${ANDROID_ABI})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
add_library(ncnn STATIC IMPORTED)
set_target_properties(ncnn PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libncnn.a)
add_library(opencv_core STATIC IMPORTED)
set_target_properties(opencv_core PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libopencv_core.so)
add_library(opencv_imgproc STATIC IMPORTED)
set_target_properties(opencv_imgproc PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libopencv_imgproc.so)
add_library(opencv_codecs STATIC IMPORTED)
set_target_properties(opencv_codecs PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI}/libopencv_imgcodecs.so)
file(GLOB jni_srcs
"*.cpp"
"detection/*.cpp"
"live/*.cpp"
)
add_library(engine SHARED ${jni_srcs})
target_link_libraries(engine log jnigraphics ncnn opencv_core opencv_imgproc opencv_codecs android -static-openmp -fopenmp)
build.gradle(Module: engine):
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
minSdkVersion 28
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -frtti -fexceptions"
abiFilters 'arm64-v8a'
arguments "-DANDROID_ARM_NEON=TRUE"
arguments "-DANDROID_TOOLCHAIN=clang"
}
}
}
packagingOptions {
pickFirst '**/*.so'
}
externalNativeBuild {
cmake {
version = "3.6.0"
path "src/main/cpp/CMakeLists.txt"
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
ndkVersion '22.1.7171670'
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.core:core-ktx:1.3.2'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
I try to find the reason but no luck. So far find only [this][1].
Any help or resource will be great for me. Thanks
I just met similar "expected buildTargetsCommandComponents ..." error log.
And like many other times, deleting the build directory and restarting IDE fixed the issue.
first rebuild your app and then check this location
PROJECT PATH...\app\build\intermediates\cmake\debug\obj
There should be at least 4 folders for different abi's
x86 - x64 -- arm64/v7a -- arm64/v8
if its not exist there
apply this to your build.gradle
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a'
Related
I'm trying to build Tensorflow lite with C++ natively on Android device. I've built a .So file of tensorflow for every architecture and placed into the jniLibs folder. This is my Cmake file:
set(pathToProject /ih/user/project/NativeTfLite/app)
set(libs ${pathToProject}/src/main/jniLibs)
add_library(libtensorflowLite SHARED IMPORTED
)
set_target_properties(libtensorflowLite PROPERTIES IMPORTED_LOCATION
${libs}/${ANDROID_ABI}/libtensorflowLite.so)
find_library(
log-lib
log )
add_library(
native-lib
SHARED
native-lib.cpp)
target_include_directories(native-lib PRIVATE
${lib}/include)
target_link_libraries( # Specifies the target library.
native-lib. #Problem is here when linking native-lib with libtensorflowlite
libtensorflowLite
${log-lib} )
During compile time these files are found. However in target_link_libraries line, the linking of the 2 libraries libtensorflowlite and native-lib there is a runtime crash with the following error:
java.lang.UnsatisfiedLinkError: dlopen failed: library "/Users/ih/project/cameraFrames/NativeTfLite/app/src/main/jniLibs/arm64-v8a/libtensorflowLite.so" not found
Additionally the .SO files are not linked to the APK.
Here is my build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.proj.nativetflite"
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
sourceSets {
main {
jniLibs.srcDir 'src/main/jniLibs'
jniLibs.srcDirs = ["src/main/jniLibs"]
}
}
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
arguments '-DCMAKE_VERBOSE_MAKEFILE=ON'
}
}
ndk {
abiFilters "armeabi-v7a", "x86" , "arm64-v8a"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
version "3.10.2"
}
}
}
dependencies {
implementation fileTree(dir: 'jniLibs', include: '**/*.so')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
What is causing this?
Prior to AGP 4.0 you need to explicitly package prebuilt libraries with jniLibs: https://developer.android.com/studio/projects/gradle-external-native-builds#jniLibs. Note that the docs say you don't need to do this for anything declared by CMake, but I'm fairly certain the docs are wrong here (I've reached out to the doc owner to see if we need to fix that or not, since it is correct for 4.0).
This should be unnecessary in 4.0, but that hasn't reached stable yet.
Although I think Dan Albert's answer should be accepted, I'd like to add that I got the exact same error because I had not set the correct path to my tflite lib.
I kept my .so files in src/main/cpp/tflite/lib/${ANDROID_ABI}/, so in build.gradle I needed to add:
main {
jniLibs.srcDirs = ['src/main/cpp/tflite/lib']
}
also, it could be worth checking out this answer (also from Dan) regarding making sure the SONAME of the .so is correct.
I've tried all the ways found on StackOverflow and still facing the issue.
I've created a demo Android project with native support, added a library to it and moved all native code into the library.
Now I'm unable to stop on breakpoints in native code, native debugger became active only after a SEGFAULT crash.
I've added defaultPublishConfig "debug" into mylibrary build.gradle and debuggable true to app build.fradle. There was enough for native debugging earlier. But it is not working since Android Studio upgrade.
Here are the full build.gradle files
app
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.raistlin.myapplication"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
debug {
debuggable true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation project(path: ':mylibrary')
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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'
}
mylibrary
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
defaultPublishConfig "debug"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
arguments "-DANDROID_TOOLCHAIN=clang"
cppFlags "-fexceptions", "-std=c++11", "-DJSONCPP_NO_LOCALE_SUPPORT"
version "3.10.2"
}
}
}
buildTypes {
debug {
debuggable true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.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'
}
First, check debug variant is selected for library.
As your build.gradle has a setting, path "src/main/cpp/CMakeLists.txt", it won't build I think. So set targets, rebuild and check again.
If breakpoints doesn't work after build, old garbage may be left in the build cache and causes problems. Open project directory in explorer and delete build cache (.externalNativeBuild folder) manually and build the project again. I also delete build folder, as it contains .so files in intermediate directory, but it's optional.
Android Studio does not clean libraries in the test device. They are overwritten basically, but clear them manually depending on needs. Files are in /data/app/(package name)/lib/(cpu arch.)/.
NB: Synchronize menu of device file explorer does not synchronize properly under lib or (cpu arch.) directory. To synchronize, select /data or /data/app and choose Synchronize.
NB.1 If targets is omitted, Android Studio seems to build no targets. The built output is in (project)/app/build/intermediates/cmake/(flavor)/obj/(cpu architecture). If it seems to work without any targets, check files on device. They confuse test results.
NB.2 debuggable true is for release build to enable debugging. No need to set it for debug build, as debuggable flag is set as default.
NB.3 Seems version dependent but Gradle in Android Studio does not clean .externalNativeBuild tree properly even if clean or rebuild is called, and confuses native code build configs. It was around AS3.0, as I remember.
NB.4 My environment is
Android Stuidio 3.2.1
classpath 'com.android.tools.build:gradle:3.2.1'
gradle-4.7-all
CMake: default(3.6.4111459)
I know there are newer versions for Android Studio, Gradle and CMake but they are buggy, so I chose current environment. As far as I've experienced, Android Studio 3.3, gradle:3.3.0, gradle-4.10.1-all have severe bug in VCS(git). Wrong file contents are shown in editor and build fails, sometimes. Setting CMake version to 3.10.x (3.10.2 for me) also seems buggy.
Here's a copy from my project as a sample, partially modified from original one but may work.
I've checked breakpoints in a library work in Android Studio 3.2.1.
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "0.0.1"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'proguard-rules.pro'
externalNativeBuild {
cmake {
cppFlags "-std=c++11"
arguments "-DANDROID_STL=c++_static"
targets "sample"
}
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
dependencies {
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'
}
Update
targets is mentioned here Guide - Link Gradle to your native library - Specify optional configurations.
I am using JNI code in my project with abiFilters like below
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.intel.hellojni"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags ""
}
}
ndk {
abiFilters "armeabi", "armeabi-v7a", "x86", "mips"
}
}
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:+'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
it's working fine in one system but after moving a code to other system,it show below error for import,i have checked with new created project it shoes the same error
ABIs [armeabi, mips] are not supported for platform. Supported ABIs are [armeabi-v7a, arm64-v8a, x86, x86_64].
Build command failed.
Error while executing process F:\sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {-HC:\Users\Intel\Downloads\TestJNI\app -BC:\Users\Intel\Downloads\TestJNI\app\.externalNativeBuild\cmake\debug\armeabi -DANDROID_ABI=armeabi -DANDROID_PLATFORM=android-15 -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=C:\Users\Intel\Downloads\TestJNI\app\build\intermediates\cmake\debug\obj\armeabi -DCMAKE_BUILD_TYPE=Debug -DANDROID_NDK=F:\sdk\ndk-bundle -DCMAKE_CXX_FLAGS= -DCMAKE_TOOLCHAIN_FILE=F:\sdk\ndk-bundle\build\cmake\android.toolchain.cmake -DCMAKE_MAKE_PROGRAM=F:\sdk\cmake\3.6.4111459\bin\ninja.exe -GAndroid Gradle - Ninja}
(include) CMakeLists.txt
Open File
CMake Error: CMAKE_C_COMPILER not set, after EnableLanguage
CMake Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!
if I remove armeabi and mips like below then it's working
ndk {
abiFilters "armeabi-v7a", "x86"
}
I have already installed CMake and NDK for android studio.
As the message says, those ABIs are no longer supported by the NDK. This is mentioned in the NDK r17 changelog:
Support for ARMv5 (armeabi), MIPS, and MIPS64 has been removed. Attempting to build any of these ABIs will result in an error.
As others have said, there are not a significant number of devices out there that benefit from targeting any of these ABIs.
remove 'armebi' from abiFilters in your build.gradle file.
armebi is no longer supported by the NDK. A correct list could be:
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a', 'x86', 'x86_64'
}
armeabi are no longer supported by NDK. Remove armeabi from build gradle or replace
with supported ABIs e.g 'x86'
The same issue using Android Studio 4.1.1 on Windows 10. Changing the version in build.gradle fixes e.g.
from
classpath "com.android.tools.build:gradle:4.1.1"
to
classpath "com.android.tools.build:gradle:4.0.0".
I'm trying to make an android application that uses the Vulkan API for rendering, but I always get the "undefined reference to 'vkCreateInstance'" message.
Here is my CMakeList.txt:
cmake_minimum_required(VERSION 3.4.1)
add_library( native-lib
SHARED
src/main/cpp/native-lib.cpp)
add_library( vulkan_activity
SHARED
src/main/cpp/vulkan_activity.cpp)
add_library( vulkan_stuff
SHARED
src/main/cpp/vulkan_stuff.cpp)
add_library( vulkan_buffers
SHARED
src/main/cpp/vulkan_buffers.cpp)
find_library( log-lib
log )
target_link_libraries( native-lib
vulkan_stuffx
vulkan_activity
vulkan_buffers
vulkan
${log-lib})
And this is my build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.game.productions.phenyl.futuroland"
minSdkVersion 16
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions -v"
}
}
}
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'
implementation 'com.google.android.gms:play-services-location:11.8.0'
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'
}
I want to build my application from scartch, so I don't want get anwsers like use the samples.
Android only has libvulkan.so on API 26 and higher (Nougat). Some devices supported it on Marshmallow, but you're not guaranteed that libvulkan.so will be present on API < 26, so the NDK won't let you link against it unless your NDK target platform is >= 26, since otherwise your app won't even launch on many of the devices that you say you want to be able to run on (minSdkVersion 16).
If you set minSdkVersion to 26, your native code should build against that version of the NDK headers/libraries, and libvulkan.so will be available at link time.
Or, if you're careful to only try to load your shared library that links against libvulkan.so when you're running on a Nougat device, you could leave minSdkVersion alone and add externalNativeBuild { cmake { arguments "-DANDROID_PLATFORM=android-26" } }. You'll want to carefully test on pre-Nougat devices or emulator images, since it's easy to accidentally end up with library dependencies that don't exist on earlier versions of the OS.
Or, instead of linking against libvulkan.so directly, you can use dlopen to load libvulkan.so at runtime, with a fallback or graceful exit if it fails, and then use dlsym() to get a pointer to vkGetInstanceProcAddr, and load everything else using that. This is the only way to use Vulkan on the Marshmallow devices that have it.
I have been stuck on this problem for 2 days and have tried out all possible solutions given on stackoverflow. Below is my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
sourceSets.main.jni.srcDirs = []
sourceSets.main.jniLibs.srcDir 'src/main/libs'
defaultConfig {
applicationId "com.example.anannyauberoi.testingcam"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
ndk {
moduleName "app"
cFlags "-std=c++11 -fexceptions"
ldLibs "log"
stl "gnustl_shared"
abiFilter "armeabi-v7a"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { jni.srcDirs = []
res.srcDirs = ['src/main/res']
jniLibs.srcDirs=['src/main/libs']
} }
//sourceSets.main.jni.srcDirs = []
// disable automatic ndk-build call, which ignore our Android.mk
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "C:/Users/Anannya-Uberoi/AppData/Local/Android/sdk/ndk-bundle/ndk-build.cmd",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=src/main/jniLibs',
'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
'NDK_APPLICATION_MK=src/main/jni/Application.mk'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
tasks.all { task ->
if (task.name.startsWith('compile') && task.name.endsWith('Ndk')) {
task.enabled = false
}
}
// call regular ndk-build(.cmd) script from app directory
}
//Modify the below set of code to the ndk-build.cmd location in your computer.
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile project(':openCVLibrary249')
}
I have already tried all possible solutions- deleting the obj folder in the build folder, trying to avoid automatic Android.mk call by setting the sourceSets.main, trying to avoid the compileDebugNdk task from getting called. I also do not have any cmake.txt files. I cannot seem to get over the problem.
I have used Android Studio 2.3.2 and 2.1.1 and the problem has persisted in both of them.
Any help would be appreciated.
You should use the latest Android Studio, 2.3.2 is OK. It has integrated externalNativeBuild in android gradle plugin, so you don't need the tricks with custom gradle task.
I could not actually test the build.gradle script below, so please forgive me any typos:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.anannyauberoi.testingcam"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
externalNativeBuild {
ndkBuild {
targets "app"
cppFlags "-std=c++11 -fexceptions"
arguments "APP_STL=gnustl_shared"
abiFilters "armeabi-v7a"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main {
res.srcDirs = ['src/main/res']
} }
externalNativeBuild {
ndkBuild {
path "src/main/jni/Android.mk"
}
}
//Modify the below set of code to the ndk-build.cmd location in your computer.
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.3.1'
compile project(':openCVLibrary249')
}
I went to:
C:\Users\Dev\AppData\Local\Android\Sdk\ndk-bundle\ndk-build.cmd
directory and for the ndk-build.cmd i press
Right-Click> Edit and change the cmd file from:
#echo off
%~dp0\build\ndk-build.cmd %*
to
#echo off
THAT works for me