Calling java from C ++ on Android NDK


Note calling ** Java ** from ** C ++ ** in ** OpenGL ES application (Android, iOS) ** of ** Visual Studio 2017 **



For existing classes

auto env = state->activity->env;
auto vm = state->activity->vm;

vm->AttachCurrentThread(&env, NULL);

jstring str = env->NewStringUTF("abc");

jclass     cls = env->FindClass("java/lang/String");
jmethodID  mid = env->GetMethodID(cls, "toCharArray", "()[C");
jcharArray ary = (jcharArray)env->CallObjectMethod(str, mid);


For user class

** [project name] .Packaging ** Add java source under ** src ** folder of project

package com.test;

public class Test
    public boolean test()
        return true;

Set application: android: hasCode in AndroidManifest.xml to true


<application android:label="@string/app_name" android:hasCode="true">

Call using NativeActivity: getClassLoader

auto env = state->activity->env;
auto vm = state->activity->vm;

vm->AttachCurrentThread(&env, NULL);

jclass activityClass = env->FindClass("android/app/NativeActivity");
jmethodID getClassLoader = env->GetMethodID(activityClass,"getClassLoader", "()Ljava/lang/ClassLoader;");
jobject classLoaderInstance = env->CallObjectMethod(state->activity->clazz, getClassLoader);
jclass classLoader = env->FindClass("java/lang/ClassLoader");
jmethodID findClass = env->GetMethodID(classLoader, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
jstring strClassName = env->NewStringUTF("com/test/Test");
jclass classIWant = (jclass)env->CallObjectMethod(classLoaderInstance, findClass, strClassName);
jmethodID testMethod = env->GetStaticMethodID(classIWant, "test", "()Z");
jboolean retValue = env->CallStaticBooleanMethod(classIWant, testMethod);


Recommended Posts

Calling java from C ++ on Android NDK
Run node.js from android java (processing)
Notes on calling Installer on Android App
Notes on Android (java) thread processing
Reintroducing Java 8 available from Android Studio 2.4
Note: Differences from Java as seen from C #
Kick ShellScript on the server from Java
Sobel filter using OpenCV on Android (Java)
Try calling Nim from Java via JNI
Java to C and C to Java in Android Studio
[Android] Call Kotlin's default argument method from Java
Try using Firebase Cloud Functions on Android (Java)
Notes on getting Callback from Android text-to-speech function TextToSpeech
Try calling the CORBA service from Spring (Java)
For Java engineers starting Kotlin from now on
Try communication using gRPC on Android + Java server
[Java] 4 steps to implement splash screen on Android
Try calling synchronized methods from multiple threads in Java
Behavior when calling Java variadic methods from Scala / Kotlin / Java
Java language from the perspective of Kotlin and C #
Try calling IBM Watson Assistant 2018-07-10 from the Java SDK.
I tried calling Java / Objective-C native code from Flutter
Try image classification using TensorFlow Lite on Android (JAVA)
Generate models from JSON to Swift, PHP, C #, JAVA
Pessimistic lock on NW ABAP from Java via JCo
Call Java from JRuby
Let's touch on Java
Changes from Java 8 to Java 11
Sum from Java_1 to 100
Install Java on Mac
Eval Java source from Java
Run PostgreSQL on Java
Access API.AI from Java
From Java to Ruby !!
Java to fly data from Android to ROS of Jetson Nano
Refer to C ++ in the Android Studio module (Java / kotlin)
Catch up on new features from Java 7 to Java 9 at once
Run R from a tomcat-powered Java process on Amazon Linux