Your First Android Project
There are two major steps for getting started with Android: 1. You need to install the Android SDK and developer tools
2. You should build a test project to confirm that those tools are properly installed and configured
If you have already done some form of "hello, world" project with the development tools on your development machine, you can skip this tutorial.
If you have not yet installed the Android SDK and related tools, there is an appendix that covers this process. Once you have the Android SDK, it is time to make your first Android project. The good news is that this requires zero lines of code – Android's tools create a "Hello, world!" application for you as part of creating a new project. All you need to do is build it, install it, and see it come up on your emulator or device. That is what this tutorial is for.
Your First Android Project
there are separate instructions for Eclipse users and everyone else – please follow the instructions that pertain to you.
Step #1: Eclipse
From the Eclipse main menu, choose File | New | Project..., and this will bring up a list of project types to choose from. Fold open the Android option and click on Android Project:
Figure 1. Eclipse New Project Wizard
Your First Android Project
Your First Android Project
Your First Android Project
Your First Android Project
• The name of the Java package in which this project goes (e.g.,
com.commonsware.android.skeleton) in the "Package name" field
• The name of the initial activity to create (e.g., Now) in the "Create
Activity" field
• Set the Minimum SDK to be 8
At this point, clicking Finish will create your Eclipse project.
Step #2: Command Line
Here is a sample command that creates an Android project from the command line:
android create project --target "Google Inc.:Google APIs:8" --path Skeleton/Now --activity Now --package com.commonsware.android.skeleton
This will create an application skeleton for you, complete with everything you need to build your first Android application: Java source code, build instructions, etc. However, you are probably going to need to customize this somewhat. Here are what those command-line switches mean:
• --target indicates what version of Android you are "targeting" in
terms of your build process. You need to supply the ID of a target that is installed on your development machine, one you downloaded via the SDK and AVD Manager. You can find out what targets are available via the android list targets command. Typically, your build process will target the newest version of Android that you have available.
• --path indicates where you want the project files to be generated.
Android will create a directory if the one you name does not exist. For example, in the command shown above, a Skeleton/Now/ directory will be created (or used if it exists) underneath the current
Your First Android Project
• --package indicates the Java package in which your first activity will
be located. This package also uniquely identifies your project on any device on which you install it, and this package also needs to be unique on the Android Market if you plan on distributing your application there. Hence, typically, you construct your package based on a domain name you own (e.g., com.commonsware.android.skeleton), to reduce the odds of an accidental package name collision with somebody else.
For your development machine, you will need to pick a suitable target, and you may wish to change the path. The activity and package you can leave alone for now.
Step #2: Build, Install, and Run the Applica-
tion in Your Emulator or Device
Having a project is nice and all, but it would be even better if we could build and run it, whether on the Android emulator or your Android device. Once again, the process differs somewhat depending on whether you are using Eclipse or not.
Step #1: Eclipse
With your project selected in the Package Explorer pane, click the green "play" button in the Eclipse toolbar to run your project. The first time you do this, you will have to go through a few steps to set up a "run configuration", so Eclipse knows what you want to do.
Your First Android Project
Figure 5. Eclipse "Run As" List
If you have more than one emulator AVD or device available, you will then get an option to choose which you wish to run the application on. Otherwise, if you do not have a device plugged in, the emulator will start up with the AVD you created earlier. Then, Eclipse will install the application on your device or emulator and start it up.
Step #2: Outside of Eclipse
For developers not using Eclipse, launch an emulator (or have a device plugged in and set up for debugging), then, in your terminal, change into the Skeleton/Now directory, then run the following command:
ant clean debug install
The Ant-based build should emit a list of steps involved in the installation process, which look like this:
Your First Android Project
[setup] API level: 7 [setup]
[setup] ---
[setup] Resolving library dependencies: [setup] No library dependencies. [setup]
[setup] --- [setup]
[setup] WARNING: No minSdkVersion value set. Application will install on all Android versions.
[setup]
[setup] Importing rules file: tools/ant/main_rules.xml clean:
[delete] Deleting directory /home/some-balding-guy/projects/Skeleton/Now/bin -debug-obfuscation-check:
-set-debug-mode:
-compile-tested-if-test: -dirs:
[echo] Creating output directories if needed...
[mkdir] Created dir: /home/some-balding-guy/projects/Skeleton/Now/bin [mkdir] Created dir: /home/some-balding-guy/projects/Skeleton/Now/gen [mkdir] Created dir: /home/some-balding-
guy/projects/Skeleton/Now/bin/classes -pre-build:
-resource-src:
[echo] Generating R.java / Manifest.java from the resources... -aidl:
[echo] Compiling aidl files into Java classes... -pre-compile:
compile:
[javac] /opt/android-sdk-linux/tools/ant/main_rules.xml:361: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
[javac] Compiling 2 source files to /home/some-balding- guy/projects/Skeleton/Now/bin/classes
Your First Android Project
-package-resources:
[echo] Packaging resources
[aapt] Creating full resource package... -package-debug-sign:
[apkbuilder] Creating Now-debug-unaligned.apk and signing it with a debug key... debug:
[echo] Running zip align on final apk...
[echo] Debug Package: /home/some-balding-guy/projects/Skeleton/Now/bin/Now- debug.apk
BUILD SUCCESSFUL Total time: 4 seconds
Note the BUILD SUCCESSFUL at the bottom – that is how you know the application compiled successfully.
When you have a clean build, in your emulator or device, open up the application launcher, typically found at the bottom of the home screen:
Your First Android Project
launcher, press the "BACK button", located to the right of the [MENU] button, and looks like an arrow pointing to the left.