Android是基于Linux平台的开源手机操作系统的名称,本文给大家介绍的是如何在Ubuntu 11.04环境下使用Eclipse, Android SDK和 PhoneGap搭建Android开发环境。

AD:

本文给大家讲解下如何在Ubuntu 11.04环境下使用Eclipse, Android SDK和 PhoneGap搭建Android开发环境。

#1,安装Eclipse 和 Android SDK/PhoneGap所需软件包:

打开终端,输入下面命令完成题目所述过程:

sudo apt-get install openjdk-6-jdk eclipse ruby rubygems ruby-dev libnokogiri-ruby git ant libxml2 libxml2-dev libxslt1-dev

若是64位,还需要安装下面两个软件包:

sudo apt-get install lib32stdc++6 ia32-libs

#2,安装Android SDK

打开网络浏览器,输入地址 http://developer.android.com/sdk/index.html,下载所需的平台(Win,Mac,Linux)的Android SDK版本。我们选择Linux(i386)即可(32位和64位平台皆适用)。

或者,用下面命令下载并解压文件包:

cd ~
wget http://dl.google.com/android/android-sdk_r11-linux_x86.tgz
tar xvfz android-sdk_r11-linux_x86.tgz

由上述命令,我们可得到文件目录 android-sdk-linux_x86 ,现在我们要将$HOME/android-sdk-linux_x86/tools 目录 和 $HOME/android-sdk-linux_x86/platform-tools 目录添加到PATH 路径变量中去:

打开 ~/.profile文件

gedit ~/.profile

添加下面一行内容到该文件末尾:

[...]
PATH="$HOME/android-sdk-linux_x86/tools:$HOME/android-sdk-linux_x86/platform-tools:$PATH"

然后,运行下面命令将上述改动生效:

export 
PATH="$HOME/android-sdk-linux_x86/tools:$HOME/android-sdk-linux_x86/platform-tools:$PATH"

现在,启动 Android SDK:

android

下图就是Android SDK所示:

Ubuntu 11.04上搭建Android开发环境_开发环境

接下来,选择左侧的“ Available packages ”,后选择右侧栏内的“ Android Repository”,选中所有“ Android Repository”组内的软件包,点击“安装所选软件包”即可。接下来选择“接受”,继续安装完毕即可。

若提示“ADB需要重启”时,我们选择“Yes”,重启ADB。

下载完毕后,点击“close”,关闭对话框。

#3,移植Android SDK到Eclipse

若是你用Eclipse创建Android应用,而非命令行下,则我们需要将移植Android SDK到Eclipse。

首先,从“Applications > Programming > Eclipse”,启动Eclipse。

接下来,请各位读者按照原文,自己调试即可@ 

If you want to use Eclipse to create your Android apps instead of the command line, you need to integrate the Android SDK into Eclipse. To do this, start Eclipse (Applications > Programming > Eclipse):

Ubuntu 11.04上搭建Android开发环境_Ubuntu 11.04_02

Click on OK if you see the Workspace Launcher window (you can check the Use this as the default and do not ask again checkbox to not see this message anymore):

Ubuntu 11.04上搭建Android开发环境_开发环境_04

Now in Eclipse, go to Help > Install New Software...:

Ubuntu 11.04上搭建Android开发环境_移动开发_06

The Available Software window opens. Click on the Add... button:

Ubuntu 11.04上搭建Android开发环境_开发环境_08

A small Add Site window opens. Fill in the following values and click on OK:

  • Name: ADT Plugin
  • Location: https://dl-ssl.google.com/android/eclipse/

Ubuntu 11.04上搭建Android开发环境_Android_10

Back in the Available Software window, you should now see the software Developer Tools. Select that software and click on Next >:

Ubuntu 11.04上搭建Android开发环境_休闲_12

In the next window, click on Next > again:

Ubuntu 11.04上搭建Android开发环境_休闲_14

Now accept the license agreements and click on Finish:

Ubuntu 11.04上搭建Android开发环境_Ubuntu 11.04_16

The packages needed to integrate the Android SDK into Eclipse are now being downloaded and installed:

Ubuntu 11.04上搭建Android开发环境_Ubuntu 11.04_18

If you see the following message about unsigned content, it's ok to click on the OK button:

Ubuntu 11.04上搭建Android开发环境_Android_20

At the end of the installation, Eclipse needs to be restarted. To do this, click on Yes:

Ubuntu 11.04上搭建Android开发环境_移动开发_22

Now there's one final step to complete the Android SDK integration in Eclipse: Go to Window > Preferences:

Ubuntu 11.04上搭建Android开发环境_移动开发_24

In the Preferences window, select Android in the left panel, and then click on the Browse... button that belongs to the SDK Location field in the right panel:

Ubuntu 11.04上搭建Android开发环境_开发环境_26

Select the directory where the Android SDK is located (it's android-sdk-linux_x86 in your home directory) and click on OK:

Ubuntu 11.04上搭建Android开发环境_移动开发_28

Back in the Preferences window, click on Apply (you should then see the supported Android versions in the Target Name column)...

Ubuntu 11.04上搭建Android开发环境_移动开发_30

... and then on OK:

Ubuntu 11.04上搭建Android开发环境_Android_32

Ubuntu 11.04上搭建Android开发环境_移动开发_03

 

5 Installing PhoneGap

 

To install PhoneGap, open Firefox and go to https://github.com/phonegap/phonegap-android. Click on the Downloads button:

Ubuntu 11.04上搭建Android开发环境_Android_34

Select Download .tar.gz from the overlay that comes up:

Ubuntu 11.04上搭建Android开发环境_移动开发_36

In the Firefox download dialogue, please choose Save File:

Ubuntu 11.04上搭建Android开发环境_开发环境_38

After the download has finished, go to the directory where the download has been saved (e.g. ~/Downloads/):

cd ~/Downloads/

Take a look at the directory's contents to learn how the PhoneGap download is named:

ls -l

falko@falko-virtual-machine:~/Downloads$ ls -l
total 11200
-rw-r--r-- 1 falko falko 11467178 2011-06-21 14:01 phonegap-phonegap-android-0.9.5.1-15-g939754e.tar.gz
falko@falko-virtual-machine:~/Downloads$

As you see, in my case it is named phonegap-phonegap-android-0.9.5.1-15-g939754e.tar.gz. Now lets uncompress the file:

tar xvfz phonegap-phonegap-android-0.9.5.1-15-g939754e.tar.gz

In my case this creates the directory phonegap-phonegap-android-939754e inside the Downloads directory. I want to move that directory to my home directory and rename it to phonegap-phonegap-android:

mv phonegap-phonegap-android-939754e ~/phonegap-phonegap-android

We must add the bin directory inside $HOME/phonegap-phonegap-android to our PATH variable, therefore we edit ~/.profile again:

cd ~
gedit ~/.profile

Replace the previously added PATH variable with this one:

[...]
PATH="$HOME/android-sdk-linux_x86/tools:$HOME/android-sdk-linux_x86/platform-tools:$HOME/phonegap-phonegap-android/bin:$PATH"

To make the change effective, run:

export PATH="$HOME/android-sdk-linux_x86/tools:$HOME/android-sdk-linux_x86/platform-tools:$HOME/phonegap-phonegap-android/bin:$PATH"

Now we are finished with setting up our Android development environment; finally we can start doing things with it!

 

6 Starting An Android Emulator

The Android SDK comes with an emulator so that you can test your apps in the emulator instead of on a real Android device. To create an emulator, we first need to know which Android versions (targets) are available:

android list targets

falko@falko-virtual-machine:~$ android list targets
Available Android targets:
id: 1 or "android-3"
     Name: Android 1.5
     Type: Platform
     API level: 3
     Revision: 4
     Skins: HVGA (default), QVGA-L, HVGA-P, HVGA-L, QVGA-P
id: 2 or "android-4"
     Name: Android 1.6
     Type: Platform
     API level: 4
     Revision: 3
     Skins: HVGA, QVGA, WVGA854, WVGA800 (default)
id: 3 or "android-7"
     Name: Android 2.1-update1
     Type: Platform
     API level: 7
     Revision: 2
     Skins: HVGA, QVGA, WQVGA400, WVGA854, WQVGA432, WVGA800 (default)
id: 4 or "android-8"
     Name: Android 2.2
     Type: Platform
     API level: 8
     Revision: 2
     Skins: HVGA, QVGA, WQVGA400, WVGA854, WQVGA432, WVGA800 (default)
id: 5 or "android-10"
     Name: Android 2.3.3
     Type: Platform
     API level: 10
     Revision: 1
     Skins: HVGA, QVGA, WQVGA400, WVGA854, WQVGA432, WVGA800 (default)
id: 6 or "android-11"
     Name: Android 3.0
     Type: Platform
     API level: 11
     Revision: 1
     Skins: WXGA (default)
id: 7 or "android-12"
     Name: Android 3.1
     Type: Platform
     API level: 12
     Revision: 2
     Skins: WXGA (default)
falko@falko-virtual-machine:~$

I will now create an emulator called mySim for Android 2.3.3 (id 5 or android-10):

android create avd -n mySim -t android-10

falko@falko-virtual-machine:~$ android create avd -n mySim -t android-10
Android 2.3.3 is a basic Android platform.
Do you wish to create a custom hardware profile [no]
<-- ENTER
Created AVD 'mySim' based on Android 2.3.3,
with the following hardware config:
hw.lcd.density=240
vm.heapSize=24
hw.ramSize=256
falko@falko-virtual-machine:~$

To start the emulator, run:

emulator -avd mySim -scale 0.75

It can take a few minutes until the emulator is up and running, but then it should look as follows:

Ubuntu 11.04上搭建Android开发环境_休闲_40

Ubuntu 11.04上搭建Android开发环境_Android_35

7 Creating An Android App

 

To build my Android app (which I call TweetMe) from its HTML, CSS, and JavaScript sources, I create a folder called tweetme in my home directory, and in that tweetme folder I place another folder called tweetme wich contains the sources (HTML, CSS, JavaScript):

HOME
    |
    |
    + tweetme
             |
             |
             +tweetme
                     |
                     |
                     SOURCES (HTML, CSS, JavaScript)

Go to ~/tweetme/tweetme/:

cd ~/tweetme/tweetme/

You should see the HTML, CSS, JavaScript sources in that directory:

ls -l

falko@falko-virtual-machine:~/tweetme/tweetme$ ls -l
total 96
-rw-r--r-- 1 falko falko  4233 2011-01-24 00:20 index.html
drwxr-xr-x 2 falko falko  4096 2011-06-21 14:23 jqtouch
-rw-r--r-- 1 falko falko 78601 2011-01-23 23:15 jquery.js
drwxr-xr-x 4 falko falko  4096 2011-06-21 14:23 themes
falko@falko-virtual-machine:~/tweetme/tweetme$

Now we can create an app either from the command line or by using Eclipse:

 

7.1 Creating An Android App From The Command Line

In the ~/tweetme/tweetme/ directory, run:

droidgap create

(If you get the error message droidgap: command not found, please run

export PATH="$HOME/android-sdk-linux_x86/tools:$HOME/android-sdk-linux_x86/platform-tools:$HOME/phonegap-phonegap-android/bin:$PATH"

and try again. )

This will create the directory ~/tweetme/tweetme_android which contains everything we need to build our final app from it. The ~/tweetme/tweetme_android/assets/www/ directory contains our HTML, CSS, JavaScript sources plus the phonegap.<version>.js file.

cd ../tweetme_android/

ls -l

falko@falko-virtual-machine:~/tweetme/tweetme_android$ ls -l
total 44
-rw-r--r-- 1 falko falko 2241 2011-06-21 14:25 AndroidManifest.xml
drwxr-xr-x 3 falko falko 4096 2011-06-21 14:25 assets
drwxr-xr-x 2 falko falko 4096 2011-06-21 14:25 bin
-rw-r--r-- 1 falko falko  696 2011-06-21 14:25 build.properties
-rw-r--r-- 1 falko falko 2891 2011-06-21 14:25 build.xml
-rw-r--r-- 1 falko falko  363 2011-06-21 14:25 default.properties
drwxr-xr-x 2 falko falko 4096 2011-06-21 14:25 libs
-rw-r--r-- 1 falko falko  425 2011-06-21 14:25 local.properties
-rw-r--r-- 1 falko falko 1159 2011-06-21 14:25 proguard.cfg
drwxr-xr-x 7 falko falko 4096 2011-06-21 14:25 res
drwxr-xr-x 3 falko falko 4096 2011-06-21 14:25 src
falko@falko-virtual-machine:~/tweetme/tweetme_android$

ls -l assets/www/

falko@falko-virtual-machine:~/tweetme/tweetme_android$ ls -l assets/www/
total 228
-rw-r--r-- 1 falko falko   4233 2011-06-21 14:25 index.html
drwxr-xr-x 2 falko falko   4096 2011-06-21 14:25 jqtouch
-rw-r--r-- 1 falko falko  78601 2011-06-21 14:25 jquery.js
-rw-r--r-- 1 falko falko 132955 2011-06-21 14:25 phonegap.0.9.5.js
drwxr-xr-x 4 falko falko   4096 2011-06-21 14:25 themes
falko@falko-virtual-machine:~/tweetme/tweetme_android$

Important: I found a problem with the phonegap.0.9.5.js file - it contains several lines with a prompt() function that asks the user some questions (it asks for getPort, getToken, getServer, restartServer, usePolling), and this is clearly not what you want inside your app - see the following screenshots:

Ubuntu 11.04上搭建Android开发环境_开发环境_42

Ubuntu 11.04上搭建Android开发环境_Android_44

Ubuntu 11.04上搭建Android开发环境_休闲_46

Ubuntu 11.04上搭建Android开发环境_Ubuntu 11.04_48

Ubuntu 11.04上搭建Android开发环境_移动开发_50

As a solution for this problem, you can either edit phonegap.0.9.5.js and comment out all lines that contain the prompt() function (there are six lines), or you download PhoneGap 0.9.4 from http://phonegap.googlecode.com/files/phonegap-0.9.4.zip (PhoneGap 0.9.4 doesn't use the prompt() function), extract the phonegap.0.9.4.js file from it, delete phonegap.0.9.5.js from the assets/www/ directory and place phonegap.0.9.4.js in the assets/www/ directory instead.

If you prefer to comment out all lines that contain the prompt() function in phonegap.0.9.5.js, but don't want to edit the file manually, you can use the following command which comments out all six lines automatically (the command works fine for phonegap.0.9.5.js; however, it might not work for future versions, so please keep this in mind!):

sed -i '/prompt/s;^;//;' assets/www/phonegap.0.9.5.js

The libs/ directory contains the file phonegap.<version>.jar:

ls -l libs/

falko@falko-virtual-machine:~/tweetme/tweetme_android$ ls -l libs/
total 136
-rw-r--r-- 1 falko falko 139004 2011-06-21 14:25 phonegap.0.9.5.jar
falko@falko-virtual-machine:~/tweetme/tweetme_android$

Now we must edit the index.html file in the assets/www/ folder and add the phonegap.<version>.js file to the <head></head> section (before all other JavaScript files/JavaScript code).

gedit assets/www/index.html

Let's assume the file starts as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Tweetme</title>

    <!-- include JQuery through Google API => Always have the latest version -->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript"> google.load("jquery", "1.3.2"); </script>

    <!-- import JQTouch -->
    <script src="jqtouch/jqtouch.min.js" type="application/x-javascript" charset="utf-8"></script>

    <!-- Import JQTouch default style (iPhone look).
       Replace the string "themes/apple" with "themes/jq" for a non-iPhone theme -->
    <link type="text/css" rel="stylesheet" media="screen" href="jqtouch/jqtouch.css">
    <link type="text/css" rel="stylesheet" media="screen" href="themes/apple/theme.css">
[...]

Add the line <script type="text/javascript" charset="utf-8" src="phonegap.0.9.5.js"></script> before all other JavaScript so that it looks as follows (if you have replaced phonegap.0.9.5.js with phonegap.0.9.4.js, please use the line <script type="text/javascript" charset="utf-8" src="phonegap.0.9.4.js"></script> instead ):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Tweetme</title>
    <script type="text/javascript" charset="utf-8" src="phonegap.0.9.5.js"></script>
    <!-- include JQuery through Google API => Always have the latest version -->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript"> google.load("jquery", "1.3.2"); </script>

    <!-- import JQTouch -->
    <script src="jqtouch/jqtouch.min.js" type="application/x-javascript" charset="utf-8"></script>

    <!-- Import JQTouch default style (iPhone look).
       Replace the string "themes/apple" with "themes/jq" for a non-iPhone theme -->
    <link type="text/css" rel="stylesheet" media="screen" href="jqtouch/jqtouch.css">
    <link type="text/css" rel="stylesheet" media="screen" href="themes/apple/theme.css">
[...]

Now, still in the ~/tweetme/tweetme_android directory, we can finally build our app:

ant debug

It should say BUILD SUCCESSFUL at the end:

[...]
-package-debug-sign:
[apkbuilder] Creating tweetme-debug-unaligned.apk and signing it with a debug key...

debug:
     [echo] Running zip align on final apk...
     [echo] Debug Package: /home/falko/tweetme/tweetme_android/bin/tweetme-debug.apk

BUILD SUCCESSFUL
Total time: 38 seconds
falko@falko-virtual-machine:~/tweetme/tweetme_android$

You can now find your app in the bin/ directory (called tweetme-debug.apk; apk is the extension for Android apps):

cd bin/
ls -l

falko@falko-virtual-machine:~/tweetme/tweetme_android/bin$ ls -l
total 808
drwxr-xr-x 3 falko falko   4096 2011-06-21 14:32 classes
-rw-r--r-- 1 falko falko 155652 2011-06-21 14:32 classes.dex
-rw-r--r-- 1 falko falko 171877 2011-06-21 14:32 tweetme.ap_
-rw-r--r-- 1 falko falko 244900 2011-06-21 14:32 tweetme-debug.apk
-rw-r--r-- 1 falko falko 244846 2011-06-21 14:32 tweetme-debug-unaligned.apk
falko@falko-virtual-machine:~/tweetme/tweetme_android/bin$

To install it to the first running emulator, we can run

adb -e install -r tweetme-debug.apk

falko@falko-virtual-machine:~/tweetme/tweetme_android/bin$ adb -e install -r tweetme-debug.apk
411 KB/s (244900 bytes in 0.581s)
        pkg: /data/local/tmp/tweetme-debug.apk
Success
falko@falko-virtual-machine:~/tweetme/tweetme_android/bin$

(Instead of running ant debug first and then adb -e install -r tweetme-debug.apk from the bin/ directory to install the app, we could have run

ant debug install

which would have built and afterwards installed the app in one go.)

Now go to your emulator, and you should find the new app listed among the other apps:

Ubuntu 11.04上搭建Android开发环境_Android_52

This is what the app looks like:

Ubuntu 11.04上搭建Android开发环境_休闲_54

Ubuntu 11.04上搭建Android开发环境_移动开发_56

Ubuntu 11.04上搭建Android开发环境_开发环境_58

Ubuntu 11.04上搭建Android开发环境_Android_35
 

7.2 Creating An Android App From Eclipse

 

In the ~/tweetme/tweetme/ directory, run:

droidgap create

(If you get the error message droidgap: command not found, please run

export PATH="$HOME/android-sdk-linux_x86/tools:$HOME/android-sdk-linux_x86/platform-tools:$HOME/phonegap-phonegap-android/bin:$PATH"

and try again. )

This will create the directory ~/tweetme/tweetme_android which contains everything we need to build our final app from it. The ~/tweetme/tweetme_android/assets/www/ directory contains our HTML, CSS, JavaScript sources plus the phonegap.<version>.js file.

cd ../tweetme_android/

ls -l

falko@falko-virtual-machine:~/tweetme/tweetme_android$ ls -l
total 44
-rw-r--r-- 1 falko falko 2241 2011-06-21 14:25 AndroidManifest.xml
drwxr-xr-x 3 falko falko 4096 2011-06-21 14:25 assets
drwxr-xr-x 2 falko falko 4096 2011-06-21 14:25 bin
-rw-r--r-- 1 falko falko  696 2011-06-21 14:25 build.properties
-rw-r--r-- 1 falko falko 2891 2011-06-21 14:25 build.xml
-rw-r--r-- 1 falko falko  363 2011-06-21 14:25 default.properties
drwxr-xr-x 2 falko falko 4096 2011-06-21 14:25 libs
-rw-r--r-- 1 falko falko  425 2011-06-21 14:25 local.properties
-rw-r--r-- 1 falko falko 1159 2011-06-21 14:25 proguard.cfg
drwxr-xr-x 7 falko falko 4096 2011-06-21 14:25 res
drwxr-xr-x 3 falko falko 4096 2011-06-21 14:25 src
falko@falko-virtual-machine:~/tweetme/tweetme_android$

ls -l assets/www/

falko@falko-virtual-machine:~/tweetme/tweetme_android$ ls -l assets/www/
total 228
-rw-r--r-- 1 falko falko   4233 2011-06-21 14:25 index.html
drwxr-xr-x 2 falko falko   4096 2011-06-21 14:25 jqtouch
-rw-r--r-- 1 falko falko  78601 2011-06-21 14:25 jquery.js
-rw-r--r-- 1 falko falko 132955 2011-06-21 14:25 phonegap.0.9.5.js
drwxr-xr-x 4 falko falko   4096 2011-06-21 14:25 themes
falko@falko-virtual-machine:~/tweetme/tweetme_android$

Important: I found a problem with the phonegap.0.9.5.js file - it contains several lines with a prompt() function that asks the user some questions (it asks for getPort, getToken, getServer, restartServer, usePolling), and this is clearly not what you want inside your app - see the following screenshots:

Ubuntu 11.04上搭建Android开发环境_开发环境_42

Ubuntu 11.04上搭建Android开发环境_Android_44

Ubuntu 11.04上搭建Android开发环境_休闲_46

Ubuntu 11.04上搭建Android开发环境_Ubuntu 11.04_48

Ubuntu 11.04上搭建Android开发环境_移动开发_50

As a solution for this problem, you can either edit phonegap.0.9.5.js and comment out all lines that contain the prompt() function (there are six lines), or you download PhoneGap 0.9.4 from http://phonegap.googlecode.com/files/phonegap-0.9.4.zip (PhoneGap 0.9.4 doesn't use the prompt() function), extract the phonegap.0.9.4.js file from it, delete phonegap.0.9.5.js from the assets/www/ directory and place phonegap.0.9.4.js in the assets/www/ directory instead.

If you prefer to comment out all lines that contain the prompt() function in phonegap.0.9.5.js, but don't want to edit the file manually, you can use the following command which comments out all six lines automatically (the command works fine for phonegap.0.9.5.js; however, it might not work for future versions, so please keep this in mind!):

sed -i '/prompt/s;^;//;' assets/www/phonegap.0.9.5.js

The libs/ directory contains the file phonegap.<version>.jar:

ls -l libs/

falko@falko-virtual-machine:~/tweetme/tweetme_android$ ls -l libs/
total 136
-rw-r--r-- 1 falko falko 139004 2011-06-21 14:25 phonegap.0.9.5.jar
falko@falko-virtual-machine:~/tweetme/tweetme_android$

Now open Eclipse and go to File > New > Project...:

Ubuntu 11.04上搭建Android开发环境_开发环境_70

In the New Project window, select Android > Android Project and click on Next >:

Ubuntu 11.04上搭建Android开发环境_Android_72

In the New Android Project window, select Create project from existing source (we have previously created the source by running droidgap create) and click on the Browse... button - don't fill out any other fields in that window:

Ubuntu 11.04上搭建Android开发环境_休闲_74

Select the ~/tweetme/tweetme_android directory and click on OK:

Ubuntu 11.04上搭建Android开发环境_开发环境_76

Back in the New Android Project window, all other fields should now be filled. Click on Finish:

Ubuntu 11.04上搭建Android开发环境_开发环境_78

You should now see the source tree of your app in the left panel in Android. Right-click the assets/www/index.html file and select Open With > Text Editor:

Ubuntu 11.04上搭建Android开发环境_Android_80

Add the phonegap.<version>.js file to the <head></head> section (before all other JavaScript files/JavaScript code).

Let's assume the file starts as follows:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Tweetme</title>

    <!-- include JQuery through Google API => Always have the latest version -->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript"> google.load("jquery", "1.3.2"); </script>

    <!-- import JQTouch -->
    <script src="jqtouch/jqtouch.min.js" type="application/x-javascript" charset="utf-8"></script>

    <!-- Import JQTouch default style (iPhone look).
       Replace the string "themes/apple" with "themes/jq" for a non-iPhone theme -->
    <link type="text/css" rel="stylesheet" media="screen" href="jqtouch/jqtouch.css">
    <link type="text/css" rel="stylesheet" media="screen" href="themes/apple/theme.css">
[...]

Add the line <script type="text/javascript" charset="utf-8" src="phonegap.0.9.5.js"></script> before all other JavaScript so that it looks as follows (if you have replaced phonegap.0.9.5.js with phonegap.0.9.4.js, please use the line <script type="text/javascript" charset="utf-8" src="phonegap.0.9.4.js"></script> instead ):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Tweetme</title>
    <script type="text/javascript" charset="utf-8" src="phonegap.0.9.5.js"></script>
    <!-- include JQuery through Google API => Always have the latest version -->
    <script type="text/javascript" src="http://www.google.com/jsapi"></script>
    <script type="text/javascript"> google.load("jquery", "1.3.2"); </script>

    <!-- import JQTouch -->
    <script src="jqtouch/jqtouch.min.js" type="application/x-javascript" charset="utf-8"></script>

    <!-- Import JQTouch default style (iPhone look).
       Replace the string "themes/apple" with "themes/jq" for a non-iPhone theme -->
    <link type="text/css" rel="stylesheet" media="screen" href="jqtouch/jqtouch.css">
    <link type="text/css" rel="stylesheet" media="screen" href="themes/apple/theme.css">
[...]

Now save the file.

Ubuntu 11.04上搭建Android开发环境_移动开发_82

Next right-click the libs/phonegap.<version>.jar file and select Build Path > Add to Build Path:

Ubuntu 11.04上搭建Android开发环境_Android_84

Now right-click the root of our source tree, tweetme, and select Run As > Android Application:

Ubuntu 11.04上搭建Android开发环境_开发环境_86

This will open our emulator and automatically start our app:

Ubuntu 11.04上搭建Android开发环境_休闲_88

 

8 Using A Real Android Device

If you have a real Android smartphone and want to test your app on this device instead of the emulator, you have to first enable USB debugging on the device (Menu > Settings > Applications > Development > USB-Debugging) and then plug it into your Ubuntu desktop using the device's USB cable.

Now check if Ubuntu has correctly identified the device:

adb devices

If you see a lot of question marks like this...

falko@falko-virtual-machine:~$ adb devices
List of devices attached
????????????    no permissions

falko@falko-virtual-machine:~$

... then Ubuntu did not identify your device. In this case create the file /etc/udev/rules.d/51-android.rules...

sudo gedit /etc/udev/rules.d/51-android.rules

... with the following contents:

SUBSYSTEM=="usb", SYSFS{idVendor}=="0bb4", MODE="0666"

Replace 0bb4 (this is for HTC phones) with the correct vendor ID which you can find here: http://developer.android.com/guide/developing/device.html#VendorIds

Then run:

sudo chmod a+r /etc/udev/rules.d/51-android.rules

Plug out your phone and plug it back in, and Ubuntu should now recognize it:

adb devices

falko@falko-virtual-machine:~$ adb devices
List of devices attached
SH0ARPL12791    device

falko@falko-virtual-machine:~$

If you have your app as an apk file, you can now install it onto your phone as follows:

adb -d install -r tweetme-debug.apk

(Please note that I use -d (for device) instad of -e (for emulator) here.)

 

9 Links