Android Java与C++的调用

来源:互联网 发布:xp linux双系统引导 编辑:程序博客网 时间:2024/06/06 02:18

2016年3月17日


1 新建一个Android工程,比如 JniTest



2. 工程右键, Android Tools  ->  Add Native Support...




3. 在 jni 文件中的 JniTest.cpp 中,写如下代码:

#include <jni.h>extern "C"{JNIEXPORT jstring JNICALL Java_com_example_jnitest_MainActivity_strFromJNI(JNIEnv *env,jobject pObj) {return env->NewStringUTF("Hello JNI");}JNIEXPORT jint JNICALL Java_com_example_jnitest_MainActivity_add(JNIEnv *env, jobject pObj,  jint a, jint b){jint res = -1;res = a + b;return res;}}


4.Android项目中的 java代码 MainActivity.java 中添加如下绿色代码


public class MainActivity extends Activity {


static{
System.loadLibrary("JniTest");
}

public native String strFromJNI();
public native int add(int a, int b);




@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

TextView txtView = (TextView)findViewById(R.id.txtView);
txtView.setText(strFromJNI());

TextView txtView2 = (TextView)findViewById(R.id.txtViewtoo);
int n = add(0,0);

txtView2.setText(String.valueOf(add(5, 104)));

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}


-------------------------------------------------------

MainActivity中使用的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/itemlayout"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="vertical" >        <TextView        android:id = "@+id/txtView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <TextView        android:id = "@+id/txtViewtoo"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    </LinearLayout>



运行结果:






                                                                                                                其它:


JNI接口的命名规范是:

Java_ + 调用该方法的包名(包名的点用_代替) + _ + 调用该接口的类名 + _ + 方法名

Java_com_example_jnitest_MainActivity_add

包名:com_example_jnitest

调用类:MainActivity

方法名:add

对于实例方法,有两个参数是必要的,一个JNI的环境指针JNIEnv *,另一个是调用该方法的Java实例jobject

JNIEXPORT jint JNICALL Java_com_example_jnitest_MainActivity_add(
JNIEnv *env, jobject pObj,  jint a, jint b){
jint res = -1;
res = a + b;
return res;
}


使用ndk库必须在static代码块里面用System.loadLaibrary加载.so库



在C/C++代码中实现本地方法  在jni目录的HelloWorld.cpp中添加如下代码,
并添加NDK SDK的头文件目录到工程设置中,在Package Explorer中选中刚才创建的Android工程,
右键选择Properties,在弹出的界面中选择C/C++ General-〉Paths and Symbols,
在包含文件路径中添加即可, 如
D:\Soft\Android\android-ndk-r10e\platforms\android-18\arch-arm\usr\include



编译器会按C语言的方式编译和连接。在C语言中,函数编译之后函数名与C++函数编译之后不同,例如foo(int x, int y),C可能会编译成_foo的名字,而C++因为支持重载,所以会编译成像_foo_int_int这种带参数的函数名。如果是按照C语言的编译方式,调用foo函数是找不到_foo的函数名就会报出函数名找不到的错误。所以要添加extern "C"修饰。


Java中的基本类型包括  boolean,byte,char,short,int,long,float,double 
这些类型分别对应的类型是 jboolean,jbyte,jchar,jshort,jint,jlong,jfloat,jdouble  
这几种类型几乎都可以当成对应的C++类型来用



源码下载:

http://download.csdn.net/detail/yulinxx/9464480

0 0
原创粉丝点击