本章介绍App project的基本结构和其常用配置。
一、App Project目录结构
App Project分为两个层次,第一个层次是项目,另一个层次是模块。
AndroidStudio依次选择菜单File→New→New Project即可创建新项目。
另一层次是模块(Module),依次选择菜单File→New→New Module即可在当前项目中创建一个模块,模块依附于项目,每个项目至少有一个模块,也能拥有多个模块。
一般所言的“编译运行App”,指的是运行某个模块,而非运行某个项目,因为模块才对应实际的App。
App项目的目录说明
App项目下面有两个分类:app(代表app模块)、Gradle Scripts。

app下面又有3个子目录,功能说明如下:
manifests子目录 | 存放AndroidManifest.xml,它是App的运行配置文件 |
java子目录 | 存放当前模块的Java源代码 |
res子目录 | 存放当前模块的资源文件 |
GradleScripts下面主要是工程的编译配置文件:
build.gradle | 该文件分为项目级与模块级两种,用于描述App工程的编译规则 |
proguard-rules.pro | 该文件描述了Java代码的混淆规则 |
gradle.properties | 该文件配置了编译工程的命令行参数,一般无须改动 |
settings.gradle | 该文件配置了需要编译哪些模块,以及依赖库的仓库地址 |
local.properties | 它是项目的本地配置文件 |
编译配置文件build.gradle
App项目的build.gradle分为两个级别,一个是Project项目级别的build.gradle,另一个是Module模块级别的build.gradle。如我项目中有3个Module,build.gradle如下图所示:

项目级别的build.gradle指定了当前项目的总体编译规则。
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '7.4.2' apply false
id 'com.android.library' version '7.4.2' apply false
}
模块级别的build.gradle对应于具体模块,每个模块都有自己的build.gradle,它指定了当前模块的详细编译规则。
plugins {
id 'com.android.application'
}
android {
// 指定编译用的SDK版本号。比如31表示使用Android 12编译
compileSdk 31
defaultConfig {
// 指定该模块的应用编号,也就是App的包名
applicationId "com.example.myapplication"
// 指定App适合运行的最小SDK版本号。比如21表示至少要在Android 5.0上运行
minSdk 21
// 指定目标设备的SDK版本号。表示App最希望在哪个版本的Android上运行
targetSdk 31
// 指定App的应用版本号
versionCode 1
// 指定App的应用版本名称
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
// 指定App编译的依赖信息
dependencies {
// 指定引用jar包的路径
implementation fileTree(dir: 'libs', include: ['*.jar'])
// 指定编译Android的高版本支持库。如AppCompatActivity必须指定编译appcompat库
//appcompat库各版本见 https://mvnrepository.com/artifact/androidx.appcompat/appcompat
implementation 'androidx.appcompat:appcompat:1.4.1'
// 指定单元测试编译用的junit版本号
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
gradle文件采用了Gradle工具完成编译构建操作,每个版本的AndroidStudio都有对应的Gradle版本。
运行配置文件AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.MyApplication"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
AndroidManifest.xml指定了App的运行配置信息,它的根节点为manifest。
manifest下面有个application节点,它的各属性说明如下:
android:allowBackup | 是否允许应用备份。为true表示允许,为false表示不允许 |
android:icon | 指定App在手机屏幕上显示的图标 |
android:label | 指定App在手机屏幕上显示的名称 |
android:supportsRtl | 是否支持从右往左的文字排列顺序 |
android:theme | 指定App的显示风格 |
activity | 活动页面的注册声明 |
activity节点的使用说明
application下面有个activity节点,它是活动页面的注册声明。
初始配置的MainActivity正是App的默认主页,如下所示:
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
其中action节点设置的android.intent.action.MAIN表示该页面是App的入口页面,启动App时会最先打开该页面。而category节点设置的android.intent.category.LAUNCHER决定了是否在手机屏幕上显示App图标。
二、XML界面设计&Java代码逻辑
App的界面设计与代码逻辑分开,使用XML文件描述APP界面,可以很方便地在AndroidStudio上预览界面效果,使用java代码书写程序逻辑。
使用XML描绘应用界面
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- 这是个线性布局, match_parent意思是与上级视图保持一致-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- 这是个文本视图,名字叫做textView_hello,显示的文字内容为“Hello World!” -->
<TextView
android:id="@+id/textView_hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
XML标签都由标签头与标签尾组成,标签头以左尖括号包括标签名称,形如“<LinearLayout”、“<TextView”;标签尾在左尖括号后面插入斜杆,以此同标签头区分开,形如“</TextView>”,如果是最末端节点可以使用“/>”这种简写方式,根节点和布局节点不可采取简写方式,因为它们内部需要包裹下级节点。
标签头允许在标签名称后面添加各种属性取值,而标签尾不允许添加任何属性。
注释使用“<!—说明文字 -->”,它的作用是包裹注释性质的说明文字,方便其他开发者理解此处的XML含义。
使用Java写代码逻辑
创建App项目时,除了生成默认的首页布局activity_main.xml之外,还会生成与其对应的代码文件MainActivity.java。
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 当前的页面布局采用的是res/layout/activity_main.xml
setContentView(R.layout.activity_main);
// 获取名叫textView_hello的TextView控件
TextView textView_hello = findViewById(R.id.textView_hello);
// 设置TextView控件的文字内容
textView_hello.setText("测试setText()方法");
}
}
通过findViewById()方法从布局layout文件activity_main.xml中获取textView_hello的TextView控件,再调用TextView控件对象的setText()方法,设置新的文字内容。
运行测试app,显示如下:
