NDK底层开发

来源:互联网 发布:重装系统那个软件好 编辑:程序博客网 时间:2024/05/02 10:12
                                                      NDK底层开发

 

       NDK全称为Native Development Kit,Android NDK的开发工具包面向底层开发人员,可以让Android平台支持Native C原生代码的开发。NDK使得java+c的开发变得方便可行。因为我们知道C程序的运行效率远远比java程序的运行效率高,所以我们可以把一些复杂的算法用C来实现,并通过JNI技术使得上层的java程序可以调用用C来实现的函数。其实除了效率方面,加入C的开发还有另外两个特点,一是JAVA在编程时有很多局限性,一些功能用C来实现变得很简单,二是C编写的代码通过NDK生成的静态库*.SO文件更具安全性。

Windows下NDK开发环境的搭建:

1、安装cygwin

NDK编译用到cygwin中的make和gcc,所以要先下载并安装Cygwin。

登录网址www.cygwin.com网站,单击其中的“Install or update now”链接,将下载一个名为setup.exe的安装文件。下载完后双击setup.exe启动安装向导,注意要认真看向导的提示,选择合适的选项安装,特别要提醒的是当进入Cygwin Setup – Select Packages安装向导时,为了保险起见,你可以单击Devel节点处的循环箭头,将Default切换为Install,NDK需要的make和gcc就是在Devel节点下。安装完成后启动Cygwin,输入make –v 和gcc –v命令检查是否安装成功。

 

2、下载Android NKD包:

因为Android开发者网站(http://developer.android.com/sdk/ndk/index.html)很多时候在国内是无法访问的,所以我们可以去一个国内可以下载的网址:http://dl.google.com/android/ndk/android-ndk-r3-windows.zip。如果你想下更高版本的如r4版本的,你可以将上面的地址改为http://dl.google.com/android/ndk/android-ndk-r4-windows.zip。

 

3、配置NDK环境:

安装完Cygwin和NDK包以后,我们就可以用NDK来编译一个简单的例子了,首先我们需要设置一下环境变量,方便我们以后的命令操作。

      先进入Cygwin的安装目录C:\cygwin\home\Administrator找到.bash_profile文件,用UltraEdit打开,并在文档的最后加上如下两行代码:

ANDROID_NDK_ROOT=/cygdrive/d/android-ndk-r3

export ANDROID_NDK_ROOT

其中第一行代码的路径是你存放NDK的路径。

   然后重启Cygwin,执行如下命令配置:

      最后执行如下编译命令就可以产生静态库文件libhello-jni.so,他是存放在D:\android-ndk-r3\apps\hello-jni\project\libs\armeabi,在D:\android-ndk-r3\out目录先也会多一个apps文件夹,这里存放着一些共享库。

 

注:NDK开发环境不经可以通过Cygwin在Window环境下搭建,也可以直接在linux环境下搭建。

 

Eclipse中使用SO库文件:

4、在Eclipse中使用静态库*.so

在Eclipse中新建一个工程HelloJni,将D:\android-ndk-r3\apps\hello-jni\project目录下的jni和libs文件夹拷贝到新建工程目录下,注意这两个文件夹要和工程中的src和res文件统一目录。然后进入Eclipse中刷新工程,HelloJni.java中的代码如下:

/*

 * 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.

 */

package com.example.hellojni;

 

import android.app.Activity;

import android.widget.TextView;

import android.os.Bundle;

 

 

public class HelloJni extends Activity

{

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

 

        /* Create a TextView and set its content.

         * the text is retrieved by calling a native

         * function.

         */

        TextView  tv = new TextView(this);

        tv.setText( stringFromJNI() );

        setContentView(tv);

    }

 

    /* A native method that is implemented by the

     * 'hello-jni' native library, which is packaged

     * with this application.

     */

    public native String  stringFromJNI();

 

    /* This is another native method declaration that is *not*

     * implemented by 'hello-jni'. This is simply to show that

     * you can declare as many native methods in your Java code

     * as you want, their implementation is searched in the

     * currently loaded native libraries only the first time

     * you call them.

     *

     * Trying to call this function will result in a

     * java.lang.UnsatisfiedLinkError exception !

     */

    public native String  unimplementedStringFromJNI();

 

    /* this is used to load the 'hello-jni' library on application

     * startup. The library has already been unpacked into

     * /data/data/com.example.HelloJni/lib/libhello-jni.so at

     * installation time by the package manager.

     */

    static {

        System.loadLibrary("hello-jni");

    }

}

最后运行我们就可以在虚拟机上看到hello-jni.c函数中输入的字符窜。如果你看到了效果,你就可以试着改改C程序和JAVA程序来再次熟悉NDK开发的整个过程了。

http://blog.ednchina.com/laizibin315/1894573/Message.aspx   此界面有文档