android ndk 内容
2015-10-10 09:36
330 查看
Concepts
On this page
Before BeginningIntroduction
How It Works
Native Activities and Applications
Before Beginning
This guide assumes假定 that you are:Already familiar with concepts inherent in native programming and in Android development.
Working in Eclipse, and using the Android Development Tools (ADT), except where otherwise noted.
Introduction
This section provides a high-level explanation of how the NDK works. The Android NDK is a set of tools allowing you to embed潜入 C or C++ (“native code”) into your Android apps. The ability to use native code in Androidapps can be particularly特别是 useful to developers
who wish to do one or more of the following:
Port(港口 端口n 持v) their apps between platforms.
Reuse重用 existing libraries, or provide their own libraries for reuse.
Increase performance in certain cases, particularly computationally计算 intensive密集 ones like games.
How it Works
This section introduces the main components used in building a native application for Android, and goes on to describe the process of building and packaging.
Main components
You should have an understanding of the following components as you build your app:ndk-build: The ndk-build script launches the build scripts at the heart of the NDK. These scripts:
Automatically probe探索 your development system and app project file to determine what to build.
Generate binaries.
Copy the binaries to your app's project path.
For more information, see ndk-build.
Java: From your Java source, the Android build process generates
.dex(Dalvik EXecutable) files, which are what the Android OS runs in the Dalvik Virtual Machine (“DVM”). Even if your app contains no Java
source code at all, the build process still generates a
.dexexecutable执行档 file within which the native component runs.
When developing Java components, use the
nativekeyword to indicate表明 methods implemented as native code. For example, the following function declaration tells
the compiler that the implementation is in a native library:
public native int add(int x, int y);
Native shared libraries: The NDK builds these libraries, or
.sofiles, from your native source code.
Note: If two libraries implement respective(各自的
分别的 单独的) methods with the same signature签名, a link error occurs. In C, "signature" means method name only.
In C++, "signature" means not only method name, but also its argument names and types.
Native static libraries: The NDK can also build static libraries, or
.afiles, which you can link against other libraries.
Java Native Interface (JNI): The JNI is the interface via which the Java and C++ components talk to one another. This guide assumes假定 knowledge of the JNI; for information about it, consult the Java
Native Interface Specification.
Application Binary Interface (ABI): The ABI defines exactly正确地 how your app's machine code is expected预期 to interact相互作用 with the system
at runtime. The NDK builds
.sofiles against these definitions定义. Different ABIs correspond to different architectures: The NDK includes ABI support for ARMEABI (default), MIPS, and x86. For more information,
see ABI Management.
Manifest: If you are writing an app with no Java component to it, you must declare the
NativeActivityclass
in the manifest. Native
Activities and Applications provides more detail on how to do this, under “Using the
native_activity.hinterface.”
The following two items are only required必需的 for building using the
ndk-buildscript,
and for debugging using the
ndk-gdbscript.
Android.mk: You must create an
Android.mkconfiguration
file inside your
jnifolder. The
ndk-buildscript looks at this file, which defines the module组件 and its name, the source files to be compiled, build flags and libraries to link.
Application.mk: This file enumerates列举 and describes the modules
that your app requires. This information includes:
ABIs used to compile for specific platforms.
Toolchains.
Standard libraries to include (static and dynamic动态 STLport or default system).
Flow流程
The general flow for developing a native app for Android is as follows:Design your app, deciding which parts to implement in Java, and which parts to implement as native code.
Note: While it is possible to completely avoid避免 Java, you are likely to find the Android Java framework useful for tasks including controlling the display and UI.
Create an Android app Project in Eclipse as you would for any other Android project.
If you are writing a native-only app, declare the
NativeActivityclass
in
AndroidManifest.xml. You can do so from the Eclipse/ADT Android Manifest Editor, or by hand-editing the file. For more information, see theNative
Activities and Applications.
Create an
Android.mkfile describing the native library, including name, flags, linked libraries, and source files to be compiled in the ‘JNI’ directory.
Optionally可选择地, you can create an
Application.mkfile configuring the target ABIs, toolchain, release/debug mode, and STL(标准模板库(Standard Template Library)).
For any of these that you do not specify指定, the following default values are used, respectively:
ABI: armeabi
Toolchain: GCC 4.8
Mode: Release
STL: system
Place your native source under the project's
jnidirectory.
Use ndk-build to compile the native (
.so,
.a) libraries.
Build the Java component, producing生产 the executable可执行的
.dexfile.
Package everything into an APK file, containing
.so,
.dex, and other files needed for your app to run.
Note that Eclipse can perform steps 7. through 9. in a single operation.
Native Activities and Applications
The Android SDK provides a helper class, NativeActivity,
that allows you to write a completely native activity.
NativeActivityhandles the
communication between the Android framework and your native code, so you do not have to subclass it or call its methods. All you need to do is declare your application to be native in your
AndroidManifest.xmlfile, and begin creating your
native application.
An Android application using
NativeActivitystill
runs in its own virtual machine, sandboxed沙盒 from other applications. You can therefore因此 still access Android framework APIs through the JNI. In certain cases在某些特定的情况下, however–such as for sensors, input events, and assets–the
NDK provides native interfaces that you can use instead of having to call across the JNI. For more information about such support, see Android
NDK Native APIs.
Regardless of whether or not you are developing a native activity, we recommend建议 that you create your projects with the traditional Android build tools. Doing so helps ensure building and packaging of Android applications
with the correct structure.
The Android NDK provides you with two choices to implement your native activity:
The
native_activity.hheader defines the native version
of the
NativeActivityclass. It contains the callback interface and data structures
that you need to create your native activity. Because the main thread of your application handles the callbacks, your callback implementations must not be blocking. If they block, you might receive ANR (Application Not Responding) errors because your main
thread is unresponsive until the callback returns.
The
android_native_app_glue.hfile defines a static helper library built on top of the
native_activity.hinterface.
It spawns生成 another thread, which handles things such as callbacks or input events in an event loop. Moving these events to a separate thread prevents any callbacks from blocking your main thread.
The
<ndk_root>/sources/android/native_app_glue/android_native_app_glue.csource is also available, allowing you to modify the implementation.
For more information on how to use this static library, examine检查 the native-activity sample application and its documentation. Further reading is also available in the comments in the
<ndk_root>/sources/android/native_app_glue/android_native_app_glue.hfile.
Using the native_activity.h interface
To implement a native activity with thenative_activity.hinterface:
Create a
jni/directory in your project's root directory. This directory stores all of your native code.
Declare your native activity in the
AndroidManifest.xmlfile.
Because your application has no Java code, set
android:hasCodeto
false.
<application android:label="@string/app_name" android:hasCode="false">
You must set the
android:nameattribute of the activity tag to
NativeActivity.
<activity android:name="android.app.NativeActivity" android:label="@string/app_name">
Note: You can subclass
NativeActivity. If you do, use the name of
the subclass instead of
NativeActivity.
The
android:valueattribute of the
meta-datatag specifies指定 the name of the shared library containing the entry point to the application
(such as C/C++
main), omitting省略 the
libprefix前缀 and
.sosuffix from the library name.
<meta-data android:name="android.app.lib_name" android:value="native-activity" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Create a file for your native activity, and implement the function named in the
ANativeActivity_onCreatevariable变量.
The app calls this function when the native activity starts. This function, analogous(相似的
模拟的 ) to
mainin C/C++, receives a pointer to an
ANativeActivitystructure,
which contains function pointers to the various callback implementations that you need to write. Set the applicable callback function pointers in
ANativeActivity->callbacksto the implementations of your callbacks.
Set the
ANativeActivity->instancefield to the address of any instance of specific data that you want to use.
Implement anything else that you want your activity to do upon starting.
Implement the rest of the callbacks that you set in
ANativeActivity->callbacks. For more information on when the callbacks are called, see Managing
the Activity Lifecycle.
Develop the rest of your application.
Create an
Android.mk filein the
jni/directory of your project to describe your native module to the build system. For more information, see Android.mk.
Once you have an
Android.mkfile, compile your native code using
the
ndk-buildcommand.
$ cd <path>/<to>/<project> $ <ndk>/ndk-build
Build and install your Android project as usual, using Ant or Eclipse. If your native code is in the
jni/directory, the build script automatically packages the
.sofile(s) built from
it into the APK.
相关文章推荐
- Android NDK开发之:配置环境的详解
- Android开发的IDE、ADT、SDK、JDK、NDK等名词解释
- Android提高之使用NDK把彩图转换灰度图的方法
- 探讨:如何在NDK中呼叫Java的class
- Android App 增量更新实例(Smart App Updates)
- 每周总结20130814——Android NDK环境的搭建和使用,YUV420SP格式图像的处理
- Android NDK开发简介
- Android NDK开发之Jni的数据类型
- Android NDK开发之数组类型的操作
- Android NDK开发之Jni调用Java对象
- Getting Started With the NDK(Pro Android Apps Performance Optimization)
- Advanced NDK(Pro Android Apps Performance Optimization)
- Android新版NDK环境配置(免Cygwin)
- android ndk 怎样调用第三方的so库文件
- Android 开发 ndk相关问题
- Android: NDK编程入门笔记
- windows下eclipse搭建android_ndk开发环境
- 基于NDK的Android防破解
- Android ndk开发之在c文件里打印log
- Android NDK 之 NDK 预生库支持