Posted by Matt Cashatt on 5/5/2011 (Last updated on 5/5/2011 3:14 PM)
In this tutorial we are going to create your first Android app. Granted, this is only a "Hello, World", so all your app will do is display a string of text to the user but along the way you will have learned about the Anatomy of an Android project, the Android Virtual Device Manager (AVD), debbugging in Eclipse, app deployment to virtual device emulators, and application packaging for release deployment.
If you haven't already set up your Android development environment, then you should hop over to my tutorial for doing so (Setting Up Your Android Development Environment) first.
1- Set up your project
Let's dive right in by creating a new project in Eclipse. We will discuss what is happening along the way.
1. Launch Eclipse.
2. On the top tool bar, choose "File" >>"New">>"Project. . ."
3. If you have set up the Android toolset correctly, you should see "Android Project" listed as an option.
Select "Android Project" and then click "Next".
4. On the next dialog you see, you will set up all of the basic information for the project:
HERE'S WHAT IS HAPPENING:
Project Name- This is the name of the project on your computer. This name will only be seen by you.
Create Project In Workspace- In my tutorial entitled "Setting Up Your Android Development Environment", we set up a default workspace to store all of your Android project files. Just use this same default location for this project.
Build Target- This part is the subject of quite a bit of discussion and will ultimately merit a little thought and planning on your part. Basically, the "Build Target is the preferred version of Android on which this application should run. If you build an app in the future that requires functionality only found in a recent Android build (say, platform 3.0/API 11), then you will target this build and you will also set your Min SDK Version (at the bottom) to the same; however, if you have a feature in your application that makes use of a cool new function of one of the newer API's BUT that feature is not critical to the app, then you set the higher/more recent API as your build target and then set the Min SDK Version to the lowest API that can run your app (even if it doesn't have all the cool features that the app running on a later API would have). Hope this makes sense. For this tutorial, just set the Build Target to version 2.1/API 7 and the Min SDK Version to the same (7).
Application Name- This is the name of your application as seen by the rest of the world once it is on the Google App Market.
Package Name- This is the namespace from which your project will be derived. Everything in this tutorial project will inherit from "com.androidbook" so let's make the namespace "com.androidbook.HelloAndroid".
Create Activity- An "activity" in Android is something you do and is usually associated with a specific screen. This is basically like setting your start page or form in a .Net application. Let's create an activity called "HelloAndroidActivity".
Min SDK Version- We already discussed this in the "Build Target" section above. Set it to "7".
5. Click "Next">>"Finish". We will not be creating a test project at this time.
2- Anatomy of an Android project
Now that you've created a project. Let's take a look at what you've got. Every Android project starts off with the same parts and pieces:
/src folder- This is the folder that will contain all of your source code. The Activity you created when you opened this project lives here.
/gen folder- This folder contains files generated by Java and should not be modified by you.
Android *.*- This folder contains all of the goodies (classes) included in the build target API you selected in the first step. I used the wildcard characters here simply to demonstrate that it could be any version. As pictured above, it is "Android 2.1-update1".
/assets- The folder contains uncompiled file resources like graphical assets (images), photos, sounds, etc.
/res- Compiled resources live here: animations, drawable graphics, layout files, data strings & numbers, etc.
AndroidManifest.xml- This is the main configuration file for the application. If you started off as a .Net developer like I did, this file is analogous to the web.config file.
default.properties- This file is generated automatically by the Android ADT plugin and is used by the Eclipse IDE and by ADT. This file is not something that you will typically (if ever) edit.
proguard.cfg- The ProGuard file shrinks, optimizes and obfuscates your application. It basically just optimizes it for faster download from the app market and makes it harder to decompile and reverse engineer.
3- Configure your project
We don't actually need to configure anything in this project, but it is still good to get to know how you would do it. The Android Manifest file editor should be open by default at the launch of your project:
If you don't see the HelloAndroid Manifest tab opened as in the screen shot above, then just double-click on the AndroidManifest.xml file in the left rail.
The Android Manifest file let's you define many characteristics of your project such as SDK version, application name, default activity, etc. Notice the tabs at the bottom of the Manifest resource editor?
Here is what they all do:
Manifest- Used to manage general, global settings like the SDK version and application name.
Application- This tab defines details about the application such as its icon, the default activity, and all activities that the app can run.
Permissions- I am sure that at some point you have downloaded an app that asked for you to grant certain permissions such as to sense your location or to read your contacts. This is where you set the permissions that the app will need to request of the user. Mor on that in a future tutorial.
Instrumentation- This will be used for unit testing by leveraging all of the instrumentation classes. Again, more in a future tutorial.
AndroidManifest.xml- This tab is a simple text editor that let's you look at and edit the raw manifest xml.
4- Make a change!
Now that every thing is set up, you are ready to start making some changes. Before doing that, take a minute to drill down into and explore everything in the/res folder. Notice how all of the non-graphic files are xml files? That is because xml is the standard filetype for Android projects just like it would be "html" for a web page or "aspx" for a .Net web app or "php" for a PHP web app. It is within these XML files that we will be making a small change that will constitute our app.
1. Go to /res >> layout >> main.xml. This file dictates the main layout for your app. Double-click on this file and you should see something like this:
2. Notice the "Hello World, HelloAndroidActivity!" string? The Android ADK put that there for us as it will do to every new project you create. Check out the raw xml for this by clicking the "main.xml" tab at the bottom of the editor (it is right next to the "Graphical Layout" tab that is currently selected). In this raw xml, you should see a textview node with this attribute: android:text="@string/hello". This attribute obviously sets the text value of textview control to a variable called "hello" which is found in the strings resource file (@string).
3. Now go to /res >> values >> strings.xml. Using either the raw xml view or the editor view, change the value of the "hello" variable to anything you like (I'll be changing it to "Hello Matt!").
4. After you have changed the value, save the project (ctrl+s) and go back to the main.xml Graphical Layout view. You should see that the string has now changed.
5- Run and Debug the App
Ok, so we have made a change and now need to run the app in an emulator to debug and test.
1. The first thing that needs to be done here is to configure the Android Virtual Device (AVD) for the emulator. The AVD allows you to emulate the various types of devices that run Android as well as their respective configurations like screen size and orientation.
a. Go to Window (or "Eclipse" on Mac) >> Android SDK and AVD Manager.
b. In the dialog that pops up, click on "Virtual devices" in the left rail and a list of installed virtual devices will be displayed on the right. You will probably not see anything since you have not yet installed any virtual devices.
c. Click "New" to install a new AVD.
d. Choose a name for this AVD like "myAVD".
e. Set the target to version 2.1/API 7.
f. The SD Card value is the amount of space that your app would require on an actual device's SD card. The value you set here is actually going to take up space on your hard drive so let's just set this to 1024 mibibytes which is more than adequate for what we are doing here.
g. Set the skin to "Default (WVGA800)" and then click "Create AVD".
2. Create a debug and run configuration:
a. Open your AndroidManifest.xml file in the resource editor.
b. Click on the Application tab at the bottom.
c. Find the "Debuggable" setting and set it to "true".
d. Save (ctrl+s).
e. In the top toolbar, click Run >> Debug Configurations. . .
f. Double-click the "Android Application" item in order to create a new profile entry:
g. This new entry will be called "New_configuration". Choose it, and then change the name to "HelloAndroidDebug".
h. Beneath the textbox where you just changed the name to "HelloAndroidDebug", you should see three tabs: "Android", "Target", and "Common". Click on Android and use the browse button to set the project value to "HelloAndroid" or whatever you called this project.
i. Now click the Target tab and check the box next to the AVD we created earlier ("myAVD").
j. Click "apply" to apply your changes.
3. Now find the little green Debug icon in the top toolbar. It looks just like a green ladybug. Click on this, then go to "Debug COnfigurations. . .". Find and double-click the debug profile we just created called "HelloAndroidDebug". The AVD emulator we created earlier will pop up and load your app. THIS CAN TAKE A LOOOOONG TIME SO BE PATIENT. You will probably see just the virtual device at first with the word "ANDROID" on the screen and a blinking cursor for several minutes. Just leave it alone and let load. It takes forever the first time. Once it has loaded into the AVD, the app should look something like this:
6- Let's do this!
CONGRATULATIONS!! You just created your first Android app! I hope this tutorial was helpful to you. As always, if you have any questions, comments, or suggestions, either leave a comment below or contact me. Also, if you develop a killer app, let me know!