您的位置:知识库 » 手机开发

关于Android开发环境的构建方法总结

作者: 曹亮  发布时间: 2010-03-25 22:29  阅读: 4418 次  推荐: 1   原文链接   [收藏]  

 本方法适用于Android SDK 2.1环境下的程序开发

安装JDK
•    在java.sun.com下载JDK并安装。
•    在“系统属性”的“高级”选项卡中点击“环境变量”,然后添加如下系统环境变量: 

1.  在PATH环境变量后追加 JDK安装路径中的bin路径,本机为:

C:\Program Files\Java\jdk1.6.0_18\bin

2.    新建CLASSPATH环境变量或在CLASSPATH环境变量后追加JDK安装路径中的lib路径和demo路径,本机为:

C:\Program Files\Java\jdk1.6.0_18\demo;C:\Program Files\Java\jdk1.6.0_18\lib

安装Eclipse
•    在eclipse.org下载Eclipse IDE for Java Developers的Windows 32bit版本。
•    下载完成后解压即可使用。

安装Android SDK OR 离线安装
•    在Android Developers下载android-sdk_r05-windows.zip,下载完成后解压到任意路径。
•    运行SDK Setup.exe,点击Available Packages,如果没有出现可安装的包请点击Settings,选中Misc中的"Force https://..."这项,再点击Available Packages 。
•    选择希望安装的SDK及其文档或者其它包,点击Installation Selected、Accept All、Install Accepted,开始下载安装所选包
•    添加SDK安装目录中的tools文件夹路径至系统PATH环境变量,本机为:
C:\Android\android-sdk-windows\tools

关于离线安装 

用上面方法更新的时候速度很慢。

更要等很久。所以我们可以直接把那些包下载下来安装。
地址就是
https://dl-ssl.google.com/androi ... 2.0_r01-windows.zip
https://dl-ssl.google.com/androi ... 2.1_r01-windows.zip
https://dl-ssl.google.com/androi ... -1.1_r1-windows.zip
https://dl-ssl.google.com/androi ... 1.5_r03-windows.zip
https://dl-ssl.google.com/androi ... 1.6_r02-windows.zip
https://dl-ssl.google.com/androi ... ver_r03-windows.zip
https://dl-ssl.google.com/androi ... ogle_apis-6_r01.zip
https://dl-ssl.google.com/androi ... ogle_apis-5_r01.zip
https://dl-ssl.google.com/androi ... ogle_apis-4_r02.zip
https://dl-ssl.google.com/androi ... ogle_apis-7_r01.zip

下完之后,
名字以android的,解压到platforms里面;
以google_apis开头的,解压到addons里面。
usb驱动的,直接解压到根目录。

安装Eclipse插件 ADT
•    Start Eclipse, then select Help > Install New Software.
•    In the Available Software dialog, click Add...
•    In the Add Site dialog that appears, enter a name for the remote site (for example, "Android Plugin") in the "Name" field.
In the "Location" field, enter this URL:
https://dl-ssl.google.com/android/eclipse/
如果无法通过上面的地址获得插件,可将https替换为http。(https is preferred for security reasons)
•    Back in the Available Software view, you should now see "Developer Tools" added to the list.
•    Select the checkbox next to Developer Tools, which will automatically select the nested tools Android DDMS and Android Development Tools. Click Next.
•    In the resulting Install Details dialog, the Android DDMS and Android Development Tools features are listed.
•    Click Next to read and accept the license agreement and install any dependencies, then click Finish.
•    Restart Eclipse. 

配置ADT 

在Eclipse中:
•    选择Window > Preferences...
•    在左边的面板选择Android,然后在右侧点击Browse...并选中SDK路径,本机为:
C:\Android\android-sdk-windows
•    点击Apply、OK。配置完成。 

创建AVD 

为使Android应用程序可以在模拟器上运行,必须创建AVD。
•    在Eclipse中。选择Windows > Android SDK and AVD Manager
•    点击左侧面板的Virtual Devices,在右侧点击New
•    填入Name,选择Target的API,SD Card大小任意,Skin随便选,Hardware目前保持默认值
•    点击Create AVD即可完成创建AVD

Create a New Android Project 

After you've created an AVD, the next step is to start a new Android project in Eclipse.
1.    From Eclipse, select File > New > Project.
If the ADT Plugin for Eclipse has been successfully installed, the resulting dialog should have a folder labeled "Android" which should contain "Android Project". (After you create one or more Android projects, an entry for "Android XML File" will also be available.)
2.    Select "Android Project" and click Next.
3.    Fill in the project details with the following values:
o    Project name: HelloAndroid
o    Application name: Hello, Android
o    Package name: com.example.helloandroid (or your own private namespace)
o    Create Activity: HelloAndroid
o    Min SDK Version: 7
Click Finish.

Here is a description of each field: 

Project Name
This is the Eclipse Project name — the name of the directory that will contain the project files.
Application Name
This is the human-readable title for your application — the name that will appear on the Android device.
Package Name
This is the package namespace (following the same rules as for packages in the Java programming language) that you want all your source code to reside under. This also sets the package name under which the stub Activity will be generated.
Your package name must be unique across all packages installed on the Android system; for this reason, it's very important to use a standard domain-style package for your applications. The example above uses the "com.example" namespace, which is a namespace reserved for example documentation — when you develop your own applications, you should use a namespace that's appropriate to your organization or entity.

Create Activity 

This is the name for the class stub that will be generated by the plugin. This will be a subclass of Android's Activity class. An Activity is simply a class that can run and do work. It can create a UI if it chooses, but it doesn't need to. As the checkbox suggests, this is optional, but an Activity is almost always used as the basis for an application.
Min SDK Version(这个是设置程序希望运行在的系统版本)
==================================
Tips: 这里的Min SDK Version会根据我们选择的Build Target改变,
      表示程序将运行在哪个系统版本之上,对应的数值关系如下:
           Android 1.5:Level API 3
           Android 1.6:Level API 4
           Android 2.01:Level API 6
           Android 2.1:Level API 7
      我们这里选择Android 2.1,故Min SDK Version为7

==================================
This value specifies the minimum API Level required by your application. If the API Level entered here matches the API Level provided by one of the available targets, then that Build Target will be automatically selected (in this case, entering "2" as the API Level will select the Android 1.1 target). With each new version of the Android system image and Android SDK, there have likely been additions or changes made to the APIs. When this occurs, a new API Level is assigned to the system image to regulate which applications are allowed to be run. If an application requires an API Level that is higher than the level supported by the device, then the application will not be installed.
Other fields: The checkbox for "Use default location" allows you to change the location on disk where the project's files will be generated and stored. "Build Target" is the platform target that your application will be compiled against (this should be selected automatically, based on your Min SDK Version).
Notice that the "Build Target" you've selected uses the Android 1.1 platform. This means that your application will be compiled against the Android 1.1 platform library. If you recall, the AVD created above runs on the Android 1.5 platform. These don't have to match; Android applications are forward-compatible, so an application built against the 1.1 platform library will run normally on the 1.5 platform. The reverse is not true.
Your Android project is now ready. It should be visible in the Package Explorer on the left. Open the HelloAndroid.java file, located inside HelloAndroid > src > com.example.helloandroid).
It should look like this:

下面是点完Finish按钮之后自动生成的代码:

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);
}
}

Notice that the class is based on the Activity class. An Activity is a single application entity that is used to perform actions. An application may have many separate activities, but the user interacts with them one at a time. The onCreate() method will be called by the Android system when your Activity starts — it is where you should perform all initialization and UI setup. An activity is not required to have a user interface, but usually will.
Now let's modify some code! 

构建 UI 

Take a look at the revised code below and then make the same changes to your HelloAndroid class. The bold items are lines that have been added.

下面让我们修改一下代码: 

package com.android.helloandroid;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView tv
= new TextView(this);
tv.setText(
"Hello, Android");
setContentView(tv);
}
}
执行代码:Hello,Android 

Eclipse的插件使得你的程序很容易运行。选择Run>Open Run Dialog菜单。(Eclipse3.4版本中,菜单为Run->Run Configurations)可以看到这样的对话框。

下一步,选择“Android Application”,点击在左上角(按钮像一张纸上有个“+”号)或者双击“Android Application”。 有个新的选项“New_configuration”。

将名字改得更形象一点,如“Hello,Android”,然后按Browse按钮选择你的项目,(如果你Eclipse里有多个Android项 目需要打开,确定要选择正确)插件会会自动扫描你项目里的活动子类,然后在“活动”的下拉菜单里加载。如果你的“Hello,Android”项目只有一 个,它将被设置为默认项目,然后你可以继续。

点击“Apply”按钮,这里有个例子:

这样就可以了,点击“Run”按钮,Android的模拟器启动。一启动你的程序就会出现,当一切OK,你可以看到:
这样就可以了,点击“Run”按钮,Android的模拟器启动。
启动完毕后你的程序就会出现,
如果不出现,有时候是因为有键盘锁的原因,
用鼠标将键盘锁解开就一切OK,你可以看到:Hello Android.

使用XML构建UI

你刚刚完成的“Hello, World”的例子使用的是我们称为“可编程”的UI层,意思是你通过编写代码来组建UI层。当你开发了足够多的UI程序,你会发现一些很糟糕的现象: 一些小的变化需要你做大量的代码改动。你常常会忘记将View连接起来,这样会导致一些错误,浪费你很多时间去调试代码。
这就是Android为什么提供一个可变化的UI开发模块:基于XML的文件。最简单解释这个概念就是演示个例子。这里有个XML的文件,它能达到和你刚才完成代码同样的效果:

通常Android里XML文件结构是很简单的。只是一些标记的树形集合,每个标记就是一个视图类。在这个例子中,它就是一个简单的 TextView元素的树,你可以在XML文件里使用任何扩展类的名字作为你的标记,这也包括你在你的自己的代码里定义的名字。这个结构使得你能使用简单 结构和语法快速的组建UI,这种模型就像网站的开发模型,你能够将UI和程序逻辑分开,单独获取或者填充数据。
在这个例子里,有4个XML属性,下面是属性的大概意思:


xmlns:android     这是一个XML命名空间,告诉Android开发工具你准备使用Android命名空间里的一些通用属性。在所有Android XML设计文件中最外层的标记必须使用这个属性。

android:layout_width     这个属性定义了这个视图需要占用的屏幕宽度。在这个例子中,我们仅有的一个视图可以占用整个屏幕,那就是“fill_parent”的意思。

android:layout_height     这个和“layout_width”差不多,表示占用屏幕的高度。

android:text     这个设置文本显示内容,在这个例子里,我们使用“Hello,Android”。

这就是XML的布局,你需要把这个文件放在什么位置? 放在你的工程/res/layout下就可以。“res”是“resource”的简称,这个目录包含了所有应用程序需要的非代码部分。比如图片、字符串、XML文件。

Eclipse插件为你创建了这些XML文件中的一个。在我们上面的范例,我们根本没有使用过它。在包的管理器里,展开目录/res/layout,编辑main.xml文件,替换上面的文本然后保存修改。
在从代码目录里打开 HelloAndroid/gen/com.example.helloandroid/R.java文件,你可以看到他们像这样:

R.java是文件中所有资源的索引界定值定义。你在代码中使用这个类,就像在你的项目里使用一个简洁的方法表示你的资源。在Eclipse这样的IDE工具里,这个方式对于代码自动完成功能还是非常有效的,因为这能让你快速得定位你要寻找的东西。
有个重要点需要注意的是有个内部类“main”,是“layout”的成员类。Eclipse插件提醒你加了一个新的XML文件,然后生成R.java文件,当你加入其他资源到你的工程里,你可以看到R.java在同步更改。

1
0
标签:Android

手机开发热门文章

    手机开发最新文章

      最新新闻

        热门新闻