MAC下NDK环境搭建及hello world程序

来源:互联网 发布:cmd telnet 端口 编辑:程序博客网 时间:2024/06/08 10:56

MAC下NDK环境搭建及hello world程序

本人用的是Eclipse,先搭建安卓开发环境(网上有很多教程),然后配置NDK开发环境:

1.下载NDK,网址:http://developer.android.com/tools/sdk/ndk/index.html 

2.安装NDK:我们从官网下载下来的安装文件是二进制文件(android-ndk-r10e-darwin-x86_64.bin),这个文件不能常规解压,解压方法:命令行下进入安装文件所在目录,执行两条命令:

ndk$ chmod a+x android-ndk-r10c-darwin-x86_64.bin

ndk$ ./android-ndk-r10c-darwin-x86_64.bin

解压完成后,会在安装文件所在目录下生成文件夹:android-ndk-r10e

3.配置环境变量:

命令下修改文件.bash_profilevim ~/.bash_profile

在里面增加:

export PATH=${PATH}:/Users/sample/android-ndk-r10e
ANDROID_NDK_ROOT=/Users/sample/android-ndk-r10e
export ANDROID_NDK_ROOT

保存退出该文件::wq

4.测试配置是否成功:

命令行下执行命令:ndk-build

如果成功,会打印:

mintekiMac:client_android mining$ ndk-build
Android NDK: Could not find application project directory !    
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.    
/Users/sample/android-ndk-r10e/build/core/build-local.mk:143: *** Android NDK: Aborting    .  Stop.
mintekiMac:client_android mining$ 


接下来,我们写一个helloworld的小例子:

1.新建一个常规的安卓工程:MyNDK,包名:com.example.ndkdemo

2.配置一下android NDK 的路径,具体操作就是->eclipse  ->preferences ->android ->NDK 然后再配置自己的NDK路径,我的路径:/Users/sample/android-ndk-r10e

3.添加NDK支持.

具体操作:  右键你的项目 ->android tools ->Add Native Support, 命名MyNDK(自由命名)。一下子就OK了,在你的项目下面会生成jni文件夹,里面有两个文件:

MyNDK.cpp和Android.mk

到这里,就建立了一个NDK的project了.

4.接下来写java代码:

public class MainActivityextends Activity {

private TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView = (TextView) findViewById(R.id.text);

textView.setText(hello()); //调用本地方法

}

}

public native String hello();//声明本地方法

static {//加载库

System.loadLibrary("MyNDK");

}

}


xml:

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"

    android:paddingLeft="@dimen/activity_horizontal_margin"

    android:paddingRight="@dimen/activity_horizontal_margin"

    android:paddingTop="@dimen/activity_vertical_margin"

    tools:context=".MainActivity">


    <TextView

        android:id="@+id/text"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/hello_world"/>


</RelativeLayout>



c代码:

MyNDK.cpp:

#include <jni.h>

#include <string.h>

#include <stdio.h>

//方法命名规则:Java开头+包名(.用下划线代替) + 类名 + 方法名

extern "C" {

JNIEXPORT   jstring   Java_com_example_ndkdemo_MainActivity_hello(JNIEnv* env,jobject thiz);

}

JNIEXPORT jstring

Java_com_example_ndkdemo_MainActivity_hello(JNIEnv* env,jobject thiz) {

    return env->NewStringUTF("hellondk ");

}


Android.mk:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := MyNDK

LOCAL_SRC_FILES := MyNDK.cpp

include $(BUILD_SHARED_LIBRARY)


5.编译原生代码:命令行进入安卓工程MyNDK目录,执行:ndk-build,成功后会在lib下面生成libMyNDK.so文件


6.运行安卓工程:方法同普通的安卓工程运行方法一样。我们会在手机屏幕看到:hello ndk。



可能会遇到的错误:

Type '*****' could not be resolved
Method '******' could not be resolved
等等很多诸如此类的错误


解决方法:
是由于没有将jni.h导入的缘故,而这个文件在ndk的目录下面。所以,参照以下步骤:
Project Properties -> C/C++ General -> Path and Symbols
选择include标签,Add -> $Android_NDK_HOME/platforms/android-14/arch-arm/usr/include
且选中All languages.
最后Apply -> OK
这样错误就解决了。

 

如果添加了头文件还是问题,那可能是因为工程是C++工程,但代码是用的C风格的代码,解决方法为将工程文件".project"中的“<nature>org.eclipse.cdt.core.ccnature</nature>”行去掉保存,重新打开Eclipse工程。参见:

http://stackoverflow.com/questions/11666711/type-jint-could-not-be-resolved-and-jnienv-jclass


还有一种方法:在extern "C"的大括号中加入#incldue <jni.h>




0 0
原创粉丝点击