在windows 上构建android NDK 开发环境

What

Android Native Development Kit

why

我们使用NDK很多情况是因为我们对以前c/c++代码的移植,或者对一些第三方开源native code的利用。

How

1.构建开发环境。

 1.0  构建Android 开发环境,这个我们稍后会讲到.

 1.1 android的NDK开发一般是基于linux 开发。这里我们基于window做,需要一个 叫做CYGWIN的环境。它是可以在windows平台上运行的unix模拟环境。cygwin 镜像站: http://cygwin.com. 建议从日本server 安装,速度好像快一点,可能是个人网络原因。配置cygwin的 "C:\cygwin\home\/%youName%/\.bash_profile"下面android NDK 路径。

 1.2.下载 android ndk 的开发包。我当前用到的是 android-ndk-r7

2. 用一个例子说明如何使用。

  2.0. 构建java 工程,放在NDK 下面APP文件下。这里我们借用ndk smaple下面一个例子。code如下。

/*
 * 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 HelloTest 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");
    }
}

 这里我们要注意几个point,public native String  stringFromJNI(), 另一个就是static {},这个会先于Oncreat构建。调用hello-jni.so.即ndk-build 编译出的动态库。

 

2.1. 生成相应的.h文件。

在android工程下在工程目录下建立一个jni文件夹。然后启动cygwin console。

$ cd $NDK 指向到我们的ndk目录,

$ cd apps/hellotest  指向工程目录。

在bin目录中已经生成了HelloTest.class的前提下。我们使用 $ javah -classpath bin -d jni dogfood.test.app.HelloTest,可以在jni目录下生成一个.H 文件。打开该文件。找到 JNIEXPORT jstring JNICALL Java_dogfood_test_app_HelloTest_stringFromJNI
  (JNIEnv *, jobject); 方法。具体这个方法解释,参阅GOOGLE微笑.

2.2. 编写c++ 文件。

 

#include <string.h>
#include <jni.h>

/* This is a trivial JNI example where we use a native method
 * to return a new VM String. See the corresponding Java source
 * file located at:
 *
 *   apps/samples/hello-jni/project/src/com/example/HelloJni/HelloJni.java
 */
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
    return (*env)->NewStringUTF(env, "Hello from JNI !");
}

2.3 编写Android.mk 文件。

 

 

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c

include $(BUILD_SHARED_LIBRARY)


2.4 我们来生成.so 文件吧。

 

$ ../../ndk-build,在obj目录下我们找到一个libhello-jni.so的文件,这就是我们要生成的动态库。

接下,在eclips下编译可以应用这个SO 文件。


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值