Android hello world without eclipse
April 01, 2011 14:29:25 Last update: June 29, 2011 13:58:27
- Start the emulator (create an AVD if none exists)
$ tools/emulator -avd Simple8
- Create new project
$ tools/android create project \ > --package com.android.helloworld \ > --activity HelloWorld \ > --target 2 \ > --path HelloWorld Created project directory: HelloWorld Created directory /home/freyo/work/HelloWorld/src/com/android/helloworld Added file HelloWorld/src/com/android/helloworld/HelloWorld.java Created directory /home/freyo/work/HelloWorld/res Created directory /home/freyo/work/HelloWorld/bin Created directory /home/freyo/work/HelloWorld/libs Created directory /home/freyo/work/HelloWorld/res/values Added file HelloWorld/res/values/strings.xml Created directory /home/freyo/work/HelloWorld/res/layout Added file HelloWorld/res/layout/main.xml Created directory /home/freyo/work/HelloWorld/res/drawable-hdpi Created directory /home/freyo/work/HelloWorld/res/drawable-mdpi Created directory /home/freyo/work/HelloWorld/res/drawable-ldpi Added file HelloWorld/AndroidManifest.xml Added file HelloWorld/build.xml
where "--target 2" identifies the target platform as displayed by "tools/android list targets", which is stored in the properties filedefault.propertiesin the project root folder.
-
cd HelloWorldand install debug package onto the running emulator:$ ant install Buildfile: build.xml [setup] Android SDK Tools Revision 6 [setup] Project Target: Google APIs [setup] Vendor: Google Inc. [setup] Platform Version: 2.2 [setup] API level: 8 [setup] WARNING: No minSdkVersion value set. Application will install on all Android versions. [setup] Importing rules file: platforms/android-8/ant/ant_rules_r2.xml -compile-tested-if-test: -dirs: [echo] Creating output directories if needed... [mkdir] Created dir: /home/freyo/work/HelloWorld/gen [mkdir] Created dir: /home/freyo/work/HelloWorld/bin [mkdir] Created dir: /home/freyo/work/HelloWorld/bin/classes -resource-src: [echo] Generating R.java / Manifest.java from the resources... -aidl: [echo] Compiling aidl files into Java classes... compile: [javac] Compiling 2 source files to /home/freyo/work/HelloWorld/bin/classes -dex: [echo] Converting compiled files and external libraries into /home/freyo/work/HelloWorld/bin/classes.dex... -package-resources: [echo] Packaging resources [aaptexec] Creating full resource package... -package-debug-sign: [apkbuilder] Creating HelloWorld-debug-unaligned.apk and signing it with a debug key... [apkbuilder] Using keystore: /home/freyo/.android/debug.keystore debug: [echo] Running zip align on final apk... [echo] Debug Package: /home/freyo/work/HelloWorld/bin/HelloWorld-debug.apk install: [echo] Installing /home/freyo/work/HelloWorld/bin/HelloWorld-debug.apk onto default emulator or device... [exec] 204 KB/s (13203 bytes in 0.063s) [exec] pkg: /data/local/tmp/HelloWorld-debug.apk [exec] Success BUILD SUCCESSFUL Total time: 5 seconds - Launch the Hello World application on the emulator. You'll see something like this:
- Edit
res/values/string.xml, change the contents to:<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">HelloWorld</string> <string name="hello">Hello World!</string> </resources>
- Edit
res/layout/main.xml, change the contents to:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout>
The contents of the text area now refer to a string defined in the resource filestrings.xml, instead of a literal string.
- Reinstall the debug apk with ant:
ant install
- The application should display: