To run some Native code you can execute ndk-build command but I am confused if the Eclipse or Android Studio IDEs execute this command automatically on the Compile-time or how ndk-build command is related to the mentioned IDEs when I compile my code by eclipse or droid studio?
For eclipse, there are already too many online sources related to it, so I will talk about Android Studio here.
If you don't have your onw native files (C++,C) in your project, just put the .a or .so native libraries in the src/main/jniLibs folders and you don't have to modify anything in the gradle file. The system will do everything for you automatically.
If you have your own native files and have put them in the src/main/jni folder, then you have to create your own makefiles and put them in the jni folder as well. You also have to modify the gradle file in your app module.
Here is what I did for a face detection sample of opencv, and I got the original code from this wonderful post(Here), which was actually modified from another one's method for simpler execution. The philosophy is rather simple: compile the native source codes and put the generated .so/.a library into the jniLibs folder:
My project structure:
Project Structure
My makefiles (make sure you have the original sdk of the library for reference in the makefile):
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include /home/ng/Desktop/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)
Application.mk:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a armeabi
APP_PLATFORM := android-19
My gradle.build file in the app module:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "org.opencv.samples.facedetect"
minSdkVersion 14
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
sourceSets.main.jni.srcDirs = []
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
// ndkDir = project.plugins.findPlugin('com.android.application').getNdkFolder()
commandLine "$ndkDir/ndk-build",
'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
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile project(':openCVLibrary300')
}
You don't actually have to change other gradle files for compiling the native part of the project.
You could also compile the native sources codes by command line and put the .so files back to the jniLibs folder. This also works.
I hope this could help in your problem.
If you want you could compile it using Terminal commands or else you could configure both the IDEs to do automatically
Today Android-Studio (Canary) Preview 1.3 RC1 got released with jni/ndk support out of the box. No need to call commandline tools anymore.
https://sites.google.com/a/android.com/tools/download/studio/canary/latest
Release Notes:
https://sites.google.com/a/android.com/tools/recent/androidstudiowithandroidndkpreviewsupportavailable
Related
I'm using NDK to use C++ native code in OpenCV through Android studio.
Every thing is alright except when I'm running this project to an android device I have the following error
Warning:Native C/C++ source code is found, but it seems that NDK option is not configured. Note that if you have an Android.mk, it is not used for compilation. The recommended workaround is to remove the default jni source code directory by adding:
android {
sourceSets {
main {
jni.srcDirs = []
}
}
}
and also
to build.gradle, manually compile the code with ndk-build, and then place the resulting shared object in src/main/jniLibs.
C:\Users\MALIK\Desktop\ImageJ\app\src\main\jni\com_example_malik_imagej_DetectionBasedTracker.cpp
Error:(2, 10) 'opencv2/opencv.hpp' file not found 1 error generated.
and when I do the workaround and make jni.srcDir empty , I got error that "cannot resolve corresponding JNI function "
so the native code won't run and the app crashes , what should I do in this case?
this is my gradle :
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.malik.haar"
minSdkVersion 16
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "C:/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
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { jni.srcDirs = [] } }
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.1.1'
androidTestCompile files('libs/junit-4.12.jar')
compile project(':openCVLibrary320')
}
and this is my Android.mk file :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCVROOT:=C:\OpenCV-android-sdk
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include $(OPENCVROOT)/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := com_example_malik_imagej_DetectionBasedTracker.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)
and this is my Header file of native code :
thanks.
I am trying to write a C++ file in Android Studio and I would like to use OpenCV in my project.
However, when I try to use the following includes, I get an error saying Cannot Find 'opencv2'.
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
To set up OpenCV in Android, you have to take the following steps, found here, to use the Java part of OpenCV in Android Studio.
To the best of my knowledge to use the C++ part of OpenCV in Android Studio, you have to add Android.mk and Application.mk to the folder containing your native code.
My Android.mk file looks like the following.
LOCAL_PATH := $(call my-dir)
CVROOT := C:/Users/Dan/Documents/Repos/Android-Studio/Assets/opencv-3.4.1-android-sdk/OpenCV-android-sdk/sdk/native/jni
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include $(CVROOT)/OpenCV.mk
LOCAL_MODULE := app
LOCAL_SRC_FILES := native-lib.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
My Application.mk file looks like the following.
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-27
Am I doing something wrong to set my project up to use the C++ part of OpenCV on Android?
Additional Information for if it makes a difference
My project outline looks like this.
My gradles look like the following
app module
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.test.test.esra"
minSdkVersion 24
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
}
}
}
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.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
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'
// Room - Managing local databases
implementation 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
//Jetbrains Annotations
implementation 'org.jetbrains:annotations-java5:15.0'
implementation project(':openCVLibrary341')
}
openCVLibrary341 module
apply plugin: 'com.android.library'
android {
compileSdkVersion 27
defaultConfig {
minSdkVersion 24
targetSdkVersion 27
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
project build.gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
For anyone else who arrives here with the same issue, the problem is fairly easy to fix.
You no longer need the .mk files so they can be deleted.
If you are starting a project from scratch, make sure you have Android NDK and CMake installed as well as enabling them for your project.
After following the initial steps posted in this answer to set up the Java part of OpenCV on Android. You will need to change your app module's build gradle to reflect the snippet below.
defaultConfig {
...
externalNativeBuild {
cmake {
cppFlags "-frtti -fexceptions"
abiFilters 'x86', 'x86_64', 'armeabi', 'armeabi-v7a', 'arm64-v8a', 'mips', 'mips64'
}
}
}
sourceSets {
main {
jniLibs.srcDirs = ['src/main/jniLibs']
}
}
Then you want to edit the file CMakeLists.txt. In this file you want to include the following code underneath the cmake_minimum_required(VERSION X.X.X) line.
Also, you will need to add lib_opencv to the target_link_libraries near the bottom of CMakeLists.txt. This will help prevent undefined reference errors.
include_directories(your-path-to/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)
...
target_link_libraries( # Specifies the target library.
native-lib
# OpenCV lib
lib_opencv
# Links the target library to the log library
# included in the NDK.
${log-lib} )
Make sure to replace your-path-to with your actual path to OpenCV for Android.
Finally, clean your project and refresh your linked C++ projects. This will remove the error.
I got this information from this really nice GitHub page. Credits to leadrien
Clarification on You no longer need the .mk files. This is true for Android Studio 3. I have not tested this on older versions of Android Studio or Eclipse, so you might need .mk files for those IDEs.
I am trying to run a demo application of a Library in which android ndk is integrated. I have imported this code in Android studio and also downloaded ndk and linked it with project. Code compiles and build succesfully. It gives crash with exception "exception ljava lang unsatisfiedlinkerror thrown while initializing" "failed: dlopen failed: cannot locate symbol "_ZN7Tangram11setPositionEdd" referenced by "libtangram.so"..."
Application.mk:
APP_STL := c++_shared
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi armeabi-v7a x86 mips
APP_PLATFORM := android-19
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := tangram
LOCAL_SRC_FILES := jniExports.cpp jniGenerated.cpp platform_android.cpp
LOCAL_LDLIBS := -llog
LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
include $(BUILD_SHARED_LIBRARY)
Module Gradle File:
buildscript {
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.github.dcendents:android-maven-plugin:1.2'
}
}
apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'
group = GROUP
version = VERSION_NAME
android {
compileSdkVersion 22
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
}
sourceSets.main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
jni.srcDirs = []
assets.srcDirs = ['core/resources']
}
task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
commandLine "C:/Users/Administrator/AppData/Local/Android/android-ndk-r10e/ndk-build.cmd",
'NDK_PROJECT_PATH=build/intermediates/ndk',
'NDK_LIBS_OUT=jniLibs',
'APP_BUILD_SCRIPT=jni/Android.mk',
'NDK_APPLICATION_MK=jni/Application.mk'
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
}
// Add gdb server to apk
afterEvaluate {
Sync packageTask = project.getTasks().findByName("packageReleaseJniLibs")
if (packageTask) { packageTask.include(['**/gdbserver', '**/gdb.setup']) }
packageTask = project.getTasks().findByName("packageDebugJniLibs")
if (packageTask) { packageTask.include(['**/gdbserver', '**/gdb.setup']) }
}
dependencies {
compile 'com.squareup.okhttp:okhttp:2.5.0'
compile 'xmlpull:xmlpull:1.1.3.1'
}
apply from: file('gradle-mvn-push.gradle')
In Java class load libraries:
static {
System.loadLibrary("c++_shared");
System.loadLibrary("tangram");
}
I google this issue and in these posts Link1 Link2 Link3 Link4 it is mentioned that it may be the issue of compiling at api 21 or newer and running it on older devices. But i have mentioend APP_PLATFORM := android-19 in my Application.mk and getting this exception.
Any Help?
It looks like you tried to compile the tangram project from your own custom ndk-build as opposed to their recommended build process (https://github.com/tangrams/tangram-es#android). Use their build process with make to create the libtangram.so file, and then copy that .so file into your native libraries directory for your app.
The reason it can't find those symbols is because you aren't including the appropriate source files that define those functions when building with the ndk. By ingnoring undefined symbols, it will compile but won't be able to resolve them at runtime.
The symbol you are missing is created from Tangram::setPosition(double, double) which is defined at https://github.com/tangrams/tangram-es/blob/master/core/src/tangram.cpp#L318 ; however, your Android.mk file doesn't include that source file.
I have an android application which is been using OpenGL libraries and PVRTC Tool.
This application was running perfectly on Eclipse till API 19. Now I want to move this till API 23 so think to port the application to Android Studio as well for future updates sync. I am facing problems in this porting please look up the screenshots to see the folder hierarchy I have in Eclipse running code
[
[
Following was my Android.mk file:
LOCAL_PATH := $(call my-dir)
PVRSDKDIR := $(LOCAL_PATH)
include $(PVRSDKDIR)/Tools/OGLES2/Build/Android/Android.mk
include $(CLEAR_VARS)
LOCAL_MODULE := nativeegl
LOCAL_CFLAGS := -DBUILD_OGLES2 -Wall -g
LOCAL_SRC_FILES := CCAppMain.cpp
LOCAL_LDLIBS := -llog -landroid -lEGL -lGLESv2# -lGLESv1_CM
LOCAL_STATIC_LIBRARIES := \
ogles2tools \
android_native_app_glue
LOCAL_C_INCLUDES := \
$(PVRSDKDIR)/Builds/OGLES2/Include \
$(PVRSDKDIR)/Tools \
$(PVRSDKDIR)/Tools/OGLES2
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
And this is respective Application.mk file in Eclipse
# The ARMv7 is significanly faster due to the use of the hardware FPU
#APP_ABI := armeabi armeabi-v7a x86
#APP_OPTIM := release
APP_STL := stlport_static
I am using Android Studio Version 1.4 , Gradle Built 2.5
have set classpath as "classpath 'com.android.tools.build:gradle-experimental:0.2.0' "
My app module built.gradle looks like
apply plugin: 'com.android.model.application'
model {
android {
compileSdkVersion = 23
buildToolsVersion = "23.0.1"
defaultConfig.with {
applicationId = "xxx"
minSdkVersion.apiLevel = 14
targetSdkVersion.apiLevel = 23
versionCode = 1
versionName = "1.0"
}
}
compileOptions.with {
sourceCompatibility=JavaVersion.VERSION_1_7
targetCompatibility=JavaVersion.VERSION_1_7
}
android.ndk {
moduleName = "nativeegl"
//cppFlags += "-I${file("src/main/jni/native_app_glue")}".toString()
cppFlags += "-I${file("src/main/jni")}".toString()
cppFlags += "-I${file("src/main/jni/Tools")}".toString()
cppFlags += "-I${file("src/main/jni/Tools/OGLES2")}".toString()
cppFlags += "-I${file("src/main/jni/Tools/OGLES2/GLES2")}".toString()
ldLibs += ["android", "EGL", "GLESv2", "OpenSLES", "log"]
stl = "stlport_static"
}
android.buildTypes {
release {
minifyEnabled = false
proguardFiles += file('proguard-rules.txt')
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
compile 'com.android.support:support-v4:23.0.1'
}
I am really not able to configure the application at moment, On syncing it always giving errors such as
Error:Execution failed for task ':app:compileArmeabi-v7aDebugNativeeglSharedLibraryNativeeglMainCpp'.
> Multiple build operations failed.
C++ compiler failed while compiling PVRTPrint3D.cpp.
C++ compiler failed while compiling PVRTBackground.cpp.
C++ compiler failed while compiling PVRTShader.cpp.
C++ compiler failed while compiling PVRTPrint3DAPI.cpp.
C++ compiler failed while compiling PVRTPFXParserAPI.cpp.
See the complete log at: file:///E:/Android_Studio_Project/XXX/app/build/tmp/compileArmeabi-v7aDebugNativeeglSharedLibraryNativeeglMainCpp/output.txt
Can anyone look into it and make correction to my build.gradle file. I am guessing that PVRTC tool is been ignored in it.
Any help will be appreciated.
You are not obliged to forsake your carefully crafted Android.mk for the unpolished and limited DSL of the experimental gradle plugin. See more at define LOCAL_SRC_FILES in ndk{} DSL.
The specific problem in your case seems to be not that the PVRTC files are ignored, but that they are not compiled according to the rules and flags as configured in Tools/OGLES2/Build/Android/Android.mk.
TL;NR: append the following lines to you build.gradle:
def ndkBuild = android.ndkDirectory
import org.apache.tools.ant.taskdefs.condition.Os
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
ndkBuild += '.cmd'
}
task buildNative(type: Exec, description: 'Compile JNI source via NDK') {
commandLine '$ndkBuild', '-C', 'src/main'
}
task cleanNative(type: Exec, description: 'Clean JNI object files') {
commandLine '$ndkBuild', 'clean', '-C', 'src/main'
}
clean.dependsOn 'cleanNative'
tasks.all {
task ->
if (task.name.startsWith('compile') && task.name.contains('MainC')) {
task.enabled = false
}
if (task.name.startsWith('link')) {
task.enabled = false
}
if (task.name.endsWith("SharedLibrary") ) {
task.dependsOn buildNative
}
}
android {
sourceSets {
main {
jniLibs.srcDirs = ['src/main/libs']
}
}
}
So I want to modify one of the files in the OpenCV build for Android and I followed the instructions on their site to download the build and I was able to make it successfully. I modified one of the files (calibinit.cpp) and made it again and copied the files in the
opencv/platforms/build_android_arm/lib/
folder into the jniLibs folder of my AndroidStudio Project.
However, the changes I made are definitely not working. I already had a previous OpenCV library linked to my project, so I'm pretty sure it's still using the old code. How do I use the new build with my AndroidStudio Project?
Can you provide more details about how u use jni in Android Studio Project?
Did u try to clean and rebuild?
Do you have your own custom Android.mk? cause Android Studio will generate its own Android.mk, which can lead to unexpected result as u thought.
delete jniLibs folder and every so file, you don't need them.
define NDK path in local.properties file:
sdk.dir=D:\Android\SDK
ndk.dir=D:\Android\NDK
create a folder(i named it jni) and put Android.mk and Application.mk in it (and an empty dummy.c file for preventing future buggy errors)
as Roy said, Android Studio generates it's own Android.mk, we have our own Android.mk file and Android Studio must use it.jni.srcDirs = []prevents that generation!
Extract openCV sources somewhere, i put them here: D:\Android\Libs\OpenCV
here is my files, modify and use them
build.gradle:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "your.package"
minSdkVersion 11
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
sourceSets {
main {
jni.srcDirs = [] /*disables automatic ndk-build call */
}
}
task ndkBuild(type: Exec) {
commandLine file('D:\\Android\\NDK\\ndk-build.cmd').absoluteFile,
'NDK_PROJECT_PATH='+file('src\\main\\jni').absolutePath,
'APP_BUILD_SCRIPT='+file('src\\main\\jni\\Android.mk').absolutePath,
'NDK_APPLICATION_MK='+file('src\\main\\jni\\Application.mk').absolutePath,
'NDK_LIBS_OUT='+file('src\\main\\jniLibs').absolutePath
}
tasks.withType(JavaCompile) {
compileTask -> compileTask.dependsOn ndkBuild
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile project(':openCVLibrary300rc1')
}
Application.mk:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a x86
APP_PLATFORM := android-8
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#opencv
OPENCVROOT:= D:/Android/Libs/OpenCV
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
OPENCV_LIB_TYPE:=SHARED
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk
//I have a local source file, you can remove it
LOCAL_SRC_FILES := DetectionBasedTracker_jni.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog
LOCAL_MODULE := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)