O'Reilly logo

Programming Android by Zigurd Mednieks, G. Blake Meike, Masumi Nakamura, Laird Dornin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Native Activities

Android 2.3 (API level 9) and Android NDK revision 5 let you write entire activities and applications as native source by using the NativeActivity class to access the Android application life cycle.

To utilize this method, the android.app.NativeActivity needs to be referenced in the Android manifest file. Note that the application reference has a hasCode attribute. This attribute should be set to false if there is no Java in the application (only the NativeActivity). In this case, however, since we do have Java code, we set the value to true:

   <!-- This .apk has Java code, so set hasCode to true which is the default. -->
   <!-- if this only had a native app (only the activity 
        called 'android.app.NativeActivity') -->
   <!-- then set to false -->

    <application android:icon="@drawable/icon" android:label="@string/app_name" 
        android:hasCode="true" >

        <activity android:name=".NDKApp" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>    

        <activity android:name="android.app.NativeActivity" 
                                android:label="SampleNativeActivity" 
                                android:debuggable="true" >

                <!-- here we declare what lib to reference -->
            <meta-data android:name="android.app.lib_name" 
                                    android:value="sample_native_activity" />
        </activity>

    </application>

In this example, we use the android_native_app_glue.h header file instead of native_activity.h. The native_activity.h interface is ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required