天天看点

android之JNI开发步骤总结

<a href="http://blog.51cto.com/attachment/201208/231746940.png" target="_blank"></a>

下载安装后,验证是否成功。打开GNUstep-&gt;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 &lt;jni.h&gt; 

/* 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 &lt;string.h&gt; 

jstring 

Java_com_xzw_jni_TestJni_hello 

  (JNIEnv* env, jobject thiz){ 

      return (*env)-&gt;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,如需转载请自行联系原作者

继续阅读