代码改变世界

Android的应用程序结构分析:HelloActivity 第二部分【转】

2010-05-19 15:19  Alex77Lee  阅读(288)  评论(0编辑  收藏  举报
第二部分: 编译的中间结果
这个HelloActivity工程经过编译后将生成out/target/common/obj/APPS /HelloActivity_intermediates/目录,这个目录中的内容都是HelloActivity工程相关的,更具体地说都与 development/samples/HelloActivity/中的Android.mk文件相关。
out/target/common/obj/APPS/HelloActivity_intermediates/
|-- classes.dex                       (字节码)
|-- classes.jar                       (JAR文件)
|-- public_resources.xml              (根据resources结构生成的xml)
`-- src
|-- R.stamp
`-- com
`-- example
`-- android
`-- helloactivity
`-- R.java        (resources生成的文件)
classes.dex是一个最重要的文件,它是给Android的JAVA虚拟机Dalvik运行的字节码文件。
classes.jar是一个JAR文件,JAR的含义为Java ARchive,也就是Java 归档,是一种与平台无关的文件格式,可将多个文件合成一个文件。解压缩之后的目录结构:(JAVA标准编译得到的类)
classes
|-- META-INF
|   `-- MANIFEST.MF
`-- com
`-- example
`-- android
`-- helloactivity
|-- HelloActivity.class
|-- R$attr.class
|-- R$id.class
|-- R$layout.class
|-- R$string.class
`-- R.class
各个以class为扩展名的文件,事实上是JAVA程序经过编译后的各个类的字节码。
第三部分: 目标apk文件

目标apk文件是Android的JAVA虚拟机Dalvik安装和运行的文件,事实上这个apk文件将由编译的中间结果和原始文件生成。apk文件的本 质是一个zip包。这个APK包解压缩后的目录结构如下所示:

out/target/product/generic/obj/APPS/HelloActivity_intermediates/package.apk_FILES/
|-- AndroidManifest.xml
|-- META-INF
|   |-- CERT.RSA
|   |-- CERT.SF
|   `-- MANIFEST.MF
|-- classes.dex
|-- res
|   `-- layout
|       `-- hello_activity.xml
`-- resources.arsc

值得注意的是,这里的xml文件经过了处理,和原始的文件不太一样,不能按照文本文件的方式阅读。

第四部分: 源代码的各个文件
Android.mk是整个工程的“Makefile”,其内容如下所示:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := samples
# Only compile source java files in this apk.
LOCAL_SRC_FILES := $(call all-java-files-under, src)
LOCAL_PACKAGE_NAME := HelloActivity
LOCAL_SDK_VERSION := current
include $(BUILD_PACKAGE)
# Use the following include to make our test apk.
include $(call all-makefiles-under,$(LOCAL_PATH))
这个文件在各个Android的工程中都是类似的,其中LOCAL_PACKAGE_NAME表示了这个包的名字。LOCAL_MODULE_TAGS 表示了模块的标,在这里使用的是
samples,正式的应用程序(packages目录中的应用)中多使用eng development。

AndroidManifest.xml是这个HelloActivity工程的描述文件,其内容如下所示:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<manifest xmlns:android=\"http://schemas.android.com/apk/res/android\"
package=\"com.example.android.helloactivity\">
<application android:label=\"Hello, Activity!\">
<activity android:name=\"HelloActivity\">
<intent-filter>
<action android:name=\"android.intent.action.MAIN\"/>
<category android:name=\"android.intent.category.LAUNCHER\"/>
</intent-filter>
</activity>
</application>
</manifest>
其中package用于说明这个包的名称,android:labeapplication中的内容是表示这个应用程序在界面上显示的标 题,activity中的android:name表示这个Android的活动的名称。

文件src/com/example/android/helloactivity/HelloActivity.java是程序主要文件,由JAVA语 言写成
package com.example.android.helloactivity;
import android.app.Activity;               
import android.os.Bundle;                  
public class HelloActivity extends Activity {
public HelloActivity() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.hello_activity);
}
}
com.example.android.helloactivity表示的是这个包的名称,在文件的头部引入了两个包 android.app.Activity是一个Android活动(Activity)包,每一个Android活动都需要继承Activity类。
包android.os.Bundle用于映射字符串的值。
onCreate()是一个重载的函数,在这个函数中实现应用程序创建的所执行的过程。其中setContentView()设置当前的视图 (View)。
设置的方法是使用一个文件,这个文件因此决定了视图中包含的内容。这里使用的是R.layout.hello_activity,表示从 res/layout/目录中使用hello_activity.xml文件。

res/layout/hello_activity.xml文件的内容如下所示:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<EditText xmlns:android=\"http://schemas.android.com/apk/res/android\" android:id=\"@+id/text\"
android:layout_width=\"fill_parent\"
android:layout_height=\"fill_parent\"
android:textSize=\"18sp\"
android:autoText=\"true\"
android:capitalize=\"sentences\"
android:text=\"@string/hello_activity_text_text\" />

其中定义了一个可编辑的文本(EditText),下面的各项其实是它的各种属性,android:text表示这个文本的内 容,string/hello_activity_text_text表示找到相应的文件,也就是res/value/string.xml文件中的 hello_activity_text_text文本。


res/value/string.xml的内容如下所示:
<?xml version=\"1.0\" encoding=\"utf-8\"?>
<resources>
<string name=\"hello_activity_text_text\">Hello, World!</string>
</resources>
hello_activity_text_text文本被res/layout/hello_activity.xml文件引用,正是应用程序运行时在屏 幕显示的文本。 阅读全文
类别:android教程 查看评论