Process

– NDK Only works on > API 3
– script: ndk-build – generates binaries and adds those binaries to the project path
– Even if your Android app has no Java src code, .dex will still be created.

– ‘native’ keyword: Compiler hint to look for implementation in native library than in Java code.

– .so files: Native shared libraries

– .a files: Link to other native library

– JNI: Java Native Interface; Bridge between native and Java

– ABI: Application Binary Interface; describes how your application’s machine code will interact with ARM, MIPs and x86 hardware.

– NativeActivity: Declare this in the Android Manifest if your app has no Java Activities

– Android.mk (REQUIRED): Module Config file for ndk-build script; references src files, build flags and libs.

– Application.mk (OPTIONAL): Manifest of modules the app needs like ABI, tool chains and standard libs.

Workflow

Step 1):
Design your app, deciding which parts to implement in Java, and which parts to implement as native code.

Step 2):
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 NativeActivity class in AndroidManifest.xml. You can do so from the Eclipse/ADT Android Manifest Editor, or by hand-editing the file. For more information, see the Native Activity section.

Step 3):
Create an Android.mk file describing the native library, including name, flags, linked libraries and source files to be compiled in the ‘JNI’ directory.

Step 3b) OPTIONAL: Create an Application.mk file configuring the target ABIs, toolchain, release/debug mode, and STL. For any of these that you do not specify, the following default values are used, respectively:
ABI: armeabi
Toolchain: GCC 4.6
Mode: Release
STL: system

Step 4):
Place your native source under the project’s jni directory.

Step 5)
Use ndk-build to compile the native (.so, .a) libraries.

Step 6)
Build the Java component, producing the executable .dex file.

Step 7)
Package everything into an APK file, containing .so, .dex, and other files needed for your app to run.
Note that Eclipse can perform steps 5. through 7. in a single operation.

Native Activities and Applications

– Declare NativeActivity in the manifest if you have no Java in your Android app
– Limitations: Sensors, input events, and assets need native interfaces to access this data instead of JNI
– native_activity.h: Native activity class that contains callback and other data structs; Callbacks must be non-blocking otherwise ANR pops up; Src can be found at /platforms/android-9/arch-arm/usr/include/android/native_a ctivity.h
– android_native_app_glue.h: Util for callbacks, iputs events, etc; These types of events need to be in a seperate thread to prevent blocking main thread; Src is at /sources/android/native_app_glue/android_native_app_glue.c

[Using the native-activity.h interface]

To create a Java-less Native app:

1) Create project in eclipse
2) Delete pre-generated Activity
3) Select Project -> Right Click -> Android Tools -> Add Native Support
4) AndroidManifest.xml needs to configured to -not- have code

5) Name of the activity must be android.app.NativeActivity

6) Add meta-data that references the shared library that is the entry point to the native app

   <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>

7) Define the native activity in native code. The equivalent of main() function is ANativeActivity_onCreate()
8) Set the ANativeActivity->;instance field to the address of any instance of specific data that you want to use
9) Implement anything else that you want your activity to do upon starting
10) Implement the rest of the callbacks that you set in ANativeActivity->;callbacks

11) Develop the rest of your application.

12) Create an Android.mk file in the jni/ directory of your project to describe your native module to the build system; (See section on Android.mk)
13) Commandline to root of project, Compile using ‘ndk-build’ command
14) Deploy /bin/*.apk

Share

Google+ Comments