GAME DEVELOPMENT TUTORIAL: DAY 4-3: Creating our First Android application
- 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.
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:
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:
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.
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:
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:
Open up the Android SDK Manager:
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?
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?
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.
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.
1. Select File >> New >> Android Application Project.
2. Enter the following value:
Name: Our First App
This will automatically fill the two boxes below.
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.
3. Press Next.
4. Press Next Again.
Feel free to change some of these settings around. Then press Next.
5. On the Create Activity screen, leave BlankActivity selected, and press Next.
5. On the Create Activity screen, leave BlankActivity selected, and press Next.
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.
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.
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.
When the Android Virtual Device Manager opens, press New...
Fill out the form as below. This will create a virtual Galaxy Nexus for us:
Fill out the form as below. This will create a virtual Galaxy Nexus for us:
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:
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:
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.
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:

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 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.
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...
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.
(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
|
|
No source code is available for this lesson.