`
abc20899
  • 浏览: 929304 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android with the NDK_001

 
阅读更多
1.设置环境
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

2.浏览NDK
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的构建
ndk-build: This shell script is the starting point of the Android NDK build system.
ndk-gdb: This shell script allows debugging native components using the GNU Debugger.
ndk-stack: This shell script helps facilitate analyzing the stack traces that are produced when native components crash.
build: This directory contains the modules of the entire Android NDK build system.
platforms: This directory contains header files and libraries for each supported Android target version.
samples: This directory contains sample applications to demonstrate the capabilities provided by the Android NDK.
sources: This directory contains shared modules that developers can import into their existing Android NDK projects.
toolchains: This directory contains cross-compilers for different target machine architectures that the Android NDK currently supports.

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

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

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


5.Building Multiple Shared Libraries
构建多个共享库
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

6.Building Static Libraries
构建静态库
静态库可以编译成共享库
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 !");
}




Running from Eclipse IDE
在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}


Method Declarations  方法解释
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 !");





















分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics