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 !");
分享到:
相关推荐
android 的C和C++开发Pro Android C with the NDK
《Android C++高级编程使用NDK》是一本深入探讨如何在Android平台上利用C++进行高效开发的专业书籍。这本书源码的提供,对于开发者来说是一份宝贵的参考资料,它涵盖了多个章节的示例代码,帮助读者更好地理解和实践...
1.1.5 在Windows平台上下载并安装AndroidNDK 1.1.6 在Windows平台上下载并安装Eclipse 1.2 AppleMacOSX 1.2.1 在Mac平台上安装Xcode 1.2.2 验证Mac平台的Java开发包 1.2.3 验证Mac平台上的ApacheANT 1.2.4 验证GNU...
Pro Android C++ with the NDK Native Development Kit) is an advanced tutorial and professional reference for today's more sophisticated app developers now porting, developing or employing C++ and ...
Pro Android C++ with the NDK Copyright ? 2012 by Onur Cinar ISBN-13 (pbk): 978-1-4302-4827-9 ISBN-13 (electronic): 978-1-4302-4828-6 Content: Chapter 1: Getting Started with C++ on Android Chapter 2: ...
原书名:Pro Android C++ with the NDK 原出版社: Apress 作者: (美)Onur Cinar 译者: 于红 佘建伟 冯艳红 丛书名: 移动开发经典丛书 出版社:清华大学出版社 ISBN:9787302343011 上架时间:2013-12-30 出版...
完美解决 No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android,mips64el-linux-android-4.9
This book provides you with a number of clear step-by-step recipes which will help you to start developing mobile games with Android NDK and boost your productivity debugging them on your computer....
当出现"Error: No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android"时,意味着Android Studio无法找到对应的工具链来编译针对MIPS64处理器的代码。 首先,我们来理解...
根据文件信息,本篇知识点的主体将围绕Android原生应用程序开发中的日志记录、调试和故障排除工具与技术,以及Android NDK(Native Development Kit)提供的原生API进行讲解。同时,文档还将介绍Bionic C库,它是...
A systematic guide consisting of over 70 recipes which focus on helping you build portable mobile games and aims to enhance your game development skills with clear instructions.If you are a C++ ...