Building OpenCV app with JNI in Eclipse - android

I've got a problem when building android applications with OpenCV and JNI part in Eclipse.
The problem is than ndk-build runs normally for the project, but Eclipse shows errors (usually irrelevant to the code).
For example, I'm building one of the OpenCV4Android samples:
boris#boris-ubuntu:~/src/OpenCV-2.4.3-android-sdk/samples/tutorial-3-native$ ndk-build
Install : libnative_camera_r2.2.0.so => libs/armeabi-v7a/libnative_camera_r2.2.0.so
Install : libnative_camera_r2.3.3.so => libs/armeabi-v7a/libnative_camera_r2.3.3.so
Install : libnative_camera_r3.0.1.so => libs/armeabi-v7a/libnative_camera_r3.0.1.so
Install : libnative_camera_r4.0.0.so => libs/armeabi-v7a/libnative_camera_r4.0.0.so
Install : libnative_camera_r4.0.3.so => libs/armeabi-v7a/libnative_camera_r4.0.3.so
Install : libnative_camera_r4.1.1.so => libs/armeabi-v7a/libnative_camera_r4.1.1.so
Install : libnative_sample.so => libs/armeabi-v7a/libnative_sample.so
Install : libopencv_java.so => libs/armeabi-v7a/libopencv_java.so
It runs normally, but Eclipse shows errors:
'size_t' is ambiguous '
Candidates are:
Field 'pt' could not be resolved
Field 'pt' could not be resolved
Field 'x' could not be resolved
Field 'y' could not be resolved
Invalid arguments '
Candidates are:
void detect(const cv::Mat &, ? &, const cv::Mat &)
void detect(const ? &, ? &, const ? &)
The type 'cv::FastFeatureDetector' must implement the inherited pure virtual method 'cv::FeatureDetector::detectImpl'
And other error like this one in other projects. And also, the Eclipse intellisense (or how is it called properly :) is not working on .cpp files.
I've tried to clean the projects, delete the obj and lib folders, but no effect.
My system is ubuntu 12.04, OpenCV version is 2.4.3.
Thanks for any help!

Had same problem with you on ubuntu 12.04, OpenCV version is 2.4.3. exactly on the same example and I am using the eclipse-android bundle.
Here's what I did:
Update the NDK plugin using google repo, It is already installed but it can be updated.
Then I changed size_t to int and added a temp variable instead of going straight as the example goes. Here's the code:
Mat* pMatGr=(Mat*)addrGray;
Mat* pMatRgb=(Mat*)addrRgba;
vector<KeyPoint> v;
FastFeatureDetector detector(50);
detector.detect(*pMatGr, v);
for( int i = 0; i < v.size(); i++ )
{
KeyPoint tK = v[i];
circle(*pMatRgb, Point(tK.pt.x, tK.pt.y ), 10, Scalar(255,0,0,255));
}
It really is a weird bug, my guess is that it has something to do with eclipse debug mechanism not working properly.

Yea very strange bug. Am on Ubuntu 12.04 and OpenCV 2.4.5.
I got the same bug on 'OpenCV Tutorial 2 - Mixed Processing'
Managed to fix it without reinstalling Eclipse or NDK. Just deleted the sample project, then imported it again. Works fine now.

Related

Eclipse ADT "undefined reference to (custom class)", but works fine in Xcode (using Cocos2dx)

I have a project that's fully functional in Xcode but not compiling in Eclipse ADT. I have successfully compiled and run android applications in the past, but this is my first time to create a custom class. To replicate the issue:
I create a new project, then add the following 2 files:
//Enemy.cpp
#include "Enemy.h"
USING_NS_CC;
bool Enemy::init()
{
if(!Layer::init())
return false;
return true;
}
//Enemy.h
#ifndef __ColorMirror__Enemy__
#define __ColorMirror__Enemy__
#include <iostream>
#include "cocos2d.h"
USING_NS_CC;
class Enemy : public cocos2d::Layer{
public:
CREATE_FUNC(Enemy);
virtual bool init();
};
#endif
I am able to see them in Eclipse in the list of classes.
Then I create a new enemy in HelloWorldScene.cpp as follows:
Enemy *newEnemy = Enemy::create();
This compiles and runs as expected in Xcode, but when I run build_native.py I get the following errors:
jni/../../Classes/Enemy.h:19: error: undefined reference to 'Enemy::init()'
jni/../../Classes/Enemy.h:19: error: undefined reference to 'vtable for Enemy'
Checking in the jni/../../Classes file, I am able to find Enemy.h and Enemy.cpp.
I have seen several explanations for fixes in other versions of Eclipse, and the suggestion it's a linker error, but I haven't figured out how to fix it in ADT.
Thanks!
So I'm not entirely sure which of these steps fixed the problem, and it may very well have been multiple issues, but here's what I did:
1) Added Enemy.cpp into Android.mk as follows:
Browse to game folder /proj.android/jni and open Android.mk
Open with TextEdit
Below the AppDelegate.cpp in LOCAL_SRC_FILES: added this:
../../Classes/Enemy.cpp \
Save
(still didn't work, so I took it out)
2) Changed workspace for Eclipse (which is specified on opening eclipse) and deleted old workspace
3) Downloaded and set up newest ADT package (Eclipse JUNO)
4) Upgraded Cocos2d-x from 3.1 to 3.2
(still didn't work)
5) Added Enemy.cpp to Android.mk again
Upon completing all those steps, everything works great and compiles as before.
Thanks to Wez Sie Tato for confirming that Enemy.cpp did in fact need to be added to Android.mk... Based on some comments I've seen elsewhere, it appears that my ADT package may have gotten damaged somehow, in conjunction with not having the proper line in Android.mk

Native OpenCV face detection error

I'm new on developing a native code for android application.
Actually I'm going to test some native OpenCV samples for android. I'm using Eclipse. I found a few errors upon compiling the face detection example.
I could correct most of the errors by  following this tutorial
And
I defined my environment variable NDK-root
activation of some configs on Android.mk
The console result is that the libraries .so are correctly built, as the following :
[armeabi-v7a] Install : libdetection_based_tracker.so =>
libs/armeabi-v7a/libdetection_based_tracker.so [armeabi-v7a] Install
: libnative_camera_r2.2.0.so =>
libs/armeabi-v7a/libnative_camera_r2.2.0.so [armeabi-v7a] Install
: libnative_camera_r2.3.3.so =>
libs/armeabi-v7a/libnative_camera_r2.3.3.so [armeabi-v7a] Install
: libnative_camera_r3.0.1.so =>
libs/armeabi-v7a/libnative_camera_r3.0.1.so [armeabi-v7a] Install
: libnative_camera_r4.0.0.so =>
libs/armeabi-v7a/libnative_camera_r4.0.0.so [armeabi-v7a] Install
: libnative_camera_r4.0.3.so =>
libs/armeabi-v7a/libnative_camera_r4.0.3.so [armeabi-v7a] Install
: libnative_camera_r4.1.1.so =>
libs/armeabi-v7a/libnative_camera_r4.1.1.so [armeabi-v7a] Install
: libnative_camera_r4.2.0.so =>
libs/armeabi-v7a/libnative_camera_r4.2.0.so [armeabi-v7a] Install
: libopencv_java.so => libs/armeabi-v7a/libopencv_java.so
But when running the application, a syntaxic error raised in the jni file: « DetectionBasedTracker_jni.cpp », at two places :
1- result = (jlong)new DetectionBasedTracker(stdFileName, DetectorParams);
error = type 'DetectionBasedTracker'could not be resolved
2- ((DetectionBasedTracker*)thiz)->getObjects(RectFaces);
error : Invalide arguments 'Candidates are : void getObjects(?&)'
Please see the attached images
I don't understand why I got these errors, since i never touched the original code ?
Any idea to correct these bugs ?
DetectionBasedTracker_jni.h is missing from your project. It is included in general, I don't know why you don't have it. It may cause your errors, but its just a tip.
timurDroid
In Eclipse i always have like this error, but after close cpp file and
restart IDE Eclipse, Apllication successfull run. Dont know, why it..
I believe the anomaly you are talking about may be a different issue, because in that case you have a Lot of error messages containing could not be resolved, correct me if you only have that one (a few, not all). For me if I open these jni files "everything cannot be resolved" due to (I'm just guessing) prebuild-linking eclipse bugish issues.
Finally!!. I solved this problem by recreating a new project, then imported the src files into.
Now it works fin!

Unity command line arguments for android and iOS

Please pardon my ignorance, relatively new to working with Unity3D. I am working on automating Unity3d builds from the command line.
I am looking for command line arguments to build apk & xcode project.
Unity's documentation does mention arguments to build a standalone Mac OSX player (-buildOSXPlayer) and a standalone Windows player (-buildWindowsPlayer) but not for android and iOS.
Any help would be really appreciated. Thanks.
Start with Unity's own documentation for command line builds for iOS and android.
For example, put this script in your Assets/Editor folder:
// C# example
using UnityEditor;
class Autobuilder
{
[MenuItem ("File/AutoBuilder/iOS")]
static void PerformBuild ()
{
string[] scenes = { "Assets/MyScene.unity" };
string buildPath = "../../../Build/iOS";
// Create build folder if not yet exists
Directory.CreateDirectory(buildPath);
BuildPipeline.BuildPlayer(scenes, buildPath, BuildTarget.iOS, BuildOptions.Development);
}
}
You can actually run this script in the Unity application by going to File -> Autobuilder -> iOS
To run on command line, it looks something like this:
/Applications/Unity/Unity.app/Contents/MacOS/Unity -quit -batchmode -executeMethod Autobuilder.PerformBuild -email me#email.com -password myPassword
Of course, you're going to want to check the debug log for any errors:
Mac OS X ~/Library/Logs/Unity/Editor.log
Windows XP C:\Documents and Settings\username\Local Settings\Application Data_\Unity\Editor\Editor.log
Windows Vista/7 C:\Users\username\AppData\Local\Unity\Editor\Editor.log
The -executeMethod command line parameter is a very simple option to invoke any function in any of your scripts - which you can then use to do pretty much anything, including firing off an Android build.

Migrate a C program to Android NDK

I am a beginner with C/C++ and the Android NDK, and I have a problem trying to create a native library. My code compiles using MinGW on CDT, but when I write the same code on a JNI file, there is an error.
My code is:
int n = 7;
int positions[n];
int final_order[n];
memcpy(positions, final_order,sizeof(final_order));
The plugin shows me:
Invalid arguments 'Candidates are: void * memcpy(void *, const void *, ?)'
This the header from MinGW on CDT:
_CRTIMP void* __cdecl __MINGW_NOTHROW memcpy (void*, const void*, size_t);
This the header from the Android NDK:
extern void* memcpy(void *, const void *, size_t);
There is a known indexing problem when using Eclipse to do NDK development.
Read about it here: https://code.google.com/p/android/issues/detail?id=33788
Near the bottom there is a reasonable workaround that I myself use:
What we want is to use the Android GCC toolchain but change the actual tools and modify the discovery options so that the include paths and symbols are correct.
Go into C/C++ Buid \ Tool Chain editor
Make sure Android GCC is the selected toolchain and Android Builder is the selected builder.
Click on "Select Tools"
Select "Allow all changes"
Remove the Android GCC Compiler
Add "GCC C Compiler" and "GCC C++ Compiler" so that we can index both c and cpp headers.
If you would look at "Paths and Symbols" you would see we have both GNU C and GNU C++ languages, but of course the built-in include paths are wrong. luckily there is a fix for that.
Go into C/C++ Build \ Discovery Options
Change Discovery profiles scope to "Configuration-wide"
Make sure the Discovery profile is using "AndroidPerProjectProfile" and make sure both checkboxes are checked.
I then have to manually add my own include directories and definies under Project Properties -> C/C++ General -> Paths and Symbols
It's worth noting that your code compiles fine. You can turn off indexing if you like, but if you still want the advantages of indexing you'll have to work around the issue.
Another workaround is:
to open the android native perspective,
to right-click on your project in the project navigator,
and to click on the index options to rebuild the indices.

cocos2d android compile time linking not working

I am trying to get cocos2d-android (cocos2d-2.0-rc2-x-2.0.1) "Helloworld" sample to run under windows. I am using latest version of cygwin along side with android ndk r6, android sdk API 8. And I tried the manual here
after a lot of challenges I am down to this problem which I think is in linking the classes at compile time. when I try to run the *build_native.sh* script I get an error stating that in CCGL.h, PFNGLDELETEVERTEXARRAYSOESPROC which is defined as extern, does not name a type.
//declare here while define in CCEGLView_android.cpp
extern PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT;
extern PFNGLBINDVERTEXARRAYOESPROC glBindVertexArrayOESEXT;
extern PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT;
'PFNGLDELETEVERTEXARRAYSOESPROC' is declared in CCEGLView.h.
#if CC_TEXTURE_ATLAS_USE_VAO
#include <EGL/egl.h>
PFNGLGENVERTEXARRAYSOESPROC glGenVertexArraysOESEXT = 0;
PFNGLBINDVERTEXARRAYOESPROC glBindVertexArrayOESEXT = 0;
PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT = 0;
#endif
I tried to add the header address to android.mk under jni directory, to LOCAL_C_INCLUDES, as suggested here,but there were no rules to make them. so can anybody help me with how to compile/link this, it would be much appreciated.
I had this problem, and using a higher version of NDK worked. Try to get the most recent version, reset the environment variables, and run build_native.sh again.
http://developer.android.com/tools/sdk/ndk/index.html

Categories

Resources