Android NDK纯C++开发(2)

54 篇文章 0 订阅
46 篇文章 0 订阅

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

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

1. 源码main.cpp:

  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(); 
// 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中的说明:

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


2. Android.mk

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

  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 to false. --> 
  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) --> 
<?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

  1. <activity android:name="android.app.NativeActivity" 
  2.                 android:label="@string/app_name" 
  3.                 android:configChanges="orientation|keyboardHidden"
<activity android:name="android.app.NativeActivity"
                android:label="@string/app_name"
                android:configChanges="orientation|keyboardHidden">


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

  1. <!-- Tell NativeActivity the name of or .so --> 
  2.            <meta-data android:name="android.app.lib_name" 
  3.                    android:value="test" /> 
 <!-- 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++程序的基本组成

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值