Android NDK纯C++开发

继续了解使用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.mk  
LOCAL_PATH := $(call my-dir)  
  
include $(CLEAR_VARS)  
  
LOCAL_MODULE    := test  
LOCAL_SRC_FILES := main.cpp  
LOCAL_LDLIBS    := -landroid  
LOCAL_STATIC_LIBRARIES := android_native_app_glue  
  
include $(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++程序的基本组成

android职业交流QQ 群,有兴趣的可以一起来多搞搞技术、职业交流,互相学习、提高,互通好的职业信息,群号:104286694

猜你喜欢

转载自iaiai.iteye.com/blog/2231803