Continue to understand how to use native_app_glue to compile Android ndk development for pure C ++.
The following describes the basic components of the native_app_glue program from a "simplest" program.
1. Source Code 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();}
First, you must include the header file android_native_app_glue.h. Then, you must implement an android_main () function. Otherwise, the function cannot be run (compilation may be normal ). In addition, app_dummy () must be called at least in android_main to ensure that glue is not optimized. For details, refer to the instructions in 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)
The difference between Android. mk and general ndk programs is as follows:
(1) Add local_ldlibs =-landroid
(2) Add local_static_libraries = android_native_app_glue
(3) add $ (call import-module, Android/native_app_glue)
This is the difference from the makefile of General ndk programs. In addition, the local_module = test here will be used below.
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) -->
It is basically the same as the androidmanifest file. Note that:
(1) The activity name must be nativeactivity
Generally, a program activity is the name of the newly created activity, which must be nativeactivity.
<activity android:name="android.app.NativeActivity" android:label="@string/app_name" android:configChanges="orientation|keyboardHidden">
(2) Add references to the JNI Library
<!-- Tell NativeActivity the name of or .so --> <meta-data android:name="android.app.lib_name" android:value="test" />
The general program does not need this part, but for pure C ++ development, you need to specify its meta data. The test here is the name of the module in Android. mk above, which must be consistent.
Summary: The above program is the "simplest" example of a pure C ++ Android app. It can be run after compilation. If the above Code is incorrect, this will cause compilation or running failure. Specifically, we need to understand the android_main and android_dummy functions. In addition, although the above program can be compiled and run, it does not have practical significance, because it cannot respond to any event, leading to failure to respond. Next we will discuss more general situations and understand the composition of General programs.
Important:
Functions of android_main and android_dummy
Basic Components of pure C ++ programs