Strange include error during Android NDK app development - android

I am struggling with some funny, but annoying error in my C++ Android native app. I am extending already working example from Oculus Mobile SDK called VrCubeWorld_SurfaceView.
The problem is constant error: defined but not used [-Werror=unused-function] when I want to add my own simple class that is only including App.h from SDK.
My class header file looks like this:
#ifndef VRAPP_H_
#define VRAPP_H_
#include "App.h"
using namespace OVR;
namespace AiLab {
class VrApp : public OVR::VrAppInterface
{
public:
VrApp();
virtual ~VrApp();
void OneTimeInit();
OvrGuiSys* GuiSys;
};
}
#endif /* VRAPP_H_ */
This is my cpp file of a class:
#include "VrApp.h"
namespace AiLab {
VrApp::VrApp():
GuiSys(OvrGuiSys::Create())
{
}
VrApp::~VrApp()
{
OvrGuiSys::Destroy(GuiSys);
}
VrApp::OneTimeInit()
{
GuiSys->Init( app, &app->GetSoundMgr(), app->LoadFontForLocale(), &app->GetDebugLines() );
}
}
What is more interesting is that functions that tend to display this error are in face used, so I comment it out then another error emerge.
Can anyone advice what might be wrong?

As marcinj stated, you can just comment the unused function 'OneTimeInit' to fix this warning, because the compiler thinks this function is not necessary exist in your code since nobody use it at all.
class VrApp : public OVR::VrAppInterface
{
public:
VrApp();
virtual ~VrApp();
// void OneTimeInit();
OvrGuiSys* GuiSys;
};

Related

Unity Plugin AndroidJavaException NoSuchMethodError on Function with parameters

I got a very annoying issue. All of my Android plugins in Unity just work if I dont use parameters in the calling functions. I am working on this issue since 2 days and I am slowly running out of ideas. Even a minimal project doesnt work. I am sure Iam doing it right because my functions are working fine without parameters.
Unity:
void Start (){
AndroidJavaClass pluginClass = new AndroidJavaClass("tnbrtrm.lications.de.myplugin.MyPlugin");
String test = pluginClass.CallStatic<string>("getMessage", new object[] { "alalalalala" });
Debug.Log("callShareAppAARPlugin: " + test);}
Android:
public class MyPlugin extends UnityPlayerActivity {
public static String getMessage(String text)
{ return "Hello World! " + text; }
}
My plugin prints "Hello world" correctly when I delete the parameter "String text", but with parameter I get this annoying error:
AndroidJavaException: java.lang.NoSuchMethodError: no static method "Ltnbrtrm/lications/de/myplugin/MyPlugin;.getMessage(Ljava/lang/String;)Ljava/lang/String;"
I see this error using Plugins as *.jar or *.aar. And I also had a look into this packages and my Classes and Functions are there. Otherwise it wouldnt work without parameters... I tried AndroidJavaClass and AndroidJavaObject.
Can anyone please help me? I tried a lot of things... (older JDK versions, etc.) I seems so easy in all the turorials so I assume it has something to do with my system or unity (5.3.5 pro).
Kind regards,
Tino

PCL working with shared code (cross platform library)

I'm new with Xamarin and have to create a cross platform library (iOS, Android, Win Phone). i have so much difficult to create a pcl who works for those 3 platforms.
For example, this pcl needs establish a socket connection with a printer.
In pcl i can't use System.Net.Sockets so my idea is create a method and tried call his code with shared project .
when i execute the code, is stepped over the code inside #if __ANDROID__ .
PCL
public class Class1
{
public void socket()
{
Conecta.Class oi = new Conecta.Class();
oi.conecta();
}
}
Shared code
#if __ANDROID__
using System.Net.Sockets;
#endif
namespace Conecta
{
class Class
{
public void conecta()
{
#if __ANDROID__
System.Net.Sockets.TcpClient clientSocket = new System.Net.Sockets.TcpClient();
clientSocket.Connect("192.168.210.171", 4002);
NetworkStream serverStream = clientSocket.GetStream();
byte[] outStream = System.Text.Encoding.ASCII.GetBytes("teste \n");
serverStream.Write(outStream, 0, outStream.Length);
serverStream.Flush();
#endif
}
}
}
It's just a test, the code isn't sophisticated
Someone can help me?
Any other idea?
I believe PCL is not yet finished.
Even if you can resolve your printer issue in future you will have a lot of other problems.
In our project we prefer to share a code from one directory with different .csproj files.
PCL benefits really not worth powder and shot.

Descrease debug output level at Xamarin.Android + Visual Studio

Sorry for lame and easy question but I failed to find an answer to it.
Every time I print something to the System.Diagnostics.Debug.WriteLine I have my message tripled:
System.Diagnostics.Debug.WriteLine("test");
Output:
[0:]
test
[0:] test
10-22 19:57:13.981 I/mono-stdout( 1026): test
I'm stick to System.Diagnostic.Debug because I write messages from both UI part (monodroid) and business logic (PCL)
Is there any way to descrease level of debug noise of Xamarin.Android?
Thank you for any suggestions.
Make your own little abstraction. We have the same issue in our project, this little Interface helps:
public interface ILogger
{
void Write(LogLevel level, String tag, String message);
}
Then you have your Loggers for each platform, for example:
public class AndroidLogger: ILogger
{
public void Write(LogLevel level, string tag, string message)
{
Android.Util.Log.WriteLine(ConvertLogLevel(level), tag, message);
}
}
You inject the logger in the iOS/Android project and you can even create fancy logger like e.g. a nice file-logger for iOS:
#if DEBUG
LOG.AddLogger(new TouchFileLogger());
LOG.AddLogger(new ConsoleLogger());
#endif
The LOG-class is static and needs not to know about the implementations, thats why it can be easily used in your shared PCL library.
Hope that helps, despite your problem might be solved by now ;-)
You can use Android.Util.Log instead, this greatly decreases it. It also has different levels of logging which you can filter in logcat.
Info: Log.Info()
Debug: Log.Debug()
Warning: Log.Warn()
Error: Log.Error()
Verbose: Log.Verbose()
and additionally a WriteLine, which does all of the above:
Log.WriteLine()

Calling native method twice of third party library in an Activity causes the Android application to close down

I have integrated two native libraries (.so ) in my application. The libraries compile fine and I can load them in my application too. The first time I invoke a native method of a library it works fine, but if I call the same method again in the Activity the application shuts down.
The problem I am facing is exactly the same as mentioned in here :
http://grokbase.com/t/gg/android-ndk/1226m68ydm/app-exit-on-second-native-call
The solution that works is to invoke the native method in another Activity and shut it down forcefully via System.exit(0). Following the article I tried setting the pointers to NULL of the called method after a successful operation, but this too didn't help me. Also its not possible to unload a library once its loaded by System.loadLibrary().
I want to call the native methods more than once without creating a new Activity. Any ideas how to solve this issue ?
(I FINALLY FOUND A SOLUTION ... HERE IT IS)
Okay, I have finally found a way to resolve this issue. The solution is actually pretty simple. Build another independent native library (utility library) to load and unload the other libraries. What we need to do is use dlopen() and dlclose() in the native method of the utility. We can load the utility library like before via System.loadLibrary().
So in the native method of the utility library what we need to do is:
Use#include <dlfcn.h> // this is required to call dlopen() and dlclose() functions.
Provide handler and function prototype:
void *handle;
typedef int (*func)(int); // define function prototype
func myFunctionName; // some name for the function
Open the library via dlopen() :
handle = dlopen("/data/data/my.package.com/lib/somelibrary.so", RTLD_LAZY);
Get and Call the function of the library:
myFunctionName = (func)dlsym(handle, "actualFunctionNameInLibrary");
myFunctionName(1); // passing parameters if needed in the call
Now that the call is done. Close it via dlclose():
dlclose(handle);
Hope this will help others facing the same issue.
So ... my solution was starting a service that runs the shared library code, this service has a different process name ( you can set it in the Android Manifest ), as it is a different process you can kill it ( Using Process.killProcess(Process.myPid()) when it finishes running, without affecting your application in any way.
Worked very well for me, hope it helps someone else.
As this is the top hit for this issue and as the issue itself still exists, it seems that the approach that ZakiMak shared with us is still the most popular solution.
For others who may want to implement it and would like a little more detail for the latest Android releases, here are some notes I made as I stumbled through this:
Firstly, there is a solution which implements this approach on GitHub now. I have not tried it personally, but I have used it as a reference. It is very useful to see how the Android.mk file is structured and how the library is opened and methods called. Link is here: https://github.com/jhotovy/android-ffmpeg
The path to the native library folder changes over Android releases and it also appears to change every time you run the app (although this may be just in debug mode). Either way, it is best to pass the path in from the calling Java method if possible. For example:
In the Java wrapping class:
import android.content.Context;
import android.util.Log;
public class FfmpegJNIWrapper {
//This class provides a Java wrapper around the exposed JNI ffmpeg functions.
static {
//Load the 'first' or 'outer' JNI library so this activity can use it
System.loadLibrary("ffmpeg_wraper_multi_invoke_jni");
}
public static int call_ffmpegWrapper(Context appContext, String[] ffmpegArgs) {
//Get the native libary path
String nativeLibPath = appContext.getApplicationInfo().nativeLibraryDir;
//Call the method in the first or 'outer' library, passing it the
//native library past as well as the original args
return ffmpegWrapper(nativeLibPath, ffmpegArgs);
}
// Native methods for ffmpeg functions
public static native int ffmpegWrapper(String nativeLibPath, String[] argv);
}
In the 'first' or 'outer' native library:
JNIEXPORT jint JNICALL Java_com_yourpackage_androidffmpegwrapper_FfmpegJNIWrapper_ffmpegWrapper(JNIEnv *pEnv, jobject pObj, jstring nativeLibPath, jobjectArray javaArgv) {
//Get the second or 'inner' native library path
char* nativePathPassedIn = (char *)(*pEnv)->GetStringUTFChars(pEnv, nativeLibPath, NULL);
char ourNativeLibraryPath[256];
snprintf(ourNativeLibraryPath, sizeof (ourNativeLibraryPath), "%s%s", nativePathPassedIn, "/libffmpeg_wraper_jni.so"); //the name of your ffmpeg library
//Open the so library
void *handle;
typedef int (*func)(JNIEnv*, jobject, jobjectArray);
handle = dlopen(ourNativeLibraryPath, RTLD_LAZY);
if (handle == NULL) {
__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "could not open library: %s", dlerror());
printf("Could not dlopen(\"libbar.so\"): %s\n", dlerror());
return(-1);
}
//Call the ffmpeg wrapper functon in the second or 'inner' library
func reenterable_ffmpegWrapperFunction;
reenterable_ffmpegWrapperFunction = (func)dlsym(handle, "Java_com_yourpackage_androidffmpegwrapper_FfmpegJNIWrapper_ffmpegWrapper");
reenterable_ffmpegWrapperFunction(pEnv, pObj, javaArgv); //the original arguments
//Close the library
dlclose(handle);
// return
return(1);
}
The Android.mk file is a little 'flaky' to put it politely. Because you are building two separate libraries in one Android.mk file, this may be a little more complex that other NDK make files so if you get some strange errors do some searching before you start taking your project apart. For example: https://stackoverflow.com/a/6243727/334402

How do I write a native method?

Now I got a .so file and a C++ header file.
There is a function as follows:
BOOL __stdcall HK_STD_CreateHandle(IN PBYTE pFileHdrBuffer, IN DWORD dwFileHdrSize, IN DWORD dwBufferSize, OUT HANDLE& hHandle);
typedef BYTE * PBYTE;
typedef unsigned long DWORD;
typedef void * HANDLE;
I think I can implement a Java class to use the .so file:
public class Decoder {
static {
System.loadLibrary("SingleDecode");
}
public native boolean HK_STD_CreateHandle(
byte[] pFileHdrBuffer,
int dwFileHdrSize,
int dwBufferSize,
int hHandle);
}
But I don't know how to write the native function. Can anybody help me?
First of all your native function must have a certain name. For example, if you have a class MyActivity in the package com.android.test where you have declared the native method, the corresponding native function must be named:
Java_com_android_test_MyActivity_functionName();
Next, the native function must receive two additional arguments that are not explicitly defind by you (they're sent by the Java environment). I could explain it here, but it's probably easiest if you read up on JNI. Try Wikipedia for example:
http://en.wikipedia.org/wiki/Java_Native_Interface
There are also a few samples that comes with the NDK that will help you out.

Categories

Resources