Android

From Ggl's wiki

Jump to: navigation, search

Contents

Installation

I just installed the SDK from the android web site. Then I installed a 2.1 target with the Android graphical tool (just type 'android' in a shell).

note: if you want to try using Eclipse en Debian or Ubuntu start it with 'eclipse -vmargs -Djava.net.preferIPv4Stack=true'

Configuration

I added two functions in my ~/.bashrc to define the environment for Android:

andoid_begin() {
    export OLDPATH=${PATH}
    export PATH=/home/ggl/src/android/android-sdk-linux_86/tools:${PATH}
}

android_end() {
    export PATH=${OLDPATH}
}

After that:

$ android_begin
$ android list
Available Android targets:
id: 1 or "android-7"
     Name: Android 2.1
     Type: Platform
     API level: 7
     Revision: 1
     Skins: WQVGA400, QVGA, WQVGA432, WVGA800, WVGA854, HVGA (default)
Available Android Virtual Devices:

$ android create avd --target 1 --name test_target0
Android 2.1 is a basic Android platform.
Do you wish to create a custom hardware profile [no]
Created AVD 'test_target0' based on Android 2.1, with the following hardware config:
hw.lcd.density=160

Create a project

$ android create project --target 1 --name hello --path ~/code/android/hello --activity HelloActivity --package com.example.greg.hello
Created project directory: /home/ggl/code/android/hello
Created directory /home/ggl/code/android/hello/src/com/example/greg/hello
Added file /home/ggl/code/android/hello/src/com/example/greg/hello/HelloActivity.java
Created directory /home/ggl/code/android/hello/res
Created directory /home/ggl/code/android/hello/bin
Created directory /home/ggl/code/android/hello/libs
Created directory /home/ggl/code/android/hello/res/values
Added file /home/ggl/code/android/hello/res/values/strings.xml
Created directory /home/ggl/code/android/hello/res/layout
Added file /home/ggl/code/android/hello/res/layout/main.xml
Created directory /home/ggl/code/android/hello/res/drawable-hdpi
Created directory /home/ggl/code/android/hello/res/drawable-mdpi
Created directory /home/ggl/code/android/hello/res/drawable-ldpi
Added file /home/ggl/code/android/hello/AndroidManifest.xml
Added file /home/ggl/code/android/hello/build.xml

I copy the file from the hello example in ~/code/android/hello/HelloActivity.java: package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;

public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Update the project

$ android update project --target 1 --name hello --path ~/code/android/hello
Updated default.properties
Updated local.properties
File build.xml is too old and needs to be updated.
Updated file /home/ggl/code/android/hello/build.xml

Build the project

You can simply build the useless empty project from its templates:

$ ant debug
Buildfile: /home/ggl/code/android/hello/build.xml
   [setup] Project Target: Android 2.1
   [setup] API level: 7
   [setup] WARNING: No minSdkVersion value set. Application will install on all Android versions.

-compile-tested-if-test:

-dirs:
    [echo] Creating output directories if needed...
   [mkdir] Created dir: /home/ggl/code/android/hello/gen
   [mkdir] Created dir: /home/ggl/code/android/hello/bin/classes

-resource-src:
    [echo] Generating R.java / Manifest.java from the resources...

-aidl:
    [echo] Compiling aidl files into Java classes...

compile:
    [javac] /home/ggl/src/android/android-sdk-linux_86/platforms/android-7/templates/android_rules.xml:248: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
    [javac] Compiling 2 source files to /home/ggl/code/android/hello/bin/classes

-dex:
     [echo] Converting compiled files and external libraries into /home/ggl/code/android/hello/bin/classes.dex...
     [echo]          

-package-resources:
     [echo] Packaging resources
 [aaptexec] Creating full resource package...

-package-debug-sign:
[apkbuilder] Creating hello-debug-unaligned.apk and signing it with a debug key...
[apkbuilder] Using keystore: /home/ggl/.android/debug.keystore

debug:
     [echo] Running zip align on final apk...
     [echo] Debug Package: /home/ggl/code/android/hello/bin/hello-debug.apk

BUILD SUCCESSFUL
Total time: 8 seconds

Start the binary in the emulator

$ android list avd
Available Android Virtual Devices:
    Name: test_target0
    Path: /home/ggl/.android/avd/test_target0.avd
  Target: Android 2.1 (API level 7)
    Skin: HVGA
$ emulator -avd test_target0
$ adb install ~/code/android/hello/bin/hello-debug.apk
* daemon not running. starting it now *
* daemon started successfully *
error: device offline
$ adb install ~/code/android/hello/bin/hello-debug.apk
220 KB/s (13358 bytes in 0.059s)
        pkg: /data/local/tmp/hello-debug.apk
Success

The '-r' switch reinstalls the application. We may use it after a having modified it:

$ adb install -r ~/code/android/hello/bin/hello-debug.apk
190 KB/s (13320 bytes in 0.068s)
        pkg: /data/local/tmp/hello-debug.apk
Success

Using resources

We applied these modification in order to use resources instead of values in the java code:

$ git diff
diff --git a/res/layout/main.xml b/res/layout/main.xml
index 46a2900..5f4c383 100644
--- a/res/layout/main.xml
+++ b/res/layout/main.xml
@@ -7,7 +7,7 @@
 <TextView  
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
-    android:text="Hello World, HelloActivity"
+    android:text="@string/hello"
     />
 </LinearLayout>
 
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 0a80762..9b2b7ed 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -1,4 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
-    <string name="app_name">HelloActivity</string>
+    <string name="app_name">My Hello Android!</string>
+    <string name="hello">Hello Android! It\'s great :)</string>
 </resources>
diff --git a/src/com/example/greg/hello/HelloActivity.java b/src/com/example/greg/hello/HelloActivity.java
index f311334..ede11ce 100644
--- a/src/com/example/greg/hello/HelloActivity.java
+++ b/src/com/example/greg/hello/HelloActivity.java
@@ -2,7 +2,6 @@ package com.example.greg.hello;
 
 import android.app.Activity;
 import android.os.Bundle;
-import android.widget.TextView;
 
 public class HelloActivity extends Activity
 {
@@ -12,8 +11,5 @@ public class HelloActivity extends Activity
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
-        TextView tv = new TextView(this);
-        tv.setText("Hello, Android");
-        setContentView(tv);
     }
 }