android之JNI開發步驟總結

原文:http://bbs.51cto.com/thread-948244-1.html

這幾天一直在研究JNI的開發過程,順便把NDK環境搭建一起總結下。在windows環境下開發jni需要c/c++編譯器的支持,網絡上我看很多人使用cygwin。呵呵我不是很喜歡使用它,感覺安裝起來挺麻煩的。我使用GNUStep,下載地址http://www.gnustep.org/experience/Windows.html



下載安裝後,驗證是否成功。打開GNUstep->Shell,輸入make -v 和 gcc -v命令,如圖所示。




下載NDK,地址http://developer.android.com/tools/sdk/ndk/index.html。下載完後解壓即可。
配置ndk環境變量,gnustep是模擬linux的環境的,打開gnustep的安裝目錄下的G:\softinstall\GNUstep\GNUstep\GNUstep.conf文件,添加以下內容:
複製內容到剪貼板
代碼:
NDK=/g/softinstall/Android/android-ndk-r8b
export=NDK

說明如果不知道ndk目錄在linux下應該是在哪裏,你可以打開gnustep的命令窗口,輸入mount,就可以找到對應的盤符。
驗證環境變量,如下圖。 


以上就配置成功了。

下載進入正題啦。Jni的開發步驟。
打開eclipse,新建工程名爲testJni。在activity中添加以下代碼
複製內容到剪貼板
代碼:
package com.xzw.jni;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
/**

@author XuZhiwei ([email protected])
* sina:http://weibo.com/xzw1989

* Create at 2012-8-30 上午10:49:45
*/
public class TestJni extends Activity { 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
    }
   //natvie必須聲明,用於生成C/C++代碼
    public native String hello();
   
    static{
            System.loadLibrary("testJni");
    }
    
}
編譯後的文件在bin目錄下,通過javah命令生成c/c++的文件頭。如下圖  


會在項目目錄下生成jni/com_xzw_jni_TestJni.h。
頭文件代碼如下:
複製內容到剪貼板
代碼:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_xzw_jni_TestJni */

#ifndef _Included_com_xzw_jni_TestJni
#define _Included_com_xzw_jni_TestJni
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     com_xzw_jni_TestJni
* Method:    hello
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_xzw_jni_TestJni_hello
  (JNIEnv *, jobject);

#ifdef __cplusplus
}
#endif
#endif
這裏我們可以根據頭文件編寫c代碼
複製內容到剪貼板
代碼:
#include <string.h>
#include <jni.h>


jstring
Java_com_xzw_jni_TestJni_hello
  (JNIEnv* env, jobject thiz){
          return (*env)->NewStringUTF(env, "哈哈完成自動化編譯 !");
}
接下來編寫 Android.mk,該文件可以直接從NDK的samples下的hello-jni的jni文件下直接靠過來改改就可以了。也貼下代碼哈。
複製內容到剪貼板
代碼:
# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := testJni
LOCAL_SRC_FILES := testJni.c

include $(BUILD_SHARED_LIBRARY)
其中你只需要該LOCAL_MODULE和LOCAL_SRC_FILES就可以了。
說明:LOCAL_MODULE是描述模塊的,用來給java調用的模塊名,會生成對應的libtestJni.so
LOCAL_SRC_FILES就是源文件啦,多個文件空格隔開即可。
接下來,我們要開始編譯生成so文件咯。
打開gnustep的命令窗口,進入到項目底下,輸入$NDK/ndk-build命令,即可自動生成libs/armeabi/libtestJni.so文件。



接下來就是java調用了。直接上代碼
複製內容到剪貼板
代碼:
package com.xzw.jni;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
/**

@author XuZhiwei ([email protected])
* sina:http://weibo.com/xzw1989

* Create at 2012-8-30 上午10:49:45
*/
public class TestJni extends Activity { 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText(hello()); //這裏的hello() 就是調用c
        setContentView(tv);
    }

    public native String hello();
   
    static{
            System.loadLibrary("testJni");
    }
    
}
運行結果如下


以上就是jni的開發步驟了。
在這裏我還要補充下:
在我們開發過程中,改一個c/c++的文件,我們都要手動去編譯一下有點兒麻煩。這裏我們可以使用讓eclipse幫助我們自己編譯。

右擊jni工程的properties-->Builders-->NEW -->;Program 可以看到以下內容:
 


argument:--login -c "cd /e/myWorkSpace/android/hellJni && $NDK/ndk-build"
切換到Refresh 標籤頁
 

切換到Build Options標籤頁
 


這樣就完成了配置,點擊確定可看到控制檯自動編譯程序了
 


能力有限,請大家多多指教。附上源碼  hellJni.rar (682.35 KB) 。歡迎關注我的博客:
http://home.51cto.com/index.php?s=/space/978424
發佈了15 篇原創文章 · 獲贊 0 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章