Welcome
to Android application development!
This
class teaches you how to build your first Android app. You’ll learn how to
create an Android project and run a debuggable version of the app. You'll also
learn some fundamentals of Android app design, including how to build a simple
user interface and handle user input.
Before
you start this class, be sure you have your development environment set up. You
need to:
1. Download the Android SDK.
2. Install the ADT plugin
for Eclipse (if you’ll use the Eclipse IDE).
3. Download the latest SDK
tools and platforms using the SDK Manager.
Create
a Project with Command Line Tools
If
you're not using the Eclipse IDE with the ADT plugin, you can instead create
your project using the SDK tools from a command line:
1. Change directories into
the Android SDK’s tools/ path.
2. Execute:
android list targets
This prints a list of the
available Android platforms that you’ve downloaded for your SDK. Find the
platform against which you want to compile your app. Make a note of the target
id. We recommend that you select the highest version possible. You can still
build your app to support older versions, but setting the build target to the
latest version allows you to optimize your app for the latest devices.
If you don't see any
targets listed, you need to install some using the Android SDK Manager tool.
See Adding Platforms
and Packages.
3. Execute:
4. android create project --target
<target-id> --name MyFirstApp \
5. --path <path-to-workspace>/MyFirstApp
--activity MainActivity \
6. --package com.example.myfirstapp
Replace <target-id> with an id from the
list of targets (from the previous step) and replace <path-to-workspace> with the location
in which you want to save your Android projects.
Create
a Project with Command Line Tools
If
you're not using the Eclipse IDE with the ADT plugin, you can instead create
your project using the SDK tools from a command line:
1. Change directories into
the Android SDK’s tools/ path.
2. Execute:
android list targets
This prints a list of the
available Android platforms that you’ve downloaded for your SDK. Find the
platform against which you want to compile your app. Make a note of the target
id. We recommend that you select the highest version possible. You can still build
your app to support older versions, but setting the build target to the latest
version allows you to optimize your app for the latest devices.
If you don't see any
targets listed, you need to install some using the Android SDK Manager tool.
See Adding Platforms
and Packages.
3. Execute:
4. android create project --target
<target-id> --name MyFirstApp \
5. --path <path-to-workspace>/MyFirstApp
--activity MainActivity \
6. --package com.example.myfirstapp
Replace <target-id> with an id from the
list of targets (from the previous step) and replace <path-to-workspace> with the location
in which you want to save your Android projects.
Running
Your App
How you run your app depends on
two things: whether you have a real Android-powered device and whether you're
using Eclipse. This lesson shows you how to install and run your app on a real
device and on the Android emulator, and in both cases with either Eclipse or
the command line tools.
Before you run your app, you
should be aware of a few directories and files in the Android project:
AndroidManifest.xml
The manifest file describes the fundamental
characteristics of the app and defines each of its components. You'll learn
about various declarations in this file as you read more training classes.
One of the most important
elements your manifest should include is the
<uses-sdk>
element. This declares your app's
compatibility with different Android versions using the android:minSdkVersion
andandroid:targetSdkVersion
attributes. For your first app, it
should look like this:<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> ... </manifest>
You should always set the
android:targetSdkVersion
as high as possible and test your app
on the corresponding platform version. For more information, read Supporting Different Platform Versions.src/
Directory
for your app's main source files. By default, it includes an
Activity
class that runs when your app is launched
using the app icon.res/
Contains
several sub-directories for app resources. Here are just a few:
drawable-hdpi/
Directory
for drawable objects (such as bitmaps) that are designed for high-density
(hdpi) screens. Other drawable directories contain assets designed for other
screen densities.
layout/
Directory
for files that define your app's user interface.
values/
Directory
for other various XML files that contain a collection of resources, such as
string and color definitions.
When you build and run the
default Android app, the default
Activity
class starts and loads a layout file that
says "Hello World." The result is nothing exciting, but it's
important that you understand how to run your app before you start developing.
No comments:
Post a Comment