简体   繁体   English

一起使用Java和C ++的Android项目

[英]Android project using java and C++ together

Anybody have one example of a android project using a same time C++ and Java together, for example, one normal android project build on eclipse and in there add a cpp class and using this cpp class on java class... 任何人都有一个同时使用C ++和Java的android项目的示例,例如,一个在eclipse上构建的普通android项目,然后在其中添加一个cpp类并在java类上使用此cpp类。

say I have a class Foo on C++ 说我在C ++上有一个Foo类

class Foo{
...
}

and I have a class MyActivity on Java 我在Java上有一个类MyActivity

public class MyActivity extends Activity{
...
}

how to i instance the Foo class on MyActivity class?... 我如何在MyActivity类上实例化Foo类?

thanks a lot for all. 非常感谢大家。

First you have to make a .so of you c++ classes with build_ndk.sh 首先,您必须使用build_ndk.sh在您的c ++类中创建一个.so

On the C++ side you have to declare your JNI methods 在C ++方面,您必须声明您的JNI方法

JNIEXPORT void JNICALL 
Java_org_your_ackage_someFunction(JNIEnv * env, jobject  obj)
{}

On the java side 在Java方面

public class YourActivity extends Activity {
    // Load Native Libary
    static {
        System.loadLibrary("your_lib_name");
    }

    public native void someFunction();
}

Then use the function as you would do in java. 然后像在Java中一样使用该函数。

You should take a look at this nice tutorial 您应该看一下这个不错的教程

If you're just trying to learn how to use the NDK, look at the documentation and samples in your NDK directory. 如果您只是想学习如何使用NDK,请查看NDK目录中的文档和示例。

I would recommend starting with the hello-jni sample which is very easy to understand. 我建议从hello-jni示例开始,该示例非常容易理解。 It shows how to call C/C++ methods from Java. 它显示了如何从Java调用C / C ++方法。

You can find a lot of Android NDK examples under path/to/<android-ndk-folder>/samples . 您可以在path/to/<android-ndk-folder>/samples下找到许多Android NDK示例。 There is also a lot of helpful info in the docs folder, and on the official site . docs文件夹以及官方网站上也有很多有用的信息。

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

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