文中對應的軟件資源下載地址:http://download.csdn.net/detail/u010857510/9646241,裏面包括博文裏介紹的所有Android代碼,歡迎下載,包括jni文件夾下的Android.mk、MyString.c、MyString.h文件,libpandasince.so文件,mydemo.apk,mydemo安卓項目代碼工程,資源截圖如下:
Android NDK的開發,首先先介紹所涉及的相關概念
1、jni簡介
JNI全稱爲Java NativeInterface(JAVA本地調用)。從Java1.1開始,JNI成爲java平臺的一部分,它允許Java代碼和其他語言寫的代碼(如C&C++)進行交互。並非從Android發佈才引入JNI的概念的。
2、NDK簡介
簡單來說,Android的NDK提供了一些交叉編譯工具鏈和Android自帶的庫,這些Android的庫可以讓開發者在編寫本地語言的程序時調用。而NDK提供的交叉編譯工具鏈就對已經編寫好的C&C++代碼進行編譯,生成庫。
筆者是在Ubuntu環境下進行的編譯,Windows環境下也可以進行編譯。
以下部分爲代碼,可從文中所附地址下載相關代碼:
1、jni
Android.mk文件代碼如下LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := pandasince
LOCAL_SRC_FILES := MyString.c
LOCAL_LDLIBS += -llog
LOCAL_LDLIBS +=-lm
include $(BUILD_SHARED_LIBRARY)
MyString.c文件代碼如下:
#include <jni.h>
#include <android/log.h>
#include "MyString.h"
/*
* Class: com_panda_since_MyString
* Method: getstring
* Signature: ()I
*/
JNIEXPORT jstring JNICALL Java_com_panda_since_MyString_getstring
(JNIEnv *env, jobject obj)
{
jstring mstring="你獲取的是一個字符串";
__android_log_print(ANDROID_LOG_INFO, "demo", "getstring");
__android_log_print(ANDROID_LOG_INFO, "demo", "mstring is :%s",mstring);
return (*env)->NewStringUTF(env, mstring);
}
MyString.h文件代碼如下:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <string.h>
#include <jni.h>
/* Header for class com_neojet_scanner_key */
#ifndef _Included_com_panda_since_MyString
#define _Included_com_panda_since_MyString
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_panda_since_MyString
* Method: getstring
* Signature: ()I
*/
JNIEXPORT jstring JNICALL Java_com_panda_since_MyString_getstring
(JNIEnv *env, jobject obj);
#ifdef __cplusplus
}
#endif
#endif
編譯環境是Ubuntu,截圖如下:
生成的so文件在libs/armeabi/目錄下,這個so文件就是安卓要調用的文件,截圖如下:
2、Android工程
上一步操作得到的so文件放在了工程的libs目錄下,工程截圖如下:
MyString.java文件
package com.panda.since;
/**
* Android NDK
* 創建時間:
2016-10-6 上午9:51:49
* 創 建 者 :
Since
* 聯繫方式:
[email protected]
*/
public class MyString {
public native Stringgetstring();
static {
System.loadLibrary("pandasince");
}
}
MainActivity.java文件
package com.panda.since;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
/**
*
* 創建時間:
2016-10-6 上午9:52:35
* 創 建 者 :
Since
* 聯繫方式:
[email protected]
*/
public class MainActivity extends Activity {
private Button btn_get;
private TextView tv;
private MyString mString = new MyString();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mystring);
btn_get = (Button)findViewById(R.id.btn_get);
tv = (TextView)findViewById(R.id.tv);
//按鍵的監聽事件
btn_get.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String s = mString.getstring();
Log.i("demo", "btn_get"+s);
tv.setText(s);
}
});
}
}
activity_mystring.xml佈局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""/>
<Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="獲取" />
</LinearLayout>
編譯運行,在安卓真機上的效果圖如下:
未完待續……後期還會更新Android NDK開發(下)