Android with the NDK_001

[size=large]1.设置环境[/size]
Microsoft Windows 系统下
 Java JDK 6
 Apache ANT Build System
 Android SDK
 Cygwin http://cygwin.com/install.html
 Android NDK
 Eclipse IDE

Apple Mac OS X系统下
 Xcode
 Java JDK 6
 Apache ANT Build System
 GNU Make
 Android SDK
 Android NDK
 Eclipse IDE

Ubuntu Linux系统下
 Java JDK 6
 Apache ANT Build System
 GNU Make
 Android SDK
 Android NDK
 Eclipse IDE
You can check the GNU C Library version by executing ldd --version on a Terminal window,
ldd --version 检测gnu c库的版本
sudo apt-get install ia32-libs-multiarch,
激活32位的软件包支持 在64位的系统上
apt-get install make make –version

[size=large]2.浏览NDK[/size]
The Android NDK is not a single tool;
cross-compilers, linkers, debuggers, build tools, documentation, and sample applications. The following are some of the key components of Android NDK:
 ARM, x86, and MIPS cross-compilers
 Build system
 Java Native Interface headers
 C library
 Math library
 POSIX threads
 Minimal C++ library
 ZLib compression library
 Dynamic linker library
 Android logging library
 Android pixel buffer library
 Android native application APIs
 OpenGL ES 3D graphics library
 OpenSL ES native audio library
 OpenMAX AL minimal support
NDK的构建
[color=red]ndk-build:[/color] This shell script is the starting point of the Android NDK build system.
[color=red]ndk-gdb:[/color] This shell script allows debugging native components using the GNU Debugger.
[color=red]ndk-stack: [/color]This shell script helps facilitate analyzing the stack traces that are produced when native components crash.
[color=red]build: [/color]This directory contains the modules of the entire Android NDK build system.
[color=red]platforms: [/color]This directory contains header files and libraries for each supported Android target version.
[color=red]samples:[/color] This directory contains sample applications to demonstrate the capabilities provided by the Android NDK.
[color=red]sources: [/color]This directory contains shared modules that developers can import into their existing Android NDK projects.
[color=red]toolchains: [/color]This directory contains cross-compilers for different target machine architectures that the Android NDK currently supports.

[size=large]3.在eclipse中引入ndk的例子工程[/size]
Android Tools menu item, and choose “Add Native Support” from the context menu.
要添加 Add Native Support 支持

[size=large]4.Building from the Command Line [/size]
命令行编译 首先要进入例子工程的根目录
然后 ndk-build开始编译
android update project –p . –n hello-jni –t android-14 --subprojects
可以生成 ant编译的工程

jni: 此目录包括 Android.mk文件 告诉编译器怎么样编译 还有本地源码文件


[size=large]5.Building Multiple Shared Libraries[/size]
构建多个共享库
LOCAL_PATH := $(call my-dir)
#
# Module 1
#
include $(CLEAR_VARS)
LOCAL_MODULE := module1
LOCAL_SRC_FILES := module1.c
include $(BUILD_SHARED_LIBRARY)

#
# Module 2
#
include $(CLEAR_VARS)
LOCAL_MODULE := module2
LOCAL_SRC_FILES := module2.c
include $(BUILD_SHARED_LIBRARY)
将会生成如下两个 so库
libmodule1.so and libmodule2.so

[size=large]6.Building Static Libraries[/size]
构建静态库
静态库可以编译成共享库
LOCAL_PATH := $(call my-dir)
#
# 3rd party AVI library #
include $(CLEAR_VARS)
LOCAL_MODULE := avilib
LOCAL_SRC_FILES := avilib.c platform_posix.c
include $(BUILD_STATIC_LIBRARY)
#
# Native module
#
include $(CLEAR_VARS)
LOCAL_MODULE := module
LOCAL_SRC_FILES := module.c
LOCAL_STATIC_LIBRARIES := avilib
include $(BUILD_SHARED_LIBRARY)

Application.mk
是可选的
APP_MODULES:
APP_OPTIM:
APP_CLAGS:
APP_CPPFLAGS:
APP_BUILD_SCRIPT:
APP_ABI:
APP_STL:


ndk-build –j 4


javah, to automate this task. The javah tool parses a Java class file for native methods
当我们导入的ndk工程 javah 已经自动执行了
javah –classpath bin/classes com.example.hellojni.HelloJni
The javah tool will parse the com.example.hellojni.HelloJni class file, and it will generate the
C/C++ header file as com_example_hellojni_HelloJni.h
生成c或c++的头文件:

#ifndef _Included_com_example_hellojni_HelloJni
#define _Included_com_example_hellojni_HelloJni
#ifdef __cplusplus
extern "C" {
#endif /*
* Class: com_example_hellojni_HelloJni
* Method: stringFromJNI
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI
(JNIEnv *, jobject);
/*
* Class: com_example_hellojni_HelloJni
* Method: unimplementedStringFromJNI
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_unimplementedStringFromJNI
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif


c或者c++文件要实现头文件中的方法
#include "com_example_hellojni_HelloJni.h"
JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI
(JNIEnv * env, jobject thiz)
{
return (*env)->NewStringUTF(env, "Hello from JNI !");
}



[size=large]Running from Eclipse IDE[/size]
在eclipse中运行的方法
Open the Eclipse IDE, and choose Run ➤ External Tools External Tools Configurations from the top menu bar. Using the External Tools Configurations dialog, select Program, and then click the New launch configuration button. Using the Main tab, fill in the tool information as follows

 Name: Generate C and C++ Header File
 Location: ${system_path:javah}
 Working Directory: ${project_loc}/jni
 Arguments: -classpath "${project_classpath};${env_var:ANDROID_SDK_HOME}/ platforms/android-14/android.jar" ${java_type_name}


[size=large]Method Declarations[/size] 方法解释
stringFromJNI java中的方法 没有带任何参数
JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI
(JNIEnv *, jobject);
带了两个参数
第一个参数都是JNIEnv*env,它代表了VM里的环境,本地代码可以通过这个env指针对Java代码进行操作,例如:创建Java类对象,调用Java对象方法,获取Java对象属性等。jobject obj相当于Java中的Object类型,它代表调用这个本地方法的对象,例如:如果有new NativeTest.CallNative(),CallNative()是本地方法,本地方法第二个参数是jobject表示的是NativeTest类的对象的本地引用。

C中的代码:
return (*env)->NewStringUTF(env, "Hello from JNI !");

C++中的代码:
return env->NewStringUTF("Hello from JNI !");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值