简体   繁体   English

Java到C ++的桥梁?

[英]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++? 我知道有很多关于将C ++集成到Java中的教程,但是反过来,从Java到C ++的桥梁又如何呢?

The reason i'm asking this is Android . 我问这个的原因是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). 每个想要为android编写应用程序的C ++开发人员都注意到在某个时候,没有适用于android的严肃的(成熟的)C ++ api(实际上,android缺少STL的实现)。

The only API that is mature enough to write android applications in, is Java. 足以成熟地编写android应用程序的API是Java。 So instead of writing an api from scratch, wouldn't it be possible to use the Java Classes from C++? 因此,不是从头开始编写api,就不可能使用C ++的Java类吗?

I know that this sounds merely like an unrealistic dream, but that way most C++ developers weren't forced to learn a new Language. 我知道这听起来像是一个不切实际的梦想,但是这样大多数C ++开发人员都不会被迫学习一种新的语言。

Essentially, Java to C++ is the same as C++ to Java, with the exception that you need to start the VM manually: 本质上,从Java到C ++与从C ++到Java相同,不同之处在于您需要手动启动VM:

#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. 其余的是/ normal / JNI编程。


Mind that this is for real Java. 请注意,这是用于真正的 Java。 Dalvik might do things differently, or completely disable them. Dalvik可能会做不同的事情,或者完全禁用它们。

Java API is the API for Android. Java API是Android API。 NDK was never intended to replace it. NDK从未打算取代它。 You have an option to write performance critical parts of your app in C++ but that's it. 您可以选择用C ++编写应用程序的性能关键部分,仅此而已。 I don't know why would you even want to write Activities in C++? 我不知道您为什么还要用C ++编写活动? So better not to waste your time, if you already know C++ switching to Java will be a piece of cake. 因此最好不要浪费时间,如果您已经知道C ++切换到Java将是小菜一碟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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