系出名门Android(1) - 在 Windows 下搭建 Android 开发环境,以及 Hello World 程序

转自:http://www.cnblogs.com/webabcd/archive/2010/01/18/1650346.html

介绍
搭建 Android 的开发环境,以及写一个简单的示例程序 
在 Windows 下搭建 Android 开发环境 
Android 项目的目录结构说明
写一个简单的 Hello World 程序  


一、在 Windows 下搭建 Android 开发环境
1、安装 JDK (Java Development Kit)
http://download.java.net/jdk6/

2、安装 Android SDK
http://developer.android.com/sdk

3、安装 Eclipse
http://www.eclipse.org/

4、打开 Eclipse ,并安装其 Android 插件(ADT)
打开菜单 "Help" -> "Install New Software",在 "Availabe Software" 中加入地址 http://dl-ssl.google.com/android/eclipse/ ,然后安装 ADT(Android Development Tools)

5、新建 Android 项目
"New" -> Android Project,Project Name - 项目名称;Build Target - 编译项目的 SDK 版本;Application name - 程序名称;Package name - 包名;Min SDK Version - 程序所支持的最低 SDK 版本代号(2 对应 1.1,3 对应 1.5,4 对应 1.6)

6、运行 Android 项目
打开菜单 "Run" -> "Run Configurations" -> New launch configuration,设置启动项目名称,在 Android 选项卡中选择启动项目,在 Target 选项卡中设置模拟器

7、创建/使用模拟 SD 卡
创建 SD 卡,运行类似如下命令:mksdcard -l sdcard 512M d:/android/sdcard.img
模拟器中使用 SD 卡,在项目配置的 Target 选项卡的 "Additional Emulator Command Line Options" 框中输入类似如下参数:-sdcard d:/android/sdcard.img

8、配置模拟器
运行类似如下命令:android create avd --name android15 --target 2。或者直接在菜单 "Window" -> "Android AVD Manager" 中配置模拟器

9、浏览模拟 SD 卡中的内容
调试程序,在 DDMS 中选择 "File Explorer" ,在其中的 sdcard 目录下就是模拟 SD 卡中的内容

10、查看日志 LogCat
Window -> Show View -> Other -> Android -> LogCat

11、在模拟器中安装/卸载 apk
安装 apk 运行类似如下命令:adb install name.apk;卸载 apk 运行类似如下命令:adb uninstall packagename(注:这里的参数是需要卸载的包名)

12、反编译 Android 程序
解压 apk 文件,取出其中的 classes.dex 文件,运行类似如下命令:dexdump.exe -d classes.dex > dump.txt(其意思是将 classes.dex dump 出来,并将反编译后的代码保存到指定的文本文件中)

13、人品不好是出现的某些错误的解决办法
如果出现类似如下的错误等
no classfiles specified
Conversion to Dalvik format failed with error 1
解决办法:Project -> Clean
出现 Android SDK Content Loader 60% (一直卡在 60%)
解决办法:Project -> 去掉 Build Automatically 前面的勾

14、查看 SDK 源代码
先想办法搞到源代码,如这个地址 http://www.digginmobile.com/android.asp ,然后将其解压到 SDK 根路径下的 sources 文件夹内即可


二、Android 项目的目录结构
1、src - 用于放置源程序
2、gen - 自动生成 R.java 文件,用于引用资源文件(即 res 目录下的数据)
3、assets - 用于放置原始文件,Android 不会对此目录下的文件做任何处理,这是其与 res 目录不同的地方
4、res/drawable - 用于放置图片之类的资源;res/layout - 用于放置布局用的 xml 文件;res/values - 用于放置一些常量数据
5、AndroidManifest.xml - Android 程序的清单文件,相当于配置文件,配置应用程序名称、图标、Activity、Service、Receiver等


三、Hello World 程序
1、res/layout/main.xml
代码
Java代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <!--   
  3. 设置 ID 的方式:ID前加前缀,@+id/   
  4. 引用资源文件内字符串资源的方式:指定的资源名称前加前缀,@string/   
  5. -->   
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  7.     android:orientation="vertical"  
  8.     android:layout_width="fill_parent"  
  9.     android:layout_height="fill_parent"  
  10.     android:id="@+id/layout"  
  11.     >   
  12. <TextView     
  13.     android:layout_width="fill_parent"    
  14.     android:layout_height="wrap_content"    
  15.     android:text="@string/hello"  
  16.     />   
  17. <TextView     
  18.     android:layout_width="fill_parent"    
  19.     android:layout_height="wrap_content"    
  20.     android:id="@+id/txt"  
  21.     />   
  22. </LinearLayout>  
<?xml version="1.0" encoding="utf-8"?>
<!--
设置 ID 的方式:ID前加前缀,@+id/
引用资源文件内字符串资源的方式:指定的资源名称前加前缀,@string/
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/layout"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/txt"
    />
</LinearLayout>
2、res/values/strings.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">layout 直接调用 values 中的字符串</string>
    <string name="hello2">编程方式调用 values 中的字符串</string>
    <string name="app_name">webabcd_hello</string>
</resources>


3、res/drawable 目录下放置一个名为 icon.png 的图片文件

4、AndroidManifest.xml
代码
Java代码 复制代码
  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.webabcd.hello"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">   
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">   
  7.         <activity android:name=".Main"  
  8.                   android:label="@string/app_name">   
  9.             <intent-filter>   
  10.                 <action android:name="android.intent.action.MAIN" />   
  11.                 <category android:name="android.intent.category.LAUNCHER" />   
  12.             </intent-filter>   
  13.         </activity>   
  14.     </application>   
  15.     <uses-sdk android:minSdkVersion="3" />   
  16. </manifest>    
  17.   
  18. 5、Main.java    
  19. 代码    
  20. package com.webabcd.hello;   
  21.   
  22. import android.app.Activity;   
  23. import android.os.Bundle;   
  24. import android.widget.LinearLayout;   
  25. import android.widget.TextView;   
  26.   
  27. public class Main extends Activity {   
  28.     /** Called when the activity is first created. */  
  29.     @Override  
  30.     public void onCreate(Bundle savedInstanceState) {   
  31.         super.onCreate(savedInstanceState);   
  32.            
  33.         // 将指定的布局文件作为 Activity 所显示的内容   
  34.         setContentView(R.layout.main);   
  35.            
  36.         // 动态地在指定的容器控件上添加新的控件   
  37.         TextView txt = new TextView(this);   
  38.         txt.setText("动态添加控件");   
  39.         // setContentView(txt);   
  40.         ((LinearLayout)this.findViewById(R.id.layout)).addView(txt);   
  41.            
  42.         // 引用资源文件内的内容作为输出内容   
  43.         TextView txt1 = (TextView)this.findViewById(R.id.txt);   
  44.         txt1.setText(this.getString(R.string.hello2));   
  45.     }   
  46. }  
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.webabcd.hello"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Main"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-sdk android:minSdkVersion="3" />
</manifest> 

5、Main.java 
代码 
package com.webabcd.hello;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // 将指定的布局文件作为 Activity 所显示的内容
        setContentView(R.layout.main);
        
        // 动态地在指定的容器控件上添加新的控件
        TextView txt = new TextView(this);
        txt.setText("动态添加控件");
        // setContentView(txt);
        ((LinearLayout)this.findViewById(R.id.layout)).addView(txt);
        
        // 引用资源文件内的内容作为输出内容
        TextView txt1 = (TextView)this.findViewById(R.id.txt);
        txt1.setText(this.getString(R.string.hello2));
    }
}

OK
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值