Mastering Android NDK
上QQ阅读APP看书,第一时间看更新

Creating an Ant-based application template manually

Let's start with the lowest level and create a template for our applications buildable with Apache Ant. Every Android application which is to be built using Apache Ant should contain a predefined directories structure and configuration .xml files. This is usually done using Android SDK tools and IDEs. We will explain how to do it by hand to let you know the machinery behind the curtains.

Tip

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

For this book, the source code files can be downloaded or forked from the following GitHub repository as well: https://github.com/corporateshark/Mastering-Android-NDK

The directory structure of our minimalistic project looks like the following screenshot (see the source code bundle for the complete source code):

Creating an Ant-based application template manually

We need to create the following files within this directory structure:

  • res/drawable/icon.png
  • res/values/strings.xml
  • src/com/packtpub/ndkmastering/App1Activity.java
  • AndroidManifest.xml
  • build.xml
  • project.properties

The icon icon.png should be there, and currently contains a dummy image of an Android application:

Creating an Ant-based application template manually

The file strings.xml is required to make use of the Android localization system. In the manifest AndroidManifest.xml, we use the string parameter app_name instead of the actual application name. The file strings.xml resolves this parameter into a human readable string:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">AntApp1</string>
</resources>

The Java source code of the minimal buildable application is in the App1Activity.java file:

package com.packtpub.ndkmastering;
import android.app.Activity;
public class App1Activity extends Activity
{
};

The rest three files, AndroidManifest.xml, build.xml, and project.properties, contain the description of the project necessary for Ant to build it.

The manifest AndroidManifest.xml is as follows:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.packtpub.ndkmastering"
android:versionCode="1"
android:versionName="1.0.0">

Our application will require Android 4.4 (API Level 19) and is tested with Android 6.0 (API Level 23):

<uses-sdk android:minSdkVersion="19" android:targetSdkVersion="23" />

Most of the examples in this book will require OpenGL ES 3. Let's mention it here:

<uses-feature android:glEsVersion="0x00030000"/>
<application android:label="@string/app_name"
android:icon="@drawable/icon"
android:installLocation="preferExternal"
android:largeHeap="true"
android:allowBackup="true">

Here is the name of the main activity:

<activity android:name="com.packtpub.ndkmastering.App1Activity"
android:launchMode="singleTask"

We want a fullscreen application in the landscape orientation:

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="landscape"

Our application can be started from the system launcher. The displayable name of the application is stored in the app_name parameter:

android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
<intent-filter>
  <action android:name="android.intent.action.MAIN" />
  <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Note

You can read the official Google documentation on the application manifest at http://developer.android.com/guide/topics/manifest/manifest-intro.html.

The file build.xml is much simpler and will resemble mostly what Android tools would generate:

<?xml version="1.0" encoding="UTF-8"?>
<project name="App1" default="help">
  <loadproperties srcFile="project.properties" />
  <fail message="sdk.dir is missing. Make sure to generate local.properties using 'android update project' or to inject it through an env var"
    unless="sdk.dir"/>
  <import file="${sdk.dir}/tools/ant/build.xml" />
</project>

There is a difference to Android SDK Tools, since we don't use ant.properties here. This was done just for the sake of simplicity and just has an educational purpose.

The same situation exists with the file project.properties, which contains platform-specific declarations:

target=android-19
sdk.dir=d:/android-sdk-windows

Now, our first application (which does not even contain any native code yet) is ready to be built. Use the following one-liner to build it:

$ ant debug

If everything was done correctly, you should see the tail of the output similar to the following:

Creating an Ant-based application template manually

To install an .apk file from the command line, run adb install -r bin/App1-debug.apk to install the freshlybuilt .apk on your device. Start the application from your launcher (AntApp1) and enjoy the black screen. You can use the BACK key to exit the application.