標籤:jni .so gnustep 動態庫
研究了很久怎麼編譯.so動態庫,感覺在linux的安卓源碼下編譯太麻煩了,所以就找網上找找有沒有方便的辦法。
現在終於實現了,現在一起總結下。
1.在windows環境下開發jni需要c/c++編譯器的支援,使用GNUStep,http://www.gnustep.org/windows/installer.html。
2.下載安裝後,驗證是否成功。開啟GNUstep->Shell,輸入make -v 和 gcc -v命令,。
3.下載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,就可以找到對應的盤符。
驗證環境變數,如。
以上就配置成功了。
4.
開啟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的開發步驟了。
5.jni自動編譯設定
在我們開發過程中,改一個c/c++的檔案,我們都要手動去編譯一下有點兒麻煩。這裡我們可以使用讓eclipse協助我們自己編譯。
右擊jni工程的properties-->Builders-->NEW -->;Program 可以看到以下內容:
argument:--login -c "cd /e/myWorkSpace/android/hellJni && $NDK/ndk-build"
切換到Refresh 標籤頁
切換到Build Options標籤頁
這樣就完成了配置,點擊確定可看到控制台自動編譯器了
android之JNI開發環境搭建