0

I am using android studio, and followed the basic tutorials with android ndk. I also get the simple projects working.

Question:

My native lib returns a simple string. The file MyLibrary.cpp is within the jni folder, its working and looks like this:

#include "de_rownage_ndklib_MyNDK.h"

JNIEXPORT jstring JNICALL Java_de_rownage_ndklib_MyNDK_getMyString  (JNIEnv *env, jobject)
{
   return(*env).NewStringUTF("My Lib");
}

Now I would like to use a class like here: Scanning and Editing Android App Memory Values Programmatically, do I have to put it in a JNIEXPORT jint wrapper? This would not make sense to me. The JNI seems to be a an interface for calling c++ code in Java. It seems like i can not create a new memory.cpp file and call it in java without the JNIEXPORT stuff?

I ve also seen sth like this:

JNI (Directory) -> NDK_MyFunc.c

In the Memory.java (Activity-Class)

public String execNDK(String params) {
    String[] commands = new String[2];
    commands[0] = "chmod 777 /data/data/com.android.test/NDK_MyFunc";
    commands[1] = "/data/data/com.android.test/NDK_MyFunc " + params;
    return shell.execute(commands, false);
}

How is this working? Is it working? Does the file get compiled on the android system and executed? What I am trying to do is executing the main() function above, within my android app.

Panup Pong
  • 1,871
  • 2
  • 22
  • 44
user3325230
  • 507
  • 1
  • 6
  • 19
  • 2
    Every C++ function that you want to call from Java must be exported and named according to the proper convention (`JNIEXPORT JNICALL Java_blah_blah_blah`), or registered with `RegisterNatives` (see [this](https://android.googlesource.com/platform/development/+/master/samples/SimpleJNI/jni/native.cpp)). The last example you showed isn't compiling anything - it's executing a precompiled binary (it might not work unless you're running as root). – Michael Aug 21 '16 at 10:27
  • Thank you Michael! You are right. The project I mentioned contains a JNI directory with the NDK_MyFunc.c. This functions seems to get compiled and executed as a binary. There is no interaction with the java code, so the JNI seems to be not necessary. Would like to mark this comment as answer, but not possible till yet. – user3325230 Aug 21 '16 at 22:25

0 Answers0