Kilobolt
  • Home
  • Tutorials
    • Game Development Tutorial >
      • Unit 1: Beginning Java >
        • Before you begin...
        • Day 1: Setting Up
        • Day 2: Java Basics
        • Day 3: More Basics
        • Day 4: Java Math
        • Day 5: More Math
        • Day 6: If... else...
        • Day 7: More Control Flow
        • Day 8: Looping
        • Day 9: More on Looping
        • Day 10: Inheritance, Interface
        • Day 11: Threads and Graphics
      • Unit 2: Creating a Game I >
        • Day 1: Foundations
        • Day 2: Basic Framework
        • Day 3: Taking User Input
        • Day 4: Enter the Robot
        • Day 5: Background and Sprites
        • Day 6: Adding Enemies
        • Day 7: Shooting Bullets
        • Day 8: Animations
        • Day 9: 2D-Arrays
        • Day 10: Painting the Tilemap
      • Unit 3: Creating a Game II >
        • Day 1: Level Creation - Part 1
        • Day 2: Level Creation - Part 2
        • Day 3: Level Creation - Part 3
        • Collision Detection Basics
        • Day 4: Collision Detection Part 1
        • Day 5: Collision Detection Part 2
        • Day 6: Collision Detection Part 3
        • Day 7: Health System & Death
        • Day 8: Basic AI & Final Touches
      • Unit 4: Android Game Development >
        • Day 1: Introduction to Android
        • Day 2: Setting up for Development
        • Day 3: Creating our First Android Application
        • Day 4: Parts of an Android Application
        • Day 5: The Android Game Framework: Part I
        • Day 6: The Android Game Framework: Part II
        • Create an Android Game From Scratch (or port your existing game)
        • Day 7: Creating an Android Game (From Start to Finish)
      • Reference Sheet
    • Zombie Bird Tutorial (Flappy Bird Remake) >
      • Unit 1: Building the Game >
        • Introduction
        • Day 1: Flappy Bird - An In-depth Analysis
        • Day 2: Setting up libGDX
        • Day 3: Understanding the libGDX Framework
        • Day 4: GameWorld and GameRenderer and the Orthographic Camera
        • Day 5: The Flight of the Dead - Adding the Bird
        • Day 6: Adding Graphics - Welcome to the Necropolis
        • Day 7: The Grass, the Bird and the Skull Pipe
        • Day 8: Collision Detection and Sound Effects
        • Day 9: Finishing Gameplay and Basic UI
        • Day 10: GameStates and High Score
        • Day 11: Supporting iOS/Android + SplashScreen, Menus and Tweening
        • Day 12: Completed UI & Source Code
    • Android Application Development Tutorial >
      • Unit 1: Writing Basic Android Apps >
        • Before you begin...
        • Day 1: Android 101
        • Day 2: Getting to Know the Android Project
        • Day 3: The Development Machine
        • Day 4: Building a Music App - Part 1: Building Blocks
        • Day 5: Building a Music App - Part 2: Intents
        • Day 6: Building a Music App - Part 3: Activity Lifecycles
  • Forum
  • About Us
    • Contact Us
  • Our Games
    • TUMBL: FallDown
  • Facebook
  • Twitter

      GAME DEVELOPMENT TUTORIAL: DAY 4-3: Creating our First Android application

Welcome to Day 3 of Unit 4: Android Game Development. 

Many of you have never created an Android application before. In today's lesson, we will be creating your very first app, and setting up a virtual Galaxy Nexus.

Examining the New Perspective

If you are picking up from Day 2, feel free to close out of that welcome screen. You won't be needing it. Trust me. ;)

You will see the familiar Eclipse interface with a few changes. Let me discuss the important changes with you.
In the ribbon of Eclipse, you will see these two new buttons:
Picture
The one on the left opens the Android SDK Manager, through which you can download various files like App samples, SDK platforms (like Jelly Bean), additional libraries, and so on.

The button on the right opens the Android Virtual Device Manager which will allow you to create emulators* through which you can test your apps. 

*A note about emulators: These emulators can be very slow and frustrating, but they are extremely accurate. They emulate an Android device to its very basic processes, and can allow you to test your app on various screen sizes and memories; however, it will not replace a real device. If you have a real device, I recommend that you use it for a sizable chunk of your debugging/developing, and to use the emulator to test the application on multiple screen sizes later on.

If you are a Samsung Developer (which you can sign up to be for free), Samsung offers a Remote Lab Service, through which you can connect to real-life devices and upload your own apps for testing. I will be talking about this more in the future, as this really only applies if you have completed your game and you want to make sure that your game works on a specific low-end device.
Along with the two buttons that we discussed above, you will also notice this change:
Picture
The DDMS Button
This DDMS button opens an alternate Eclipse perspective that will allow you to take screenshots, check for memory leaks and heap size (using LogCat), and so on. We will employ it several times throughout the Unit. Don't be intimidated by the terminology - you will be using them in your own words very soon.

The SDK Platforms

To create your first Android application, you must first have an SDK platform. 

Open up the Android SDK Manager:
Picture
Oh look! The Bundle includes the latest SDK Platform. So we won't be needing to download that; however, if you want to create emulators to test your application on lower versions of Android, you will need to download that here.

Device Targeting

Two of the problems with Android are device & software fragmentation. Not only do we have thousands of devices with different specs and screen sizes, we face the problem of staggered updates.

The chart below shows the distributions in percents of the various versions of Android.

Trivia: Many know that Android's APIs (versions) are named after dessert. But did you know that they were named in alphabetic order?
Picture
There's a trade-off here. The more older devices you target, the less newer features you will be able to use.
 
Of course, you could include a bunch of if statements checking for the phone's software version before calling certain methods, but that takes a lot of time and knowledge of each API's limiations, not to mention the fact that you will be offering different experiences to people with different versions of Android. This usually is not a good thing for games.

Also, generally speaking, devices with lower versions of Android tend to have weaker hardware. If you spend time trying to optimize your game to run smoothly on these devices by removing "excess" features, your game will suffer.

So generally, the recommended range of targeting is 90%. You can achieve this by setting the minimum version as Gingerbread, which included several updates to how Android manages heap(memory). This handles the problem of hardware weaknesses and lack of functionality.

You will ultimately have to make the decision when you decide to make your own game. For this tutorial, we will be targettng Gingerbread and above.

Creating your First App

Now that we discussed the logistics, let's create our first application.

1. Select File >> New >> Android Application Project.
2. Enter the following value:
Name: Our First App

This will automatically fill the two boxes below. 

Picture
Leave the other settings as they are. The Target SDK should always be the latest version of Android. This lets you use the latest features available to Android in your applications.

3. Press Next. 
4. Press Next Again. 
Picture
Feel free to change some of these settings around. Then press Next.


5. On the Create Activity screen, leave BlankActivity selected, and press Next.
Picture
A lot of tutorials and books like to talk about every little detail of Android before you begin creating apps. What ends up happening is that you read a bunch of stuff that you intellectually understand but cannot conceptually visualize, and then you forget everything to learn it again after you start programming. So I will skip the traditional "before you begin" spiel about Activities, services, and so on, and we will talk about those things as they come up. 

For now, think of an Activity as a page. Something that is visible to you.

Press Finish.

Running the Application

You might have some errors after creating the project. Let it sit for a few moments. The errors should disappear.

At this point, you can press Run to run your project, as an Android Application.
Once it finishes loading, you should get an error saying no compatible devices were found. Press OK.

You should see the Android Device Chooser. The upper part of this labeled "Choose a running Android device" will run all the Android devices connected to the computer with USB Debugging enabled (more on this later).

The bottom part of the Android Device Chooser lists all the Android Virtual Devices on your computer.

Click on Manager... to open up the Android Virtual Device Manager.
Picture
When the Android Virtual Device Manager opens, press New...

Fill out the form as below. This will create a virtual Galaxy Nexus for us:
Picture
You can see that it emulates the type of processor, you can specify RAM size, and VM Heap size* (in MegaBytes). 
Once you have the values that I have on my screen, Press OK. 

(Snapshot lets you restart your emulator from where you signed off. It lets the emulator get started more quickly, so I recommend checking that).

*A quick note about heap: Heap usually refers to the amount of memory available to our application. Despite our device having 1GB of memory, only 64MB is available to our application. On some lower end devices, the heap size is 16MB. This is a problem that we did not have in the previous Units, but from now on, we will have to manage memory to make sure we do not fill up the heap (which activates the Garbage Collector and in worst cases causes an Out of Memory exception). I will discuss the GC and the exception in later lessons.

Now if you open the Android Device Chooser again and click Refresh, you will see the following:
Picture
With our virtual Galaxy Nexus selected, press OK.

NOTE: You might get an error saying:
Failed to allocate memory: 8 This application has requested the Runtime to terminate it in an unusual way.

To Work around This:

Picture
Open up the Android Virtual Device Manager again.Select GalaxyNexus, and press the Edit... to the right.
Now change the RAM to 512, and press OK. 
Exit out of AVD.

This should fix the issue (an annoying bug in the Android SDK). Press Run again.

Now wait for a very long time.

It takes a very long time for the emulator to get started up. Let it sit for anywhere from 1-10 minutes. If you have the Snapshot option enabled, your emulator should start up much faster the next time.

Developers usually keep the emulator minimized so that they do not have to start it up every time they make a change to the application.

When you see this screen...

Picture
You have successfully created your first application, setup a virtual Galaxy Nexus, and ran the program... all without writing a single line of code!

(You can say OK to the little message about LogCat).

Congratulations! You took a small step into the new world of Android.
We will discuss "Our First App" in Day 4.
Feel free to email me questions at jamescho7@kilobolt.com

Picture
No source code is available for this lesson.
Go to Unit 4: Day 2
Go to Unit 4: Day 4
© 2014 Kilobolt, LLC. All rights reserved.