现在的位置: 首页 > 综合 > 正文

windows 平台下 android 搭建 ndk 环境

2013年10月07日 ⁄ 综合 ⁄ 共 2865字 ⁄ 字号 评论关闭

1.首先是下载NDK,解压,如,E:\android-ndk-r5

2.下载Cygwin,因为要编译c文件,建议从163的站点下载,速度还可以。。。选择安装项的时候,一定要选择Devel,全部安装吧(将Default左键点击成Install),配置环境变量,在Cygwin的安装目下的/home/用户名/.bash_profile文件的末尾添加

NDK=/cygdrive/E/android-ndk-r5/
export NDK

(注意:1.E/android-ndk-r5/为你自己的NDK目录,冒号变成/

2.NDK名字为任意,建议简单,其目的主要是为了代替你的ndk目录,即$NDK=E://android-ndk-r5/

3.下载之后测试一下是否成功make -v,gcc -v,都可以显示对应版本后,确定安装成功

4.新建一个android工程,在根目录下新建目录jni,在下面新建c文件,文件名为native.c

/*
 * 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.
 *
 */
#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_onerain_gj_MainActivity_getStringFromNative( JNIEnv* env, jobject thiz )
{
    return (*env)->NewStringUTF(env, "Hello Native Function~!");
}

5.在和c文件同目录下新建配置文件Android.mk,必须,如果是其他名字则会编译错误,简单格式如下,(针对本例,如果需要复杂的,可参考ndk/samples/下的例子)

# 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    := native
LOCAL_SRC_FILES := native.c

include $(BUILD_SHARED_LIBRARY)

6.编译c文件,从Cygwin命令行进入到刚才新建的android工程目录下的jni目录下,输入命令:$NDK/ndk-build,(即调用你本机下ndk目录里的ndk-build工具进行编译)

7.如果一切按着上述步骤,会在工程目录下生成一个libs的目录,里面会有一个子目录armeabi,其目录下会生成一个libnative.so,即我们需要的ndk动态库(为啥叫libnative.so呢?因为在Android.mk文件中是这么配置的,当然,你可以配置任何你喜欢的名字

LOCAL_MODULE    := native

8.在你想要使用NDK库的地方,比如Activity或者其他java类中,声明,使用如下:

package onerain.gj;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity
{
	/** Called when the activity is first created. */
	private TextView textView = null;
	
	public native String getStringFromNative();
	
	static
	{
		System.loadLibrary("native");
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	
		textView = (TextView)findViewById(R.id.textView);
		textView.setText(getStringFromNative());
	}
}

9.运行,即可看到效果。。。复杂的关于文件读写,还有OpenGL之类的研究之后会写上,当然会尽快补上一片配置eclipse,不用每次修改完c文件都要手动编译

PS:如果觉得表示不清楚,请大家提意见,欢迎留言!

抱歉!评论已关闭.