如何使用android的ndk建立native的开发环境

从网上看了一篇使用andriod的toolchain在cygwin上来建立android的开发环境,但是在vista上编译始终失败,在xp上能够成功。但是编译的时间比较长,而且对于新手来说也比较麻烦,难道就没有简单的方法吗?google已经把andriod的ndk已经放出来了,所以我就想着打它的主意了,把它配置一下,就能来开发c的程序了。旁边小伙肯定笑了,“搞啥?,有病啊,ndk就是一个开发native code的环境。”大哥,我当然知道了,虽然使用ndk来开发native code相对容易,但是它的.mk文件我看的是云里雾里,我本来想调用自己写的另外一个so库,都不知道在.mk文件里如何写,我现在也懒的去看ndk里面的mk文件,等哪天(哪天?天晓得是哪一天)有空了好好研究一下。好了,闲话少说,开练吧。首先安装cygwin,这个网上的教程多的是,就不说了,接着下载android ndk,这个在andriod的官网上就有了,然后下载一个从android模拟器里取system lib的工具busybox,然后调用命令

$adb push busybox /dev/sample/busybox

$adb shell chmod 777 /dev/sample/busybox

$adb shell ./dev/sample/busybox tar -cf /dev/sample/libs.tar /system/lib

$adb pull /dev/sample/libs.tar libs.tar

这样就将模拟器下的 /system/lib 目录的所有库(so)文件打包并下载下来了,解压libs.tar就得到了我们所需要的所有库文件。

接着将所有的文件copy 到 $(NDK)/build/prebuilt/windows/arm-eabi-4.2.1/lib/gcc/arm-eabi/4.2.1,好了,这个时候基本的配置工作就结束了,怎么样简单多了吧。

接着编写一个简单的c文件 tutorial01.c

#include <stdio.h>

int getinformation() 

{

return 0;

}

然后编写一个Makefile文件

CC = /cygdrive/f/software/android/android-ndk-1.5_r1/build/prebuilt/windows/arm-eabi-4.2.1/bin/arm-eabi-gcc

 

CFLAGS  = -g -O2 -fPIC -DANDROID -I ./ -I ../  -I F:/software/android/android-ndk-1.5_r1/build/platforms/android-1.5/arch-arm/usr/include

SDFLAGS = -nostdlib -Wl,-T,armelf.xsc -Wl,-soname,$@ -Wl,-shared,-Bsymbolic -lc 

CRT_OBJS= -lz -lm

all: libtutorial01.so

 

libtutorial01.so: tutorial01.o

$(CC) $(SDFLAGS) -o $@ tutorial01.o  $(CRT_OBJS)

 

tutorial01.o: tutorial01.c

然后make,这个时候会报错 can't find "armelf.xsc", 在ndk的目录里搜索一下,搜到之后copy 到$(NDK)/build/prebuilt/windows/arm-eabi-4.2.1/lib/gcc/arm-eabi/4.2.1,然后make,成功。这样一个简单的so文件就生成了,这个时候如果想在android的虚拟机上运行,我们还需要给它包装一下。再编写一个文件test01.c,在这里是使用dl动态加载so文件,静态加载始终有问题,搞不清楚android是如何搜索目录,而且现在只能用绝对路径,这个问题还得仔细研究研究。

#include <string.h>

#include <jni.h>

 

jint

Java_com_example_testffmpeg_testffmpeg_getinformation( JNIEnv* env,

                                                  jobject thiz )

{

void*  filehandle = dlopen("/data/data/com.example.test/lib/libtutorial.so", RTLD_LAZY );

int ll = -1;

if(filehandle)

{

int( * getinformation ) (); 

getinformation = dlsym(filehandle, "getinformation");

if( getinformation )

{

ll = getinformation();

}

else

{

ll = -3;

}

dlclose(filehandle);

filehandle=0;

}

else

{

ll = -2;

}

    return ll;

}

同样再来一个Makefile文件

CC = /cygdrive/f/software/android/android-ndk-1.5_r1/build/prebuilt/windows/arm-eabi-4.2.1/bin/arm-eabi-gcc

 

CFLAGS  = -g -O2 -fPIC -DANDROID  -I ./ -I ../  -I F:/software/android/android-ndk-1.5_r1/build/platforms/android-1.5/arch-arm/usr/include

SDFLAGS = -nostdlib -Wl,-T,armelf.xsc -Wl,-shared,-Bsymbolic -Wl,-soname,$@ -lc  -L ../tutorial

CRT_OBJS= -lz -lm  -ldl

all: libtest01.so

 

libtest01.so: test01.o

$(CC) $(SDFLAGS) -o $@ test01.o  $(CRT_OBJS)

 

 

ok, make一下成功。好了,接下来使用andriod的sdk写一个简单的activity, testapp来测试其运行情况,以下是test01.java的代码。

package com.example.test;

 

import android.app.Activity;

import android.widget.TextView;

import android.os.Bundle;

 

 

public class test01 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() );

        Integer ll = getinformation();

        String lls = ll.toString();

        tv.setText(lls);

        setContentView(tv);

    }

 

    /* A native method that is implemented by the

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

     * with this application.

     */

    public native int  getinformation();

     /* 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("test");

     }

}

 

在eclipse中运行,在模拟器上显示0,就表示成功了。

busybox在此下载https://sites.google.com/site/anakia/Home/android-native01/busybox?attredirects=0

例子代码在此下载:https://sites.google.com/site/anakia/Home/android-native01/samples.zip?attredirects=0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值