Creating an application template manually
First of all, we are going to create a basic template for our applications. Every Android application that is to be built via Android SDK, should contain a predefined directory structure and the configuration .xml
files. This can be done using Android SDK tools and IDEs. In this recipe, we will learn how to do it manually. We will use these files later on as the very starting point for all our examples.
Getting ready
Let us set up the directory structure of our project (see the following screenshot):
This is a typical structure for any Android project. We will create all the required files manually rather than using Android tools.
How to do it...
Place the Java Activity
code into the App1\src\com\packtpub\ndkcookbook\app1\App1Activity.java file
, which should look as follows:
package com.packtpub.ndkcookbook.app1; import android.app.Activity; public class App1Activity extends Activity { };
The localizable application name should go to App1\res\values\strings.xml
. The string parameter app_name
is used in the AndroidManifest.xml
file to specify the user-readable name of our application, as seen in the following code:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">App1</string> </resources>
Now we need to write more scripts for Apache Ant and the Android SDK build system. They are necessary to build the .apk
package of your application.
- The following is the
App1/project.properties
file:target=android-15 sdk.dir=d:/android-sdk-windows
- We need two more files for Ant. The following is
App1/AndroidManifest.xml
:<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.packtpub.ndkcookbook.app1" android:versionCode="1" android:versionName="1.0.0"> <supports-screens android:smallScreens="false" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" /> <uses-sdk android:minSdkVersion="8" /> <uses-sdk android:targetSdkVersion="18" />
Our examples require at least OpenGL ES 2. Let Android know about it:
<uses-feature android:glEsVersion="0x00020000"/> <application android:label="@string/app_name" android:icon="@drawable/icon" android:installLocation="preferExternal" android:largeHeap="true" android:debuggable="false"> <activity android:name="com.packtpub.ndkcookbook.app1.App1Activity" android:launchMode="singleTask"
Create a full-screen application in a landscape screen orientation:
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:screenOrientation="landscape" 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>
The second file is
App1/build.xml
:<?xml version="1.0" encoding="UTF-8"?> <project name="App1" default="help"> <property file="ant.properties" /> <loadproperties srcFile="project.properties" /> <import file="${sdk.dir}/tools/ant/build.xml" /> </project>
How it works...
With all the listed files in place, we can now build the project and install it on an Android device by carrying out the following steps:
- From the
App1
folder run:>ant debug
- The tail of the output from the previous command should look like:
BUILD SUCCESSFUL Total time: 12 seconds
- And the built debug
.apk
package is inbin/App1-debug.apk
. - To install the app, run:
>adb install App1-debug.apk
- You should see the output from
adb
, similar to the following commands:* daemon not running. starting it now on port 5037 * * daemon started successfully * 1256 KB/s (8795 bytes in 0.006s) pkg: /data/local/tmp/App1-debug.apk Success
The application can now be started from your Android launcher (named App1
). You will see just a black screen. You can exit the application using the BACK button.
There's more...
Don't forget to put the application icon into App1\res\drawable\icon.png
. Refer to the book's code bundle if you want to build the app quickly, or put your own icon there. 72 x 72 32-bit will do just fine. You can find the official Android icons guidelines at http://developer.android.com/design/style/iconography.html.
The official documentation on the AndroidManifest.xml
file can be found at http://developer.android.com/guide/topics/manifest/manifest-intro.html.
Furthermore, you can update your applications without uninstalling the previous version using the adb -r
command-line switch in the following way:
>adb install -r App1-debug.apk
Otherwise, before installing a new version of your application you will have to uninstall the existing one using the following command:
>adb uninstall <package-name>
See also…
- Signing release Android applications