Android Development http://developer.android.com/develop/ 吳 俊 興 國 立 高 雄 大 學 資 訊 工 程 學 系
Android 3D 1. Design 2. Develop Training API Guides Reference 3. Distribute 2
Development Training Get Started Building Your First App Managing the Activity Lifecycle Building a Dynamic UI with Fragments Saving Data Interacting with Other Apps Advanced Training 3
Development - Get Started Learn how to set up the development environment Java SDK, Android SDK, Eclipse, ADT Plugin for Eclipse Learn how to build the first Android app How to create an Android project How to run a debuggable version of the app Learn some fundamentals of Android app design 4
Installing the SDK Set up the Android development environment 1. Download the Android SDK Check and install Java SDK if necessary Download the latest SDK tools and platforms using the SDK Manager 2. Install Eclipse 3. Install ADT (Android Developtment Tools) plugin for Eclipse Tip: For easy access to the SDK tools from a command line, add the location of the SDK's tools/ and platform-tools to your PATH environment variable http://developer.android.com/sdk/installing/ 5
Step 1. Download the Android SDK http://developer.android.com/sdk/ Includes only the core SDK tools (mainly Android SDK Manager) to download the rest of the SDK packages (such as the latest system image) Checks for required tools, such as the proper Java SE Development Kit (JDK) and installs it if necessary To develop an Android app, you also need to download at least one Android platform and the latest SDK Platform-tools For the first time, simply click Install to install the recommended packages Anytime you can launch the Android SDK Manager to download others 6
Adding Platforms and Packages SDK Tools (Required) The new SDK installation already has the latest version Make sure to keep this up to date SDK Platform-tools (Required) Must install this package for the first time SDK Platform (Required) Must download at least one platform to compile applications The latest platform version is recommended To get started, download the latest Android version, plus the lowest version planned to support (we recommend Android 2.2 for your lowest version) System Image (Recommended) It's a good practice to download system images for all versions of Android your app supports and test your app running on them with the Android emulator Android Support (Recommended) Includes a static library to use some of the latest Android APIs on devices running a platform version as old as Android 1.6 SDK Samples (Recommended) Include source codes used to learn about Android, load as a project and run, or reuse in your own app 7
Step 2. Install Eclipse 1. Download Eclipse from http://www.eclipse.org/downloads/ Classic Version recommended, otherwise Java or RCP Version Self-contained executable file (zip) 2. Unzip it to your preferred location, i.e., \Program Files\Eclipse 3. Create a shortcut as you like. Start installation as the first time of running Eclipse Select a default workspace (folder) to store projects 8
Step 3. Install the ADT plugin for Eclipse Step 3-1. Download the ADT Plugin 1. Start Eclipse, then select Help->Install New Software 2. Click Add, in the top-right corner In the Add Repository dialog that appears, enter Name: ADT Plugin Location: https://dl-ssl.google.com/android/eclipse/ If having trouble acquiring the plugin, try using "http" instead of "https" 3. In the Available Software dialog, select the checkbox next to Developer Tools and click Next In the next window, you'll see a list of the tools to be downloaded. Click Next Read and accept the license agreements, then click Finish If you get a security warning saying that the authenticity or validity of the software can't be established, click OK 4. When installation completes, restart Eclipse 9
Step 3. Install the ADT plugin for Eclipse (Cont.) Step 3-2. Configure the ADT Plugin Once restart, check the location of Android SDK directory Select Window -> Preferences -> Android -> SDK Location If succeeding, Select Window -> Show Toolbar to enable ADT toolbar To update the ADT Plugin, select Help -> Check for Updates 10
Building the First App Creating an Android Project Create a Project with Eclipse Create a Project with Command Line Tools Running Your Application Run on a Real Device Run on the Emulator Building a Simple User Interface Starting Another Activity The default project includes a default set of "Hello World" source files that allow you to immediately run the app 11
Create a Project with Eclipse 1. Click New Android App Project in the toolbar or New -> Select a Wizard -> Android -> Android Application Project 2. Fill in the form Application Name: My First App Project Name: MyFirstApp Package Name: userid.example.myfirstapp Unique namespace across all packages installed on the Android system Build SDK: default set to the latest version Minimum Required SDK: the lowest version for support your app 12
Create a Project with Eclipse (Cont.) 3. Configure the launcher icon May use the default to generate an icon for all screen densities Before publishing app, be sure the icon meets the specifications defined in the Iconography design guide 4. Select an activity template from which to begin building your app For this project, select BlankActivity Leave all the details for the activity in their default state and click Finish It may ask to install dependencies (Android Support Library) 13
My First App Created 14
File List of <Workspace>\My First App.classpath.project AndroidManifest.xml bin/androidmanifest.xml bin/jarlist.cache bin/classes/wuch/example/my/first/app/ BuildConfig.class MainActivity.class R$attr.class R$drawable.class R$id.class R$layout.class R$menu.class R$string.class R$style.class R.class gen/wuch/example/my/first/app/ BuildConfig.java R.java ic_launcher-web.png libs/android-support-v4.jar Describes the fundamental characteristics and defines each of its components proguard-project.txt project.properties For app resources res/drawable-hdpi/ic_action_search.png res/drawable-hdpi/ic_launcher.png res/drawable-ldpi/ic_launcher.png res/drawable-mdpi/ic_action_search.png res/drawable-mdpi/ic_launcher.png res/drawable-xhdpi/ic_action_search.png res/drawable-xhdpi/ic_launcher.png res/layout/activity_main.xml res/menu/activity_main.xml res/values/strings.xml res/values/styles.xml res/values-v11/styles.xml res/values-v14/styles.xml defining UI Default Activity class loads a layout file that says "Hello World" src/wuch/example/my/first/app/mainactivity.java For app's main source files 15
src\com\wuch\my\first\app/mainactivity.java package com.wuch.my.first.app; import android.os.bundle; import android.app.activity; import android.view.menu; public class MainActivity extends Activity { @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } } @Override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.activity_main, menu); return true; } 16
res\values\strings.xml <resources> <string name="app_name">my First App</string> <string name="hello_world">hello world!</string> <string name="menu_settings">settings</string> <string name="title_activity_main">mainactivity</string> </resources> 17
Create a Project with Command Line Tools 1. Make sure to add Android SDK paths set PATH= U:\Data\Android\android-sdk\platformtools\; U:\Data\Android\androidsdk\tools 2. Change directories into the Android SDK s tools/ path U: cd \Data\Android\android-sdk\tools 3. Execute to check settings android list targets 4. Create projects android create project --target <target-id> --name MyFirstApp \ --path <path-to-workspace>/myfirstapp --activity MainActivity \ --package com.example.myfirstapp Replace <target-id> with an id from targets Replace <path-to-workspace> with the location to save the Android projects 18
Running the App - Run on a Real Device 1. Plug in your device 2. Ensure that USB debugging is enabled in the device Settings -> Applications -> Development 3a. Run the app from Eclipse Open the project's file and click Run from the toolbar Eclipse installs the app on the connected device and starts it 3b. Run the app from a command line 1.Make sure the Android SDK platform-tools/ directory is included in PATH 2.Change directories to the root of the Android project and execute: ant debug 3.Execute: adb install bin/myfirstapp-debug.apk 4.On the device, locate MyFirstActivity and open it 19
Running the App - Run on the Emulator Firstly create an Android Virtual Device (AVD) 1. Launch the Android Virtual Device Manager: In Eclipse, click AVD Manager from the toolbar From the command line, change directories to <sdk>/tools/ and execute: android avd 2. In the AVD Manager panel, click New 3. Fill in the details for the AVD Name: My_Phone Platform target: Android 4.1.2 SD card size: 512 MiB Snapshot: Enabled Skin: (HVGA is default) 4. Click Create AVD 5. Select the new AVD and click Start 6. After the emulator boots up, unlock the emulator screen 7. Open the project's files and click Run from the toolbar. Eclipse installs the app and starts it Run the app from the command line 1.Change directories to the root of the Android project and execute: ant debug 2.execute:adb install bin/myfirstapp-debug.apk 3.On the emulator, locate MyFirstActivity 20
Default Location of AVD files Android tool always creates an <AVD_name>.ini file for the AVD at the root of the.android/avd/ directory ~/.android/avd/ (on Linux/Mac) C:\Documents and Settings\<user>\.android\ on Windows XP, and C:\Users\<user>\.android\ on Windows 7 and Vista Choice: move.android from your directory to the default ~/.android/avd/ http://developer.android.com/tools/devices/managing-avdscmdline.html#defaultlocation You may need to copy your AVD to.android/avd 21
Exercise Exercise: Change message to Hello world! Cool NUKCSIE! 22
Installation Case Android SDK (2.2 + 4.2): 2.46GB Eclipse: 299MB AVD (512MB SD): 518MB My First App: 1.02MB 23