Android NDK纯C++开发(2)

来源:互联网 发布:淘宝用打印机什么牌子 编辑:程序博客网 时间:2024/05/22 00:54

继续了解使用native_app_glue来编写纯C++的Android NDK开发。

下面从一个"最简单“的可运行的程序来了解native_app_glue程序的基本组成。

1. 源码main.cpp:

[cpp] view plaincopyprint?
  1. // main.cpp 
  2. #include <android_native_app_glue.h> 
  3.  
  4.  
  5. /**
  6. * This is the main entry point of a native application that is using
  7. * android_native_app_glue.  It runs in its own thread, with its own
  8. * event loop for receiving input events and doing other things.
  9. */ 
  10. void android_main(struct android_app* state) { 
  11. // Make sure glue isn't stripped. 
  12. app_dummy(); 


首先,需要包含头文件android_native_app_glue.h,然后,必须实现一个android_main()函数,否则是无法运行的(编译可能没有问题)。另外,在android_main里面必须至少调用app_dummy(),这个函数的作用是为了保证glue没有被优化掉,参考android_native_app_glue.h中的说明:

[cpp] view plaincopyprint?
  1. /**
  2. * Dummy function you can call to ensure glue code isn't stripped.
  3. */ 
  4. void app_dummy(); 


2. Android.mk

[cpp] view plaincopyprint?
  1. # Android.mk 
  2. LOCAL_PATH := $(call my-dir) 
  3.  
  4. include $(CLEAR_VARS) 
  5.  
  6. LOCAL_MODULE    := test 
  7. LOCAL_SRC_FILES := main.cpp 
  8. LOCAL_LDLIBS    := -landroid 
  9. LOCAL_STATIC_LIBRARIES := android_native_app_glue 
  10.  
  11. include $(BUILD_SHARED_LIBRARY) 
  12.  
  13. $(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

[cpp] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <!-- BEGIN_INCLUDE(manifest) --> 
  3. <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
  4.         package="com.example.native_activity" 
  5.         android:versionCode="1" 
  6.         android:versionName="1.0"
  7.  
  8.     <!-- This is the platform API where NativeActivity was introduced. --> 
  9.     <uses-sdk android:minSdkVersion="14" /> 
  10.  
  11.     <!-- This .apk has no Java code itself, so set hasCode tofalse. --> 
  12.     <application android:label="@string/app_name" android:hasCode="false"
  13.  
  14.         <!-- Our activity is the built-in NativeActivity framework class
  15.              This will take care of integrating with our NDK code. --> 
  16.         <activity android:name="android.app.NativeActivity" 
  17.                 android:label="@string/app_name" 
  18.                 android:configChanges="orientation|keyboardHidden"
  19.             <!-- Tell NativeActivity the name of or .so --> 
  20.             <meta-data android:name="android.app.lib_name" 
  21.                     android:value="test" /> 
  22.             <intent-filter> 
  23.                 <action android:name="android.intent.action.MAIN" /> 
  24.                 <category android:name="android.intent.category.LAUNCHER" /> 
  25.             </intent-filter> 
  26.         </activity> 
  27.     </application> 
  28.  
  29. </manifest>  
  30. <!-- END_INCLUDE(manifest) --> 

基本上和一般的AndroidManifest文件也差不多,需要注意的是:

(1) Activity的名称必须为NativeActivity

一般的程序Activity就是自己新建的Activity的名称,这里必须为NativeActivity

[cpp] view plaincopyprint?
  1. <activity android:name="android.app.NativeActivity" 
  2.                 android:label="@string/app_name" 
  3.                 android:configChanges="orientation|keyboardHidden"


(2) 增加对JNI中库的引用

[cpp] view plaincopyprint?
  1. <!-- Tell NativeActivity the name of or .so --> 
  2.            <meta-data android:name="android.app.lib_name" 
  3.                    android:value="test" /> 

一般的程序不需要这一部分,但是对于纯C++开发,需要指定其meta data,这里的test就是上面的Android.mk中的module的名称,要保持一致。


总结:上面的程序是一个”最简单“的纯C++的Android应用的例子,编译之后就可以运行了,如果上面的某一个地方错误,就会导致无法编译或运行。其中重点要理解的是android_main和android_dummy函数,另外,上面的程序尽管可以编译运行,但是并不具有实际意义,因为它不能响应任何事件,会导致无法响应。下面讨论更一般的情况,了解一般的程序的组成。


重点理解:

android_main和android_dummy的作用

纯C++程序的基本组成

 

0 0
原创粉丝点击