简体   繁体   中英

Java to C++ bridge?

I know there are plenty of tutorials about integrating C++ into Java, but whats about the other way around, a Bridge from Java to C++?

The reason i'm asking this is Android .

Every C++ developer who wanted to write applications for the android noticed at some point that there is no serious (mature) C++ api for android (infact, android is lacking an implementation of the STL).

The only API that is mature enough to write android applications in, is Java. So instead of writing an api from scratch, wouldn't it be possible to use the Java Classes from C++?

I know that this sounds merely like an unrealistic dream, but that way most C++ developers weren't forced to learn a new Language.

Essentially, Java to C++ is the same as C++ to Java, with the exception that you need to start the VM manually:

#include <jni.h>

JNIEnv* create_vm() {
    JavaVM * jvm;
    JNIEnv * environment;

    JavaVMInitArgs args;
    JavaVMOption options[1];

    args.version = JNI_VERSION_1_4;
    args.nOptions = 1;

    options[0].optionString = "-Djava.class.path=/path/to/project's/root/";
    args.options = options;
    args.ignoreUnrecognized = JNI_FALSE;

    JNI_CreateJavaVM(& jvm, (void **) & environment, & args);

    return environment;
}

The rest is the /normal/ JNI programming.


Mind that this is for real Java. Dalvik might do things differently, or completely disable them.

Java API is the API for Android. NDK was never intended to replace it. You have an option to write performance critical parts of your app in C++ but that's it. I don't know why would you even want to write Activities in C++? So better not to waste your time, if you already know C++ switching to Java will be a piece of cake.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM