Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Android NDK Game Development Cookbook

You're reading from   Android NDK Game Development Cookbook For C++ developers, this is the book that can swiftly propel you into the potentially profitable world of Android games. The 70+ step-by-step recipes using Android NDK will give you the wide-ranging knowledge you need.

Arrow left icon
Product type Paperback
Published in Nov 2013
Publisher Packt
ISBN-13 9781782167785
Length 320 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Sergey Kosarevsky Sergey Kosarevsky
Author Profile Icon Sergey Kosarevsky
Sergey Kosarevsky
Viktor Latypov Viktor Latypov
Author Profile Icon Viktor Latypov
Viktor Latypov
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Establishing a Build Environment 2. Porting Common Libraries FREE CHAPTER 3. Networking 4. Organizing a Virtual Filesystem 5. Cross-platform Audio Streaming 6. Unifying OpenGL ES 3 and OpenGL 3 7. Cross-platform UI and Input Systems 8. Writing a Match-3 Game 9. Writing a Picture Puzzle Game Index

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

Getting ready

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.

  1. The following is the App1/project.properties file:
    target=android-15
    sdk.dir=d:/android-sdk-windows
  2. 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:

  1. From the App1 folder run:
    >ant debug
    
  2. The tail of the output from the previous command should look like:
    BUILD SUCCESSFUL
    Total time: 12 seconds
    
  3. And the built debug .apk package is in bin/App1-debug.apk.
  4. To install the app, run:
    >adb install App1-debug.apk
    

    Note

    Don't forget to connect your device through a USB and turn USB Debugging on in Android settings before running this command.

  5. 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
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image