一、C/C++在ndk开发时的区别主要体现在2个方面
1、Android.mk中增加以下代码:
LOCAL_CPP_EXTENSION := cpp
2、这时候jni目录下的文件名应该命名为Hello.cpp(以Hello为例)
而且,一般的情况下,加入涉及到字符串的转换,还需要
#include <malloc.h>
#include <stdlib.h>
代码的下载链接为:http://download.csdn.net/detail/caihongshijie6/6651737
二、代码实现
1、main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="调用c++代码" android:onClick="click" /> </LinearLayout>
2、MainActivity
package com.njupt.cpp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { static{ System.loadLibrary("Hello"); } public native String helloInC(); public native String helloInC(String str); public void click(View v){ Toast.makeText(this, helloInC("haha in cpp..."), 1).show(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @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; } }
3、Hello.cpp
#include <stdio.h> #include <jni.h> #include <malloc.h> #include <stdlib.h> #include "com_njupt_cpp_MainActivity.h" #include <android/log.h>//include D:\android-ndk-r7b\platforms\android-8\arch-arm\usr\include\android下的log.h这个目录 #define LOG_TAG "System.out" #define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__) #define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) /** * c++版的将java风格的字符串转化成c风格的字符串... */ char* Jstring2CStr(JNIEnv* env, jstring jstr) { char* rtn = NULL; jclass clsstring = (env)->FindClass("java/lang/String"); jstring strencode = (env)->NewStringUTF("UTF8"); jmethodID mid = (env)->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B"); jbyteArray barr= (jbyteArray)(env)->CallObjectMethod(jstr,mid,strencode); // String .getByte("GB2312"); jsize alen = (env)->GetArrayLength(barr); jbyte* ba = (env)->GetByteArrayElements(barr,JNI_FALSE); if(alen > 0) { rtn = (char*)malloc(alen+1); //"\0" memcpy(rtn,ba,alen); rtn[alen]=0; } (env)->ReleaseByteArrayElements(barr,ba,0); // return rtn; } JNIEXPORT jstring JNICALL Java_com_njupt_cpp_MainActivity_helloInC__ (JNIEnv * env, jobject){ return env->NewStringUTF("hello jni in cpp.."); } JNIEXPORT jstring JNICALL Java_com_njupt_cpp_MainActivity_helloInC__Ljava_lang_String_2 (JNIEnv * env, jobject, jstring jstr){ char* str = Jstring2CStr(env,jstr); LOGD("%s",str); return env->NewStringUTF("我的女神是章泽天..."); }
4、Android.mk
LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_CPP_EXTENSION := cpp LOCAL_MODULE := Hello LOCAL_SRC_FILES := Hello.cpp LOCAL_LDLIBS += -llog include $(BUILD_SHARED_LIBRARY)