I have native method like this.
public native int NativeMethod(Object object);
JNIEXPORT jint JNICALL NativeMethod(
JNIEnv *,
jobject,
jobject object) {
printf("%d", (int)object);
}
I call the method two times with different Java objects.
In Java I output their hashCode() and toString() info and they have different values.
But in native C, they have same output value.
It is just observed on Android 5.x devices and working well on devices below 5.0.
Also it works first time(different value in C) and doesn't work after that(some operations, not specific).
Really I don't how to fix it.
Anyone can help? Please.
Related
I am using TextureView and Vulkan to draw 3D models. I get the surface from the TextureView and pass it to the native side to render.
I need to support multiple TextureView to render different 3D models simultaneously, but I find out that my native .so library is only loaded once per process, suitable for memory saving.
So I think I need a global std::map in jni to hold different c++ objects. Every TextureView has an id and could use its id to find its c++ objects.
I was planning to code like this.
#include <jni.h>
#include <string>
#include <map>
#include <android/native_window_jni.h>
#include <android/asset_manager_jni.h>
std::unique_ptr<std::map<jint, MiuiVkWidgetApp*>> mVkWidgetAppMapUniquePtr;
extern "C" JNIEXPORT jboolean JNICALL
Java_com_miui_vkwidget_MiuiVkTextureView_nativeInitVkWidgetApp(JNIEnv* env, jobject /* this */, jint id) {
...
if (!mVkWidgetAppMapUniquePtr) {
mVkWidgetAppMapUniquePtr.reset(new std::map<jint, MiuiVkWidgetApp*>());
}
...
return true;
}
extern "C" JNIEXPORT void JNICALL
Java_com_miui_vkwidget_MiuiVkTextureView_nativeDestroyVkWidgetApp(JNIEnv* /* env */,
jobject /* this */, jint id) {
mVkWidgetAppMapUniquePtr.reset(nullptr);
...
}
But in https://developer.android.com/training/articles/perf-jni#local-and-global-references "Local and global reference," it says that "The only way to get non-local references is via the functions NewGlobalRef and NewWeakGlobalRef." But NewGlobalRef seems only to support java objects.
I assume how I create the global map may have some potential risks, such as memory leak(I call DestroyVkWidgetApp when TextureView is onDestroy)? I am curious on how to create a proper global map in jni to hold multiple c++ objects? Thanks!
JNI GlobalRefs refers to Java VM objects: those objects will persists in memory until a "destroy" method is called and Java could refers to them (if needed) in that period. Without GlobalRefs that object's memory could be freed early (by JVM GarbageCollector) and Java will FC due to memory issue.
It's seems that your JVM spawns multiple Processes, so if second process is spawn from the first one, yes you could use a "Mapped Memory File" starting from JavaVersion.VERSION_1_4 (JNI part could be copied from here: https://www.codeproject.com/Articles/2460/Using-Memory-Mapped-Files-and-JNI-to-communicate-b even if you don't need the Java one).
If processes are spawned from different Apps, I'm not sure but I think my solution will work.
(A "global map" is GLOBAL inside the same JVM (one Process per JVM), so even if you use a "global map" it will not be "seen" from the second Process but this last one will create its own "global map")
I could call a very simple JNI method below from Java on Android somehow. However I have no idea what should I do next.
JNIEXPORT jstring JNICALL Java_com_test_ndktest_MyActivity_HelloJNI
(JNIEnv *env, jobject obj)
{
(*env)->NewStringUTF(env, "Hello from JNI");
}
What I want to do are two things...
What is compatible Java's substring method.
String str1 = new String("Hello World!");
String new_str1 = str1.substring(2, 5);
System.out.println(new_str1); // llo
I want to have a limitation to call
I may be able to get caller's package name. I want to avoid calling by unexpecting callers.
I am very grateful if you can share any hints or advices.
Thanks
Why don't you do that in Java? As for me the better way is to do this things in java. Sorry if i din't understand something.
As you know you can use C/C++. Did you see this:
Elegant way to copy substring from char* to std::string or http://www.cplusplus.com/reference/string/string/string/?
I have those two pieces of code, the first is:
JNIEXPORT jlongArray* JNICALL Java_com_home_overlay_activity_MainActivity_ProcessPointer(JNIEnv* env, jobject) {
jlongArray blobs_arr;
return &blobs_arr;
}
and the second is:
JNIEXPORT jlongArray JNICALL Java_com_home_overlay_activity_MainActivity_Process(JNIEnv* env, jobject) {
jlongArray blobs_arr;
return blobs_arr;
}
all I want is to return long array to the java code.
The first runs okay while the second not, is there any issue here with returning a long array this way ??
There are no pointers in Java, so I think that if the first snippet of code actually works, it will not produce what you want at all. It probably returns the memory address of the C jlongArray.
As for the second piece of code, I can't see any problem with it except that it returns an uninitialized object, maybe NULL, maybe some random memory garbage, which probably causes unexpected behavior on the Java side. Maybe you should try initializing it to NULL in the C part, or try making your code snippet more realistic by actually filing the array so you can test the code behavior in real conditions.
My application was not reading the jni.h but working .. which is weird, after I set the NDKROOT variable it all worked correctly
For example, in android Java code, it calls a native method:
private native final String native_getParameters();
Where/how should I grep where is the C++ method defined native_getParameters();?
Thank you.
The C++ method will also contain your namespace (e.g. com.domain.your) and classname (e.g. YourActivity), it will look something like
JNIEXPORT jstring JNICALL Java_com_domain_your_YourActivity_native_1getParameters(JNIEnv * env, jclass clazz)
Note the C++ _1 equates to the _ in your Java method
I have the following JNI wrapper C++ code:
#include "map_container.h"
extern "C" {
JNIEXPORT void JNICALL Java_com_map_Map_openMapNative(JNIEnv* env, jobject thiz, jstring path);
};
static map_container* map = NULL;
void Java_com_map_Map_openMapNative(JNIEnv* env, jobject thiz, jstring path)
{
const char* filename_utf8 = env->GetStringUTFChars(path, false);
if ( mapview )
{
delete mapview;
mapview = NULL;
}
mapview = new map_container((char*)filename_utf8);
if (filename_utf8)
{
env->ReleaseStringUTFChars(path, filename_utf8);
}
}
and have com.map.Map.openMapNative declared as static which means that I can operate one map at a time. How do I modify this C++ code so that map_container* map becomes not static and belongs to the exact instance of com.map.Map class? map_container is totally C++ class and has no reflection in Java.
I'm using SWIG to generate all the necessary wrapper code. You simply define the classes and functions you want to wrap in an interface definition file, and let SWIG create all the required C++ and Java code for you. Highly recommended! Writing JNI code by hand is just way too boring and error-prone IMHO. See the SWIG docs for Java, it's very easy to use.
If you have declared Map.openMapNative as "static native" in the Java source, then the current declaration is misleading, because the second argument is actually a reference to the Map class (should be "jclass clazz" rather than "jobject thiz"). Doesn't really matter, since you're not using "thiz", and every jclass is a jobject.
The way you make this non-static is to remove the "static" from the declaration on the Java side, and start using "thiz" to access members of the instance.
It might be a little late, but this cookbook here is invaluable!
http://thebreakfastpost.com/2012/01/23/wrapping-a-c-library-with-jni-part-1/
At first glance, and depending that you need, SWIG might be meta-overkill!