Android的应用程序结构分析:HelloActivity

本文以HelloActivity为例,详细解析Android应用程序结构,涵盖源代码、编译中间结果、目标APK文件等内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[原创]Android的应用程序结构分析:HelloActivity

(hanchao3c Android开发者论坛原创,转载请注明)
本例以一个简单的HelloActivity程序为例,简单介绍Android应用程序的源代码结构。事实上,Android应用程序虽然不是很复杂,但 是通常涉及了JAVA程序,XML文件,Makefile多方面的内容。HelloActivity虽然简单,但是麻雀虽小,五脏俱全,是 学习Android应用程序的最好示例。

第一部分:HelloActivity的源代码
HelloActivity工程的源代码在Android目录的development/samples/HelloActivity/中,代码的结构如下所示:
development/samples/HelloActivity/
|-- Android.mk
|-- AndroidManifest.xml
|-- res
|   |-- layout
|   |   `-- hello_activity.xml
|   `-- values
|       `-- strings.xml
|-- src
|   `-- com
|       `-- example
|           `-- android
|               `-- helloactivity
|                   `-- HelloActivity.java
`-- tests
    |-- Android.mk
    |-- AndroidManifest.xml
    `-- src
        `-- com
            `-- android
                `-- helloactivity
                    `-- HelloActivityTest.java

其中tests是一个独立的项目,可以暂时不考虑。其他部分看作一个Android的一应用程序的工程。这个工程主要的组成部分如下所示:
AndroidManifest.xml:工程的描述文件,在运行时有用处
Android.mk:整个工程的Makefile
res:放置资源文件的目录
src/com/example/android/helloactivity/HelloActivity.java:这是JAVA类文件,这个文件的路径表示在Andorid的JAVA包的结构中的位置,这个包的使用方式为com.example.android.helloactivity。

第二部分: 编译的中间结果
这个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文件引用,正是应用程序运行时在屏幕显示的文本。
List of Sample Apps The list below provides a summary of the sample applications that are available with the Android SDK. Using the links on this page, you can view the source files of the sample applications in your browser. You can also download the source of these samples into your SDK, then modify and reuse it as you need. For more information, see Getting the Samples. API Demos A variety of small applications that demonstrate an extensive collection of framework topics. Backup and Restore A simple example that illustrates a few different ways for an application to implement support for the Android data backup and restore mechanism. Bluetooth Chat An application for two-way text messaging over Bluetooth. BusinessCard An application that demonstrates how to launch the built-in contact picker from within an activity. This sample also uses reflection to ensure that the correct version of the contacts API is used, depending on which API level the application is running under. Contact Manager An application that demonstrates how to query the system contacts provider using the ContactsContract API, as well as insert contacts into a specific account. Home A home screen replacement application. JetBoy A game that demonstrates the SONiVOX JET interactive music technology, with JetPlayer. Live Wallpaper An application that demonstrates how to create a live wallpaper and bundle it in an application that users can install on their devices. Lunar Lander A classic Lunar Lander game. Multiple Resolutions A sample application that shows how to use resource directory qualifiers to provide different resources for different screen configurations. Note Pad An application for saving notes. Similar (but not identical) to the Notepad tutorial. SampleSyncAdapter Demonstrates how an application can communicate with a cloud-based service and synchronize its data with data stored locally in a content provider. The sample uses two related parts of the Android framework — the account manager and the synchronization manager (through a sync adapter). Searchable Dictionary A sample application that demonstrates Android's search framework, including how to provide search suggestions for Quick Search Box. Snake An implementation of the classic game "Snake." Soft Keyboard An example of writing an input method for a software keyboard. Spinner A simple application that serves as an application-under-test for the SpinnerTest sample application. SpinnerTest An example test application that contains test cases run against the Spinner sample application. To learn more about the application and how to run it, please read the Activity Testing tutorial. TicTacToeLib An example of an Android library project that provides a game-play Activity to any dependent application project. For an example of how an application can use the code and resources in an Android library project, see the TicTacToeMain sample application. TicTacToeMain An example of an Android application that makes use of code and resources provided in an Android library project. Specifically, this application uses code and resources provided in the TicTacToeLib library project. Wiktionary An example of creating interactive widgets for display on the Android home screen. Wiktionary (Simplified) A simple Android home screen widgets example.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值