Undefined reference when calling a OpenCL function in Android - android

I am trying to set a simple project in Android Studio with OpenCL. So I created a new project with NDK support and an empty activity. I have copied the libOpenCL.so from my mobile phone to the folder app\src\main\jniLibs\arm8 and I have downloaded the OpenCL headers from the Khronos webpage and copied them to app\src\main\jni\CL. Furthermore, I have done two modifications to the build.gradle file, I have added a parameter to the cppFlags to include the OpenCL folder and I have set an abi filter to build only for arm8. I copy my build.gradle file here:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "26.0.1"
defaultConfig {
applicationId "es.um.mompes.testopencl"
minSdkVersion 22
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++11 -fexceptions -I${projectDir}/src/main/jni"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
productFlavors {
arm8 {
ndk {
abiFilter "arm64-v8a"
}
}
}
}
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.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
I have also added a three lines to the generated native-lib.cpp file to test if OpenCL is working. This is my native-lib.cpp:
#include <jni.h>
#include <string>
#include <CL/cl.h>
extern "C"
JNIEXPORT jstring JNICALL
Java_es_um_mompes_testopencl_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
cl_int err;
// Find the number of OpenCL platforms
cl_uint num_platforms;
err = clGetPlatformIDs(0, NULL, &num_platforms);
return env->NewStringUTF(hello.c_str());
}
However, I must be missing something because I am getting an undefined reference error regarding the OpenCL function when I build the project. This is the complete error:
Execution failed for task ':app:externalNativeBuildArm8Debug'.
Build command failed.
Error while executing process C:\Users\Juan\AppData\Local\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build C:\Users\Juan\AndroidStudioProjects\TestOpenCL\app.externalNativeBuild\cmake\arm8Debug\arm64-v8a --target native-lib}
[1/1] Linking CXX shared library ........\build\intermediates\cmake\arm8\debug\obj\arm64-v8a\libnative-lib.so
FAILED: cmd.exe /C "cd . && C:\Users\Juan\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android --gcc-toolchain=C:/Users/Juan/AppData/Local/Android/sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/Juan/AppData/Local/Android/sdk/ndk-bundle/sysroot -fPIC -isystem C:/Users/Juan/AppData/Local/Android/sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=22 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -fexceptions -IC:\Users\Juan\AndroidStudioProjects\TestOpenCL\app/src/main/jni -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a --sysroot C:/Users/Juan/AppData/Local/Android/sdk/ndk-bundle/platforms/android-22/arch-arm64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ........\build\intermediates\cmake\arm8\debug\obj\arm64-v8a\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -llog -lm "C:/Users/Juan/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/arm64-v8a/libgnustl_static.a" && cd ."
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function Java_es_um_mompes_testopencl_MainActivity_stringFromJNI':
C:\Users\Juan\AndroidStudioProjects\TestOpenCL\app\src\main\cpp/native-lib.cpp:14: undefined reference toclGetPlatformIDs'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
Do you know what am I missing? I've searching for a few hours over the last two days about how to setup OpenCL in Android Studio but everything I find seems to be outdated and doesn't seem to work for my problem.

Look for Android.mk under jni folder. Then add this:
LOCAL_PATH := $(call my-dir) // should already be there
.
.
LOCAL_LDLIBS := $(LOCAL_PATH)/libOpenCL.so
This example takes the libOpenCL.so from the jni root but you can change this for your needs.
Reference: Getting Started with OpenCL on Android

I would recommend you replace the automatic Android Studio NDK building with your own so you have total control.
Then you can use #RonTLV answer to add the library to the Android.mk and have a proper build.
Android Studio by default ignores Android.mk and generates its own with all the .c/.cpp files in the jni folder.
sourceSets {
debug.jniLibs.srcDir 'src/main/obj/local' //debug builds have symbols
release.jniLibs.srcDir 'src/main/libs' //release builds have cleaned libraries
main.jni.srcDirs = [] //disable automatic ndk-build call with auto-generated Android.mk
}
// call regular ndk-build script manually
task ndkBuild(type: Exec) {
if (System.properties['os.name'].toLowerCase(Locale.ROOT).contains('windows')) {
commandLine 'ndk-build.cmd', '-j8', '-C', file('src/main').absolutePath
} else {
commandLine 'ndk-build', '-j8', '-C', file('src/main').absolutePath
}
}
//Make the NDK task depend on Java build Task
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}

Related

Android Studio JNI build Error with cmake

I want to build the JNI with android studio so that I can use .so file.
I used the Cmake for it but when I build the JNI, I got the built error.
My build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.ss.raynor.nativehttprequest"
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-std=c++14"
}
}
}
buildTypes {
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'
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'
}
Build command failed.
Error while executing process
E:\environment\sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {-HE:\Work\NativeHttpRequest\NativeHttpRequest\app\src\main\cpp -BE:\Work\NativeHttpRequest\NativeHttpRequest\app\.externalNativeBuild\cmake\debug\armeabi-v7a -DANDROID_ABI=armeabi-v7a -DANDROID_PLATFORM=android-18 -DCMAKE_LIBRARY_OUTPUT_DIRECTORY=E:\Work\NativeHttpRequest\NativeHttpRequest\app\build\intermediates\cmake\debug\obj\armeabi-v7a -DCMAKE_BUILD_TYPE=Debug -DANDROID_NDK=E:\environment\sdk\ndk-bundle -DCMAKE_CXX_FLAGS=-std=c++14 -DCMAKE_TOOLCHAIN_FILE=E:\environment\sdk\ndk-bundle\build\cmake\android.toolchain.cmake -DCMAKE_MAKE_PROGRAM=E:\environment\sdk\cmake\3.6.4111459\bin\ninja.exe -GAndroid Gradle - Ninja}
-- Check for working C compiler: E:/environment/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe
-- Check for working C compiler: E:/environment/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe -- broken
-- Configuring incomplete, errors occurred!
See also "E:/Work/NativeHttpRequest/NativeHttpRequest/app/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeOutput.log".
See also "E:/Work/NativeHttpRequest/NativeHttpRequest/app/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeError.log".
CMake Error at E:/environment/sdk/cmake/3.6.4111459/share/cmake-3.6/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler
"E:/environment/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/bin/clang.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: E:/Work/NativeHttpRequest/NativeHttpRequest/app/.externalNativeBuild/cmake/debug/armeabi-v7a/CMakeFiles/CMakeTmp
Run Build Command:"E:\environment\sdk\cmake\3.6.4111459\bin\ninja.exe"
"cmTC_2ada3"
[1/2] Building C object CMakeFiles/cmTC_2ada3.dir/testCCompiler.c.o
FAILED:
E:\environment\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang.exe
--target=armv7-none-linux-androideabi18
--gcc-toolchain=E:/environment/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64
--sysroot=E:/environment/sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot
-g -DANDROID -fdata-sections -ffunction-sections -funwind-tables
-fstack-protector-strong -no-canonical-prefixes -fno-addrsig -march=armv7-a
-mthumb -Wa,--noexecstack -Wformat -Werror=format-security -fPIE -o
CMakeFiles/cmTC_2ada3.dir/testCCompiler.c.o -c
E:\Work\NativeHttpRequest\NativeHttpRequest\app\.externalNativeBuild\cmake\debug\armeabi-v7a\CMakeFiles\CMakeTmp\testCCompiler.c
clang.exe: error: unknown argument: '-fno-addrsig'
ninja: build stopped: subcommand failed.
I think your error is related in NDK version.
I checked your build.gradle and there is no issue.
Please download the android-ndk-r16b and set the new NDK Location.
File / Project Structure / SDK Location
I hope it will work for you.
I have experienced in Buiding the JNI. You should try the following actions
Create the new project as Native C++.
Insert your code and try again.
This solved my issue

incompatible target while building apk for Native Android Project

I am trying to implement Alexa Voice Service SDK in Android.
I have pre-build libraries, which i am using in my Android Project.
I have .so files under src/main/jniLibs/armeabi-v7a/*.so
I can successfully compile and build apk for x86 platform , but my target is run it on armeabi-v7a device.
but while generating apk i am facing the incompatible target error.
the full error is shown below ,
FAILURE: Build failed with an exception.
What went wrong:
Execution failed for task ':app:externalNativeBuildDebug'.
> Build command failed.
Error while executing process /home/ajinkya/Androidsdk/cmake/3.10.2.4988404/bin/cmake with arguments {--build /root/AndroidStudioProjects/alexaProtoApp/app/.externalNativeBuild/cmake/debug/armeabi-v7a --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/native-lib.cpp.o
[2/2] Linking CXX shared library /root/AndroidStudioProjects/alexaProtoApp/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
FAILED: /root/AndroidStudioProjects/alexaProtoApp/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so
: && /home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi24 --gcc-toolchain=/home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -march=armv7-a -mthumb -Wa,--noexecstack -Wformat -Werror=format-security -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -Wl,-z,noexecstack -shared -Wl,-soname,libnative-lib.so -o /root/AndroidStudioProjects/alexaProtoApp/app/build/intermediates/cmake/debug/obj/armeabi-v7a/libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/../../main/jniLibs/armeabi-v7a/libSampleApp.so /home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/24/liblog.so /home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/24/liblog.so -latomic -lm && :
/home/ajinkya/Androidsdk/ndk-bundle/toolchains/llvm/prebuilt/linux-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/../../../../arm-linux-androideabi/bin/ld: error: /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/../../main/jniLibs/armeabi-v7a/libSampleApp.so: incompatible target
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:25: error: undefined reference to 'my_initialize(_JavaVM*, _jobject*)'
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:31: error: undefined reference to 'jni_wakeAlexa()'
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:37: error: undefined reference to 'jni_wakeWithHoldAlexa()'
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:43: error: undefined reference to 'jni_resetAlexa()'
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:49: error: undefined reference to 'jni_reauthorizeAlexa()'
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:55: error: undefined reference to 'jni_quitAlexa()'
/root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/native-lib.cpp:61: error: undefined reference to 'jni_infoAlexa()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.
My build.gradle like,
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
defaultConfig {
applicationId "my.project.vdi"
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
externalNativeBuild {
cmake {
cppFlags ""
// abiFilters "armeabi-v7a"
// arguments "-DANDROID_ARM_MODE=arm" , "-DANDROID_TOOLCHAIN=clang"
}
}
ndk {
// Specifies the ABI configurations of your native
// libraries Gradle should build and package with your APK. , x86 , armeabi-v7a
abiFilters "armeabi-v7a"
}
}
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"
}
}
sourceSets {
main {
jni.srcDirs = []
// let gradle pack the shared library into apk
jniLibs.srcDirs = ['src/main/jniLibs/']
}
}
// packagingOptions {
// exclude 'lib/arm64-v8a/libnative-lib.so'
// exclude 'lib/x86_64/libnative-lib.so'
// exclude 'lib/x86/libnative-lib.so'
// }
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.squareup.picasso:picasso:2.5.2'
}
ALso the CMakeLists.txt
cmake_minimum_required(VERSION 3.4.1)
add_library( # Sets the name of the library.
native-lib
SHARED
native-lib.cpp)
add_library(SampleApp SHARED IMPORTED)
set_target_properties(SampleApp PROPERTIES IMPORTED_LOCATION ${PROJECT_SOURCE_DIR}/../../main/jniLibs/${ANDROID_ABI}/libSampleApp.so)
find_library(
log-lib
log)
find_library(ANDROID_LOG_LIB log)
target_link_libraries(
native-lib
SampleApp
${ANDROID_LOG_LIB}
${log-lib} )
Please guide me resolving this issue.
.../armeabi-v7a/libSampleApp.so: incompatible target
That library isn't actually built for armeabi-v7a. The following should tell you what it is:
$ file /root/AndroidStudioProjects/alexaProtoApp/app/src/main/cpp/../../main/jniLibs/armeabi-v7a/libSampleApp.so
Here's example output from libc++_shared.so in the NDK:
$ file android-ndk-r21-canary/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libc++_shared.so
android-ndk-r21-canary/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/libc++_shared.so: ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV), dynamically linked, BuildID[sha1]=4dc00a833014c1ddd2e1ec64c5dd72237c4179ee, with debug_info, not stripped
The interesting part is:
ELF 32-bit LSB shared object, ARM, EABI5 version 1 (SYSV)
I got the solution, it is very unusual for me , the .so files which were generating while compiling, were not getting replaced. the old version of the files were not getting updated by the new ones. I deleted the folder and then compiled and the new files generated were the proper ones.

Build command failed:fatal error: 'string' file not found

I am trying to build and run the existing Android NDK project with latest Gradle. But I am getting below error while running the application.
Build command failed.
Error while executing process D:\rapiscan\Android\Sdk\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\rapiscan\codebase\NGC\NextGenConsole\app\.externalNativeBuild\cmake\debug\x86 --target UiDataProvider}
[1/4] Building CXX object UiDataProvider/CMakeFiles/UiDataProvider.dir/UiCmdHandler.cpp.o
[2/4] Building CXX object UiDataProvider/CMakeFiles/UiDataProvider.dir/UiDataProvider.cpp.o
[3/4] Building CXX object UiDataProvider/CMakeFiles/UiDataProvider.dir/UiMessageGenerator.cpp.o
FAILED: D:\rapiscan\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=i686-none-linux-android23 --gcc-toolchain=D:/rapiscan/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64 -DUiDataProvider_EXPORTS -IUiDataProvider -ID:/rapiscan/codebase/NGC/NextGenConsole/malibu/UiDataProvider -ID:/rapiscan/Android/Sdk/ndk-bundle/toolchains/llvm/prebuilt/windows-x86_64/sysroot/usr/include/arm-linux-androideabi -ID:/rapiscan/codebase/NGC/NextGenConsole/malibu/Common -pthread -O0 -fno-limit-debug-info -fPIC -std=gnu++14 -MD -MT UiDataProvider/CMakeFiles/UiDataProvider.dir/UiDataProvider.cpp.o -MF UiDataProvider\CMakeFiles\UiDataProvider.dir\UiDataProvider.cpp.o.d -o UiDataProvider/CMakeFiles/UiDataProvider.dir/UiDataProvider.cpp.o -c D:\rapiscan\codebase\NGC\NextGenConsole\malibu\UiDataProvider\UiDataProvider.cpp
In file included from D:\rapiscan\codebase\NGC\NextGenConsole\malibu\UiDataProvider\UiDataProvider.cpp:1:
D:/rapiscan/codebase/NGC/NextGenConsole/malibu/UiDataProvider/UiDataProvider.h:5:10: fatal error: 'string' file not found
`#include <string>`
1 error generated.
Below is the ndk configuration specified in app build.gradle
ndk {
ndk {
abiFilters 'armeabi-v7a', 'arm64-v8a','x86'
}
externalNativeBuild {
cmake {
arguments '-DCMAKE_BUILD_TYPE=Debug',"-DPROJECT_DIR:STRING=${mb_sdk_path}",'-DANDROID_PLATFORM=android-23', '-DANDROID_TOOLCHAIN=clang', '-DANDROID_ARM_NEON=TRUE', "-DPATH_TO_MALIBU:STRING=${mb_sdk_path}" ,"-DANDROID_STL=c++_static",
"-DTARGET_PLATFORM=Windows","-DUNIX=FALSE"
cppFlags "-std=c++14 -stdlib=libc++ -frtti -fexceptions"
}
}
}
and below is the build.gradle
buildscript {
repositories {
jcenter()
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.1'
classpath 'com.novoda:bintray-release:0.9'
}
}
I had the same problem with last version of gradle and cmake in my project, but I detected that only occurs when the Build Variants is wrong, for example the target is debug-armeabi-v7a but you are in a release compiling, please review the right variant target.
My settings:
NDK 19.1.5304403
CMake 3.10.2
build.gradle:
defaultConfig {
...
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
...
externalNativeBuild {
cmake {
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
...
NOTE: sometimes too you should remove the hidden directory rm -rf app/.externalNativeBuild for it works
Using -stdlib=libc++ with CMake is either redundant or broken on every NDK version. What you want is -DANDROID_STL=c++_shared (or -DANDROID_STL=c++_static for the static library, or nothing at all for the static library on NDK r18+ since that's the default).
Passing -stdlib=libc++ may cause the compiler to look in the wrong location on older NDK releases, and is the default on r19+.

How to resolve Android ndk build command faild?

I am developing simple ndk sample example using android ndk in android studio. While running my app studio shows below errors.
Build command failed.
Error while executing process D:\Sdk\cmake\3.6.4111459\bin\cmake.exe with
arguments {--build D:\Android Studio\Workspace\NDKSample\app\.externalNativeBuild\cmake\debug\arm64-v8a --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.oFAILED: D:\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android --gcc-toolchain=D:/Sdk/ndk-bundle/toolchains/aarch64-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=D:/Sdk/ndk-bundle/sysroot -Dnative_lib_EXPORTS -isystem D:/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include -isystem D:/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/arm64-v8a/include -isystem D:/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/include/backward -isystem D:/Sdk/ndk-bundle/sysroot/usr/include/aarch64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -frtti -fexceptions -O0 -fno-limit-debug-info -fPIC -MD -MT CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -MF CMakeFiles\native-lib.dir\src\main\cpp\native-lib.cpp.o.d -o CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o -c "D:\Android Studio\Workspace\NDKSample\app\src\main\cpp\native-lib.cpp"
error: error reading 'D:\Android Studio\Workspace\NDKSample\app\src\main\cpp\native-lib.cpp'
1 error generated.ninja: build stopped: subcommand failed.
I have search lot but not able to find proper answer that why I am asking here. I have also unintsall all the components and re-install on my studio still error is there.
Another thing If I change file extension to .C from .CPP whole project gets complied and even runs properly. I don't know why Its not working for .CPP file.
Components I am using.
Android Stuido - 3.1.3
gradle - 3.1.0
cmake - 3.6.4111459
Android ndk - 17.1.4828580
lldb -3.1
native-lib.cpp
#include <jni.h>#include <string>extern "C" JNIEXPORT jstringJNICALLJava_com_mastek_ndksample_MainActivity_stringFromJNI(
JNIEnv *env,
jobject /* this */) {
std::string hello = "Hello from C++";
return env->NewStringUTF(hello.c_str());}
build.gradle
apply plugin: 'com.android.application'android { compileSdkVersion 27
defaultConfig {
applicationId "com.example.ndksample"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
}
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(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
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'}
Anyone know please help me to solve this problem. Thanks in advance.
NDK build fails miserably when the path to project files contains spaces. To fix your build, copy the project to path without spaces.
After lot of trials and help from github google guy I am able to solve. To resolve problem I have follow below steps.
1.go to project directory.
2.deleted .gradle ,app/.externalNativeBuild and app/build.
3.changed local.properties ndk path to external ndk-r16b.
4.Run through Android studio.
5.Finally apk is generate.

ndk-build.cmd failed during compilation (opencv face detect)

I know there was a lot of similar topics but none of them helped and belive I'm trying to run this for over 2 days.
I want to run face detection sample and I'm stuck on ndk build
Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-23
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include C:/Users/dpach/OpenCV-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := DetectionBasedTracker_jni.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)
app.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "threewe.testopencv"
minSdkVersion 21
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs'
jni.srcDirs = [] //disable automatic ndk-build call
}
task ndkBuild(type: Exec) {
commandLine 'C:\\Users\\dpach\\android-ndk-r12\\ndk-build.cmd', '-C', file('jni').absolutePath
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha2'
compile project(':libraries:opencv')
}
error
System nie moľe odnale«† okre?lonej ?cieľki.
make: *** [C:/Users/dpach/AndroidStudioProjects/TestOpenCv/app/obj/local/armeabi-v7a/objs/detection_based_tracker/DetectionBasedTracker_jni.o] Error 1
make: Leaving directory `C:/Users/dpach/AndroidStudioProjects/TestOpenCv/app/jni'
:app:ndkBuild FAILED
Error:Execution failed for task ':app:ndkBuild'.
> Process 'command 'C:\Users\dpach\android-ndk-r12\ndk-build.cmd'' finished with non-zero exit value 2
Translation of the error: 'System cannot find specified path'
I've checked the path and it exists I even put there file from another project but the result is the same. I've tried also to run ndk-build.cmd manualy from the console but the result is the same so this is not Android Studio issue. I have no idea what more I can do :(
// EDIT
added V=1 parameter
make: Entering directory `C:/Users/dpach/AndroidStudioProjects/TestOpenCv/app/jni'
[armeabi-v7a] "Compile++ thumb": "detection_based_tracker <= DetectionBasedTracker_jni.cpp"
C:/Users/dpach/android-ndk-r12/build//../toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++ -MMD -MP -MF C:/Users/dpach/AndroidStudioProjects/TestOpenCv/app/obj/local/armeabi-v7a/objs/detection_based_tracker/DetectionBasedTracker_jni.o.d -fpic -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -g -march=armv7-a -mfpu=vfpv3-d16 -mfloat-abi=softfp -fno-exceptions -fno-rtti -mthumb -Os -DNDEBUG -I"C:/Users/dpach/OpenCV-android-sdk/sdk/native/jni/include/opencv" -I"C:/Users/dpach/OpenCV-android-sdk/sdk/native/jni/include" -IC:/Users/dpach/AndroidStudioProjects/TestOpenCv/app/jni -IC:/Users/dpach/android-ndk-r12/build//../sources/cxx-stl/gnu-libstdc++/4.9/include -IC:/Users/dpach/android-ndk-r12/build//../sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include -IC:/Users/dpach/android-ndk-r12/build//../sources/cxx-stl/gnu-libstdc++/4.9/include/backward -IC:/Users/dpach/AndroidStudioProjects/TestOpenCv/app/jni -DANDROID -fPIC -DANDROID -fsigned-char -Wa,--noexecstack -Wformat -Werror=format-security -frtti -fexceptions -isystem C:/Users/dpach/android-ndk-r12/build//../platforms/android-23/arch-arm/usr/include -c C:/Users/dpach/AndroidStudioProjects/TestOpenCv/app/jni/DetectionBasedTracker_jni.cpp -o C:/Users/dpach/AndroidStudioProjects/TestOpenCv/app/obj/local/armeabi-v7a/objs/detection_based_tracker/DetectionBasedTracker_jni.o
This is a bug on the download page of NDK, see the issue yours truly opened.
You can use the wiki page on GitHub that is updated more frequently.
In your specific case, your are using 32-bit version on your 64-bit Windows, which is suboptimal. You can download the latest stable version r12b for your OS.

Categories

Resources