0

Here is what I do :

  1. create a Native C++ project use android studio template
  2. create a class com.jnitest.app.JNIInterface
package com.jnitest.app;

public class JNIInterface {
    public static native String getString();
    public static native String getName();
}


with the native-lib.cpp

#include <jni.h>
#include <string>

extern "C" JNIEXPORT jstring JNICALL
Java_com_jnitest_app_JNIInterface_getString(JNIEnv *env, jclass thiz) {
    std::string name = "return String from JNIInterface";
    return env->NewStringUTF(name.c_str());
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_jnitest_app_JNIInterface_getName(JNIEnv *env, jclass clazz) {
    std::string name = "return name from JNIInterface";
    return env->NewStringUTF(name.c_str());
}
  1. create a test class com.jnitest.app.JNITest
package com.jnitest.app;

public class JNITest {
    {
        System.loadLibrary("app");
    }
    public static void main(String[] args) {
        System.out.println("Hello from JNITest");
        System.out.println("String from JNI: " + JNIInterface.getString());
    }
}

  1. build push and run
adb push .\build\intermediates\apk\debug\app-debug.apk /data/local/tmp/app-debug.jar 

adb shell CLASSPATH=/data/local/tmp/app-debug.jar  app_process ./ com.jnitest.app.JNITest
  1. get output
Hello from JNITest
Killed

Why I cannot get the right result ?

Lty
  • 1
  • 2
  • Possibly related: [How to execute the dex file in android with command?](https://stackoverflow.com/a/10200822/295004). As it is an old question, please read/follow comment threads. – Morrison Chang Apr 08 '22 at 04:43

0 Answers0