Senin, 22 September 2014

How To Make Your First Android App "Hello World"

When we entering a new programming language we always gonna make a Hello World application first. It's look like a tradition I think. Making a Hello World application will help us understand how to display a character on the monitor.
Nice first step, isn't? :)
In android we have three way to make an Hello World Application : Instant even without write a code, by Java coding and by XML. Let's try instant hello world first. 

Step 0: Read
Goto "Android Training" @ http://developer.android.com/training/index.html. Read "Get Started", "Building your first app".
You will get many reference too that will help you to understanding Android programming from this website.
Step 1: Create a New Android Project
  1. Launch Eclipse. For "ADT Bundle", run "eclipse.exe" under the "eclipse" sub-directory. Choose a NEW workspace (don't use you previous workspace).
  2. Close the "Welcome" screen, if it appears.
  3. From "File" menu ⇒ New ⇒ Project... ⇒ "Android Application Project" ⇒ Next.
  4. The "New Android Project" dialog appears:
    1. In "Application Name": Enter "Hello Android" - this is the Android application name that shows up on the real device.
    2. In "Project Name": Enter "HelloAndroid" (default) - this is the Eclipse's project name.
    3. In "Package Name": Enter "com.example.helloandroid" (default).
    4. In "Minimum Required SDK": Select "API 8: Android 2.2 (Froyo)" (default) - almost all of the Android devices meet this minimum requirement.
    5. In "Target SDK" and "Compile With": Select the latest Android version.
    6. For "Theme", use the default ⇒ Next.
  5. The "Configure Project" dialog appears ⇒ Use the defaults ⇒ Next.
  6. The "Configure Launcher Icon" dialog appears, which allows you to set the application's icon to be displayed on the devices ⇒ Use the defaults ⇒ Next.
  7. The "Create Activity" dialog appears ⇒ Check "Create Activity" Box (default) ⇒ Select "Blank Activity" (default) ⇒ Next.
  8. The "Blank Activity" dialog appears.
    1. In "Activity Name": Enter "MainActivity" (default).
    2. In "Layout Name": Enter "activity_main" (default) ⇒ Finish.
  9. By default, a hello-world app is created.
Step 2a: Setup Emulator (or Android Virtual Device (AVD))
Android Virtual Devices (AVDs) are emulators that allow you to test your application without the real device. You can create AVDs for different android platforms (from Android 1.x to Android 4.x) and configurations (e.g., phone/pad, screen size, orientation, SD card and its capacity).
  1. From Eclipse's "Window" menu ⇒ Preferences ⇒ Android ⇒ In "SDK Location", check and confirm it contains your Android's SDK installed directory.
  2. Start the AVD Manager: From Eclipse's "Window" menu ⇒ Select "AVD Manager". (You could also start the AVD manager by running "AVD Manager.exe" under the "sdk\tools\lib".)
  3. The "Android Virtual Device Manager" dialog appears ⇒ "New".
  4. The "Create New Android Virtual Device (AVD)" dialog appears.
    1. In "Name", enter "Android44_qvga".
    2. In "Device", select "2.7 QVGA" (try the smallest device first).
    3. In "Target", select "Android 4.4 - API Level 19".
    4. Set "SD Card Size" to 10 MB (do not set a huge SD Card size, which would take hours to create.) ⇒ OK.
Notes: For windows, the AVD is saved in "c:\Users\<user>\.android\avd\<avd-profile-name>.avd". The AVD location is shown in AVD manager on top of the table.
Step 2b: Launch Emulator (or AVD)
You can test your AVD created in the previous step by launching the emulator.
Start the AVD Manager: From Eclipse's "Window" menu ⇒ run "AVD Manager" ⇒ Select a AVD (e.g., "Android44_qvga") ⇒ Click the "Start" button ⇒ The "Launch Options" dialog appears ⇒ Launch.
WAIT patiently! The emulator is VERY SLOW and takes a few MINUTES to launch. Wait for the "Android" logo to appears and disappear. If a lock appears, unlock the screen by dragging the lock to the right.
You can change the orientation (between portrait and landscape) of the the emulator via "ctrl-F11".
We typically create different AVDs to emulate different real devices, e.g., Android44_xga of resolution (1024x768 XGA).
DO NOT CLOSE the emulator. Just leave it running. Trust me, it takes time to re-start the emulator!!!
Step 3: Run the Android App
Before running the Android app, turn on the "Error Log" and "Progress" views: From "Window" ⇒ "Show View" ⇒ "Error Log". Repeat for "Progress" view.
Now, run the application by right-click on the "HelloAndroid" PROJECT NODE ⇒ "Run As" ⇒ "Android Application".
Be patient! It takes a few MINUTES to fire up the emulator (if the emulator has not been started)! Watch the "Progress" (for launch progess), Android's "Console" (for messages), and "Error Log" (for error messages).
[2014-03-20 18:20:26 - HelloAndroid] ------------------------------
[2014-03-20 18:20:26 - HelloAndroid] Android Launch!
[2014-03-20 18:20:26 - HelloAndroid] adb is running normally.
[2014-03-20 18:20:26 - HelloAndroid] Performing com.example.helloandroid.MainActivity activity launch
[2014-03-20 18:20:26 - HelloAndroid] Automatic Target Mode: using existing emulator 'emulator-5554'
                                     running compatible AVD 'Android44_phone'
[2014-03-20 18:20:26 - HelloAndroid] Uploading HelloAndroid.apk onto device 'emulator-5554'
[2014-03-20 18:20:30 - HelloAndroid] Installing HelloAndroid.apk...
[2014-03-20 18:20:39 - HelloAndroid] Success!
[2014-03-20 18:20:40 - HelloAndroid] Starting activity com.example.helloandroid.MainActivity on device emulator-5554
[2014-03-20 18:20:42 - HelloAndroid] ActivityManager: Starting: Intent { act=android.intent.action.MAIN
                                     cat=[android.intent.category.LAUNCHER] cmp=com.example.helloandroid/.MainActivity }
Once the emulator started, unlock the device by holding and sweeping the "lock" to the right (or left). It shall launch your Hello-world app, and displays "hello, world" on the screen with a title "Hello Android".
If your program is not launched automatically, try launching it from the "app menu" manually, after the emulator is started. Look for the icon marked "Hello Android".



Trying launching the app from "HOME" ⇒ "..." ⇒ Look for the icon "Hello Android".
Also try "HOME" ⇒ "..." ⇒ "MENU" ⇒ "Manage Apps" ⇒ Select "HelloAndroid" ⇒ Un-install.
NOTE: DO NOT CLOSE the emulator, as it really takes a long time to start. You could always re-run or run new applications on the same emulator.
Step 5: Run the Android App on Real Devices
To run the Android app on a real device (Android Phone or Android Pad):
  1. Connect the real device to your computer. Make sure that you have the "USB Driver" for your device installed on your computer. You can find the "Google USB Driver" @ http://developer.android.com/sdk/win-usb.html, and Google's certified "OEM USB Drivers" @ http://developer.android.com/tools/extras/oem-usb.html. If you device is not certified there, good luck! It took me many hours to find a compatible driver for my cheap Android Pad.
  2. Enable "USB Debugging" mode on your real device: from "Settings" ⇒ "Applications" ⇒ "Development" ⇒ Check "USB Debugging". This allows Android SDK to transfer data between your computer and your device.
    Also enable "Unknown source" from "Applications". This allows applications from unknown sources to be installed on the device.
  3. You shall see the message "USB Debugging Connected" when you plugs the USB cable into your computer.
  4. From Eclipse, right-click on the project node ⇒ Run As ⇒ Android Application.
  5. The "Android Device Chooser" dialog appears. Select your real device (instead of the AVD emulator) ⇒ OK.
  6. Eclipse ADT installs the app on the connected device and starts it.
  7. You can unplug the device. The app has been installed. You can un-install the app via "Manage Apps".
(Advanced) Alternatively, you can also use the "adb" (Android Debug Bridge) tool (under "sdk\platform-tools") to install the ".apk" file ("HelloAndroid.apk") onto the real devices. The "sdk\platform-tools" directory must be included in the PATH.
// Change directory to <project-root>\bin, where the ".apk" is located
// -d option for real device
> adb -d install filename.apk
2402 KB/s (157468 bytes in 0.064s)
        pkg: /data/local/tmp/filename.apk
Success
  
> adb --help
This is the end of our Hello World tutorial in this post, maybe you will find it is weird because we don't type any coding in this application. That's because "Hello World" are created by default when we make an Android Application, so we don't even need to type the Hello World itself.
I will make a deepening of Hello World in next post so it will make you clear.

1 komentar:

  1. You’ve got some interesting points in this article. I would have never considered any of these if I didn’t come across this. Thanks!. android-emulateurs.com

    BalasHapus