繼續瞭解使用native_app_glue來編寫純C++的Android NDK開發。
下面從一個"最簡單“的可啟動並執行程式來瞭解native_app_glue程式的基本組成。
1. 源碼main.cpp:
// main.cpp#include <android_native_app_glue.h>/** * This is the main entry point of a native application that is using * android_native_app_glue. It runs in its own thread, with its own * event loop for receiving input events and doing other things. */void android_main(struct android_app* state) {// Make sure glue isn't stripped.app_dummy();}
首先,需要包含標頭檔android_native_app_glue.h,然後,必須實現一個android_main()函數,否則是無法啟動並執行(編譯可能沒有問題)。另外,在android_main裡面必須至少調用app_dummy(),這個函數的作用是為了保證glue沒有被最佳化掉,參考android_native_app_glue.h中的說明:
/** * Dummy function you can call to ensure glue code isn't stripped. */void app_dummy();
2. Android.mk
# Android.mkLOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := testLOCAL_SRC_FILES := main.cppLOCAL_LDLIBS := -landroidLOCAL_STATIC_LIBRARIES := android_native_app_glueinclude $(BUILD_SHARED_LIBRARY)$(call import-module,android/native_app_glue)
這裡可見和一般的NDK程式的Android.mk的區別在於:
(1) 需要增加LOCAL_LDLIBS=-landroid
(2) 增加LOCAL_STATIC_LIBRARIES=android_native_app_glue
(3) 增加$(call import-module,android/native_app_glue)
這就是和一般的NDK程式的makefile的區別,另外,這裡的LOCAL_MODULE=test,下面會用到。
3. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><!-- BEGIN_INCLUDE(manifest) --><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.native_activity" android:versionCode="1" android:versionName="1.0"> <!-- This is the platform API where NativeActivity was introduced. --> <uses-sdk android:minSdkVersion="14" /> <!-- This .apk has no Java code itself, so set hasCode to false. --> <application android:label="@string/app_name" android:hasCode="false"> <!-- Our activity is the built-in NativeActivity framework class. This will take care of integrating with our NDK code. --> <activity android:name="android.app.NativeActivity" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden"> <!-- Tell NativeActivity the name of or .so --> <meta-data android:name="android.app.lib_name" android:value="test" /> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest> <!-- END_INCLUDE(manifest) -->
基本上和一般的AndroidManifest檔案也差不多,需要注意的是:
(1) Activity的名稱必須為NativeActivity
一般的程式Activity就是自己建立的Activity的名稱,這裡必須為NativeActivity
<activity android:name="android.app.NativeActivity" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden">
(2) 增加對JNI中庫的引用
<!-- Tell NativeActivity the name of or .so --> <meta-data android:name="android.app.lib_name" android:value="test" />
一般的程式不需要這一部分,但是對於純C++開發,需要指定其meta data,這裡的test就是上面的Android.mk中的module的名稱,要保持一致。
總結:上面的程式是一個”最簡單“的純C++的Android應用的例子,編譯之後就可以運行了,如果上面的某一個地方錯誤,就會導致無法編譯或運行。其中重點要理解的是android_main和android_dummy函數,另外,上面的程式儘管可以編譯運行,但是並不具有實際意義,因為它不能響應任何事件,會導致無法響應。下面討論更一般的情況,瞭解一般的程式的組成。
重點理解:
android_main和android_dummy的作用
純C++程式的基本組成