<a href="http://blog.51cto.com/attachment/201208/231746940.png" target="_blank"></a>
下載下傳安裝後,驗證是否成功。打開GNUstep->Shell,輸入make -v 和 gcc -v指令,如圖所示。
<a href="http://blog.51cto.com/attachment/201208/231327684.png" target="_blank"></a>
配置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 MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public native String hello();
static{
System.loadLibrary("testJni");
}
編譯後的檔案在bin目錄下,通過javah指令生成c/c++的檔案頭。如下圖
<a href="http://blog.51cto.com/attachment/201208/110018262.png" target="_blank"></a>
會在項目目錄下生成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);
根據頭檔案編寫c代碼
#include <string.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檔案。
<a href="http://blog.51cto.com/attachment/201208/110945188.png" target="_blank"></a>
接下來就是java調用來。直接上代碼
public class TestJni extends Activity {
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText(hello());
setContentView(tv);
運作結果如下
<a href="http://blog.51cto.com/attachment/201208/111208492.png" target="_blank"></a>
本文轉自xuzw13 51CTO部落格,原文連結:http://blog.51cto.com/xuzhiwei/976839,如需轉載請自行聯系原作者