I have an android project configured in eclipse that works good with ant and ndk-build with some customized Android.mk and Application.mk files. I need to move them to Android Studio and I don't want to use CMake that comes with it, so I just want to keep my old .mk files. To do so I started with a simple hello world example with the following code:
Here the native-lib.cpp:
#include <jni.h>
#include <string>
extern "C"
{
jstring Java_com_comscore_android_app_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++ lalalaaaaaa";
return env->NewStringUTF(hello.c_str());
}
}
and here my gradle file:
android {
compileSdkVersion 23
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.comscore"
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
ndkBuild {
arguments "NDK_APPLICATION_MK:=src/main/jni/Application.mk"
}
}
}
externalNativeBuild{
ndkBuild{
path "src/main/jni/Android.mk"
}
}
...
}
Here the Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_MODULE := native-lib
LOCAL_LDLIBS := -llog
LOCAL_CPPFLAGS += -fsigned-char -fexceptions -frtti -g -O0 -std=c++0x -std=gnu++0x
LOCAL_CFLAGS += -fsigned-char -fexceptions -frtti -g -O0 -std=c++0x -std=gnu++0x
include $(BUILD_SHARED_LIBRARY)
And finally the Application.mk:
APP_ABI := all
APP_STL := gnustl_static
But the C code is never compiled, could any body help me with this issue?
Thanks!
it was not compiling anything, that was the problem. Now I solved after cleaning up the build folder manually
Related
i am trying to use android ndk/jni but i get this exception when ever i call stringFromJNI();
java.lang.UnsatisfiedLinkError: dlopen failed: "/data/app/com.raya.aman-
A_B2z5nEzTvL2uOtrxLexQ==/lib/arm/libnljnitest.so" has unsupported e_shentsize: 0x9 (expected 0x28)
this is where i call the native library
public class ToolModule {
public native String stringFromJNI();
static {
System.loadLibrary("nljnitest");
}
}
android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := nljnitest
LOCAL_C_INCLUDES += $(LOCAL_PATH)/
LOCAL_SRC_FILES += FieldTest.c log.c
LOCAL_LDLIBS += -llog
include $(BUILD_SHARED_LIBRARY)
Application.mk
NDK_TOOLCHAIN_VERSION := 4.9
APP_ABI := armeabi-v7a arm64-v8a
APP_CFLAGS += -Wno-error=format-security
i added this to build.gradle
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
Looks like the system were looking for libnljnitest.so in armeabi folder which actually doesn't have such library. Have you tried to filter supported ABIs following instructions here https://medium.com/mobiwise-blog/unsatisfiedlinkerror-problem-on-some-android-devices-b77f2f83837d
In your case, the filter might look like this.
defaultConfig {
ndk {
abiFilters "arm64-v8a", "armeabi-v7a"
}
}
My app run perfectly on 32bit android devices when I try to run on 64bit devices
its gives me error on build time.
Error:
Build command failed.
Error while executing process D:\Software\sdk\NDK\android-ndk-r17\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=E:\Projects\TestProjects\VirtualApp-master\VirtualApp\lib\src\main\jni\Android.mk NDK_APPLICATION_MK=E:\Projects\TestProjects\VirtualApp-master\VirtualApp\lib\src\main\jni\Application.mk APP_ABI=arm64-v8a NDK_ALL_ABIS=arm64-v8a NDK_DEBUG=1 APP_PLATFORM=android-21 NDK_OUT=E:/Projects/TestProjects/VirtualApp-master/VirtualApp/lib/build/intermediates/ndkBuild/debug/obj NDK_LIBS_OUT=E:\Projects\TestProjects\VirtualApp-master\VirtualApp\lib\build\intermediates\ndkBuild\debug\lib E:/Projects/TestProjects/VirtualApp-master/VirtualApp/lib/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/libva++.so}
Android NDK: WARNING: APP_STL gnustl_static is deprecated and will be removed in the next release. Please switch to either c++_static or c++_shared. See https://developer.android.com/ndk/guides/cpp-support.html for more information.
Android NDK: WARNING: Ignoring unknown import directory: :E:/Projects/TestProjects/VirtualApp-master/VirtualApp/lib/src/main/jni
Android NDK: WARNING:E:/Projects/TestProjects/VirtualApp-master/VirtualApp/lib/src/main/jni/fb/Android.mk:fb: LOCAL_LDLIBS is always ignored for static libraries
[arm64-v8a] Compile++ : va++ <= IOUniformer.cpp
E:/Projects/TestProjects/VirtualApp-master/VirtualApp/lib/src/main/jni/Foundation/IOUniformer.cpp:130:23: error: use of undeclared identifier '__NR_chmod'
int ret = syscall(__NR_chmod, redirect_path, mode);
^
E:/Projects/TestProjects/VirtualApp-master/VirtualApp/lib/src/main/jni/Foundation/IOUniformer.cpp:140:23: error: use of undeclared identifier '__NR_fstatat64'
int ret = syscall(__NR_fstatat64, dirfd, redirect_path, buf, flags);
^
E:/Projects/TestProjects/VirtualApp-master/VirtualApp/lib/src/main/jni/Foundation/IOUniformer.cpp:149:23: error: use of undeclared identifier '__NR_fstatat64'
int ret = syscall(__NR_fstatat64, dirfd, redirect_path, buf, flags);
^
E:/Projects/TestProjects/VirtualApp-master/VirtualApp/lib/src/main/jni/Foundation/IOUniformer.cpp:159:23: error: use of undeclared identifier '__NR_fstat64'
int ret = syscall(__NR_fstat64, redirect_path, buf);
^
....
....
...
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [E:/Projects/TestProjects/VirtualApp-master/VirtualApp/lib/build/intermediates/ndkBuild/debug/obj/local/arm64-v8a/objs/va++/Foundation/IOUniformer.o] Error 1
I have build app on 64bit device using following steps.
Add "arm64-v8a" in abiFilters
update APP_ABI in Application.mk
but
Android.mk
LOCAL_PATH := $(call my-dir)
MAIN_LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := va++
LOCAL_CFLAGS := -Wno-error=format-security -fpermissive -DLOG_TAG=\"VA++\"
LOCAL_CFLAGS += -fno-rtti -fno-exceptions
LOCAL_C_INCLUDES += $(MAIN_LOCAL_PATH)
LOCAL_C_INCLUDES += $(MAIN_LOCAL_PATH)/Foundation
LOCAL_C_INCLUDES += $(MAIN_LOCAL_PATH)/Jni
LOCAL_SRC_FILES := Jni/VAJni.cpp \
Foundation/IOUniformer.cpp \
Foundation/VMPatch.cpp \
Foundation/SymbolFinder.cpp \
Foundation/Path.cpp \
Foundation/SandboxFs.cpp \
Substrate/hde64.c \
Substrate/SubstrateDebug.cpp \
Substrate/SubstrateHook.cpp \
Substrate/SubstratePosixMemory.cpp \
LOCAL_LDLIBS := -llog -latomic
LOCAL_STATIC_LIBRARIES := fb
include $(BUILD_SHARED_LIBRARY)
include $(MAIN_LOCAL_PATH)/fb/Android.mk
LOCAL_SHORT_COMMANDS := true
Application.mk
APP_ABI := arm64-v8a
APP_PLATFORM := android-24
APP_STL := gnustl_static
APP_OPTIM := release
VA_ROOT := $(call my-dir)
NDK_MODULE_PATH := $(NDK_MODULE_PATH):$(VA_ROOT)
Gradle
apply plugin: 'com.android.library'
android {
compileSdkVersion 27
buildToolsVersion '27.0.3'
defaultConfig {
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
externalNativeBuild {
ndkBuild {
abiFilters "armeabi-v7a", "x86","arm64-v8a"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
ndkBuild {
path file("src/main/jni/Android.mk")
}
}
lintOptions {
//IJobService need NewApi
warning 'NewApi','OnClick'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
}
[CXX1405] error when building with ndkBuild using C:\Users\IT RENTAL\AndroidStudioProjects\appcloner\lib\src\main\jni\Android.mk: Build command failed.
Error while executing process C:\Users\IT RENTAL\AppData\Local\Android\Sdk\ndk\21.4.7075529\ndk-build.cmd with arguments {NDK_PROJECT_PATH=null APP_BUILD_SCRIPT=C:\Users\IT RENTAL\AndroidStudioProjects\appcloner\lib\src\main\jni\Android.mk NDK_APPLICATION_MK=C:\Users\IT RENTAL\AndroidStudioProjects\appcloner\lib\src\main\jni\Application.mk APP_ABI=arm64-v8a NDK_ALL_ABIS=arm64-v8a NDK_DEBUG=1 APP_PLATFORM=android-16 NDK_OUT=C:\Users\IT RENTAL\AndroidStudioProjects\appcloner\lib\build\intermediates\cxx\Debug\591uu4n4/obj NDK_LIBS_OUT=C:\Users\IT RENTAL\AndroidStudioProjects\appcloner\lib\build\intermediates\cxx\Debug\591uu4n4/lib APP_SHORT_COMMANDS=false LOCAL_SHORT_COMMANDS=false -B -n}
'C:\Users\IT' is not recognized as an internal or external command,
operable program or batch file.
any idea on this issue ?
We have taken clone of a project and made all settings as per instruction still not able to build app due to following error:
Error:(7, 10) fatal error: 'gst/gst.h' file not found
above error is in tutorial.c file:
#include <gst/gst.h>
#include <gst/video/video.h>
#include <gst/video/videooverlay.h>
#include <pthread.h>
GST_DEBUG_CATEGORY_STATIC (debug_category);
#define GST_CAT_DEFAULT debug_category
Please find below gradle and Android.mk file which I have added to my project:
Gradle
android {
compileSdkVersion 25
buildToolsVersion "25.0.0"
defaultConfig {
applicationId "com.example.ndktest"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
arguments '-DANDROID_TOOLCHAIN=clang',
'-DANDROID_PLATFORM=android-19',
'-DANDROID_STL=gnustl_static',
'-DANDROID_ARM_NEON=TRUE',
'-DANDROID_CPP_FEATURES=exceptions rtti'
}
}
ndk {
moduleName "tutorial-5"
}
}
sourceSets.main {
jni.srcDirs = []
jniLibs.srcDir new File(buildDir, 'lib')
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
externalNativeBuild {
cmake {
path "CMakeLists.txt"
}
}
}
Android.mk
LOCAL_PATH := $(call my-dir)
GSTREAMER_ROOT_ANDROID := D:\ndk-gst1.9.1
SHELL := PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin /bin/bash
include $(CLEAR_VARS)
LOCAL_MODULE := tutorial-5
LOCAL_SRC_FILES := tutorial-5.c
LOCAL_SHARED_LIBRARIES := gstreamer_android
LOCAL_LDLIBS := -llog -landroid
include $(BUILD_SHARED_LIBRARY)
ifndef GSTREAMER_ROOT
ifndef GSTREAMER_ROOT_ANDROID
$(error GSTREAMER_ROOT_ANDROID is not defined!)
endif
GSTREAMER_ROOT := $(GSTREAMER_ROOT_ANDROID)
endif
GSTREAMER_NDK_BUILD_PATH := $(GSTREAMER_ROOT)/share/gst-android/ndk-build
include $(GSTREAMER_NDK_BUILD_PATH)/plugins.mk
GSTREAMER_PLUGINS := $(GSTREAMER_PLUGINS_CORE) $(GSTREAMER_PLUGINS_PLAYBACK) $(GSTREAMER_PLUGINS_CODECS) $(GSTREAMER_PLUGINS_NET) $(GSTREAMER_PLUGINS_SYS) $(GSTREAMER_PLUGINS_CODECS_RESTRICTED)
G_IO_MODULES := gnutls
GSTREAMER_EXTRA_DEPS := gstreamer-video-1.0
include $(GSTREAMER_NDK_BUILD_PATH)/gstreamer-1.0.mk
Please suggest what changes need to be done in above code to make it run.
I already answered a similar question... The problem with the official tutorials from the Gstreamer site is that they are old and made for Eclipse. Maybe your error is because you didn't link your Android Studio project to the C++ files... to do it follow the second step from this answer: Gstreamer examples in Android Studio
Since the linking with C++ files ins't the only error that will occurs I also suggest that you follow all other steps. There's a link for gitlab project with "turorial 5" working in the link above.
I'm trying to reach a break point in a simple C++ code:
Here the .cpp
#include <jni.h>
#include <string>
extern "C"
{
jstring Java_com_comscore_android_app_MainActivity_stringFromJNI(
JNIEnv* env,
jobject /* this */) {
std::string hello = "Hello from C++ lalalaaaaaa";
return env->NewStringUTF(hello.c_str());
}
}
here the gradle file:
android {
compileSdkVersion 23
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.comscore"
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
ndkBuild {
arguments "NDK_APPLICATION_MK:=src/main/jni/Application.mk"
}
}
}
externalNativeBuild{
ndkBuild{
path "src/main/jni/Android.mk"
}
}
...
}
The Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_MODULE := native-lib
LOCAL_LDLIBS := -llog
LOCAL_CPPFLAGS += -fsigned-char -fexceptions -frtti -g -O0 -std=c++0x -std=gnu++0x
LOCAL_CFLAGS += -fsigned-char -fexceptions -frtti -g -O0 -std=c++0x -std=gnu++0x
include $(BUILD_SHARED_LIBRARY)
And the Application.mk
APP_ABI := all
APP_STL := gnustl_static
The application compiles and works but I'm not able to stop in any breakpoint in the C++ code while running the debugger. I can see how it loads the native libraries but it doesn't stop any where and Android studio is telling me that the break point has been attached.
I'm using Android Studio 2.2 Preview 6
Can anybody help me?
In my case I have two modules, one with the C++ code (library) and an other one with the application that consumes the library module.
The problem was that I tried to debug the library module uaing the app module, so I just need to specify the folder where the debug symbols are:
Run->Edit configurations... -> Debuggerand in the symbol directories tab add the right path. For instance:
/path_to_my_project/lib_module/build/intermediates/ndkBuild/flavor/debug/obj/local/x86
app.imp file contains "<"facet type="native_android_gradle">", , option=SELECTED_BUILD_VARIANT..
check if value="debug"
I try to build project with OpenCV, i downloaded OpenCV SDK 3.1 from official website, however during building i get error
/Users/Mario/Downloads/OpenCV-android-sdk/sdk/native/jni/include/opencv2/core/mat.inl.hpp
Error:(571) undefined reference to 'cv::fastFree(void*)'
Error:(663) undefined reference to 'cv::Mat::create(int, int const*, int)'
Error:(682) undefined reference to 'cv::Mat::deallocate()'
here is my gradle.build, i use gradle-experimental:0.4.0
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.2"
defaultConfig.with {
applicationId = "pl.mariusz.opencv"
minSdkVersion.apiLevel = 17
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
task ndkBuild(type: Exec) {
commandLine '/Users/Mario/Library/Android/android-ndk-r10e/ndk-build', '-C', file('src/main').absolutePath
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
android.ndk {
moduleName = "source_file"
cppFlags.add("-std=c++11")
cppFlags.add("-fexceptions")
cppFlags.add("-I${file("/Users/Mario/Downloads/OpenCV-android-sdk/sdk/native/jni/include")}".toString())
cppFlags.add("-I${file("/Users/Mario/Downloads/OpenCV-android-sdk/sdk/native/jni/include/opencv")}".toString())
stl = "gnustl_static"//"gnustl_static"//"gnustl_shared"//"stlport_static"
}
android.sources {
main {
jni{
source{
srcDirs = []
}
}
jniLibs {
source {
srcDirs = ['src/main/Libs']
}
}
}
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles.add(file('proguard-rules.txt'))
}
}
}
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCVROOT:= /Users/Mario/Downloads/OpenCV-android-sdk
OPENCV_LIB_TYPE=STATIC
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := source_file.cpp
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := source_file
include $(BUILD_SHARED_LIBRARY)
and Application.mk
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-17
i tried mix it with shared,static STL with still same error, how can i fix it?
[EDIT]
[SOLUTION]
its more like compromise not real solution, but i changed gradle-experimental:0.4.0 back to classpath 'com.android.tools.build:gradle:1.5.0' and added file gradle.properties to project root directory with
android.useDeprecatedNdk=true
You have to add opencv libraries to your linker. To do that, you have to do 2 things: 1. add the library files; 2. add the library directory.
Try
LOCAL_LDLIBS += -llog -ldl -LlibPath -lopencv_calib3d -lopencv_core
and so an, add all the opencv libraries (or at least the ones you'll need) in this way. -l will automatically assume the "lib" in the filename and the suffix.
The -L will tell the linker where to find libraries, so instead of libPath please add the absolute path of your opencv library folder. See how to mention path of libraries in Android.mk file or Application.mk file?
Try it and tell me the new error message.