Android 正向开发——HelloWorld 项目剖析
非原创声明:
- Android 正向开发 系列博文,主体内容摘自 《第一行代码》第二版,保留核心内容的基础进行了条理化,并在此基础上加入自己理解。
- 主要目的是自己记录学习过程,并非系列教程。如果你想系统学习,建议移步其他平台。
- 若需转载,请注明出处,并注明原作者。
HelloWorld项目如何运行
首先打开 app/src/main/AndroidManifest.xml
文件,从中可以找到如下代码:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
- 这段代码表示对
MainActivity
活动进行注册,没有再此注册的活动不能使用。 intent-filter
里两行代码非常重要,表示MainActivity
是这个项目的主活动,在手机上点击APP图标,首先启动的就是这个活动。
活动:Android 应用程序的门面,在应用中看到的东西都是放在活动中的。
app\src\main\java\com\example\helloworld_api27\MainActivity.java
代码如下所示
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
- 继承自
AppCompatActivity
,这是一种向下兼容的 Activity
Activity: Android系统提供的一个活动基类,项目中所有的活动都必须继承它或者它的子类才能拥有活动的特性
onCreate()
方法,一个活动被创建时必定要执行的方法
Android程序的设计讲究 逻辑 和 视图分离,不推荐在活动中直接编写界面
- 在布局文件中编写界面
- 在活动中引入进来,如
setContentView(R.layout.activity_main)
给当前的活动引入了 activity_main 布局
打开文件:app\src\main\res\layout\activity_main.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
TextView是Android系统提供的一个控件,用于在布局中显示文字。界面中显示的 字样 通过 android:text="Hello World!"
定义!
项目中的资源
res
目录
drawable开头
存放图片,为兼容不同设备,才会有那么多文件夹,可以自建drawable-xxdpi
等文件夹
理想情况:最好能提供几个不同分辨率的版本,程序运行的时候,会自动选择加载哪个文件夹下的图片。
更多时候:美工只会提供一份图片,这时把所有图片放到 drawable-xxhdpi
即可
mipmap开头
存放应用图标,为兼容不同设备,才会有那么多文件夹
values开头
存放字符串、样式、颜色等配置
strings.xml
文件内容如下:
<resources>
<string name="app_name">HelloWorld-api27</string>
</resources>
两种方式引入字符串:
- 代码中通过
R.string.xxx
- 在XML中通过
@string/xxx
layout
存放布局文件
引用资源例子 AndroidManifest.xml 文件
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
...
</application>
- App图标通过
android:icon
属性指定 - App名称通过
android:label
属性指定
build.gradle
Android Studio 采用 Gradle 来构建项目
- 基于Groovy的领域特定语言(DSL)来声明项目设置
- 摒弃传统基于XML(如Ant和Maven)的各种烦琐配置
最外层的build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
- 两处
repositories
的闭包都声明的jcenter()
这行配置,jcenter 是一个代码托管仓库(有很多Android开源项目),声明后,可以引用任何 jcenter 上的开源项目。google同理? dependencies
闭包中使用classpath
声明了一个 Gradle 插件com.android.tools.build:gradle:3.5.3
来构建 Android 项目,最后面的部分是插件的版本号
Gradle并不是专门为构建 Android 项目而开发,Java、C++ 等很多项目都可以使用 Gradle
通常情况下,并不需要修改这个文件的内容,除非想添加一些全局的项目构建配置。
app目录下的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.helloworld_api27"
minSdkVersion 27
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
-
apply plugin 应用了一个插件,一般可选值:
- 应用程序模块:
com.android.application
:可直接运行 - 库模块:
com.android.library
:只能作为代码库 依附于 别的应用程序模块 来运行
- 应用程序模块:
-
android闭包:配置项目构建的各种属性
-
compileSdkVersion
指定项目 编译版本,29 表示使用 Android 9.0 系统的SDK编译 -
buildToolsVersion
指定项目 构建工具的版本,如果有更新的版本,Android Studio 会进行提示 -
defaultConfig 闭包,对项目的更多细节进行配置
-
applicationId
指定项目的 包名,可以修改 -
minSdkVersion
指定项目最低兼容的 Android 系统版本 -
targetSdkVersion
表示在该目标版本上已经做过了充分的测试,系统将会为你的App启动一些最新的功能和特性 -
versionCode
指定项目的 版本号 -
versionName
指定项目的 版本名 -
testInstrumentationRunner
暂无说明
-
-
buildTypes闭包:指定生成安装文件的相关配置
-
release 闭包:指定生成正式版安装文件的配置
- minifyEnabled:指定是否对 项目的代码 进行混淆
- proguardFiles:指定 混淆时 使用的规则文件
- proguard-android-optimize.txt
在Android SDK 目录下,里边是所有项目通用的混淆规则 - proguard-rules.pro
在当前项目的 根目录 下,编写当前项目特有的混淆规则
- proguard-android-optimize.txt
-
debug 闭包:指定生成测试版安装文件的配置,可忽略不写
注:Android Studio 直接运行项目生成的都是测试版安装文件
-
-
dependencies闭包:指定当前项目所有的依赖关系(本地依赖、库依赖、远程依赖)
-
implementation fileTree :本地依赖
-
implementation : 远程依赖
Gradle在构建项目时,会检查一下本地是否已经有这个库的缓存,没有的话会去自动联网下载 -
testImplementation 声明测试用例库
-
-
Android Studio 项目的依赖关系:
本地依赖 implementation fileTree
对本地 jar 包或者目录添加依赖关系
库依赖
对项目中的库模块添加依赖关系
远程依赖
对 jcenter 库上的开源项目添加依赖关系
格式 域名:组名:版本号
日志工具
Log
分为5种类别的日志:
Log.v()
最为琐碎、意义最小的日志,级别 verbose 最低:Log.d()
调试信息,级别为 debugLog.i()
比较重要的数据,非常想看到分析用户行为数据,级别为 infoLog.w()
警告信息,提示程序在这个地方可能会有潜在的风险,最好去修复一下,级别为 warnLog.e()
错误信息,比如程序进入到了 catch 语句当中。程序出现严重问题,必须尽快修复。级别为 error
在Android Studio 底部 logcat 中 可以看到打印信息,logcat 可以添加过滤器、日志级别控制、关键词过滤
快捷键:
- 在
onCreate()
的外面输入logt
后按 Tab 键,自动生成TAG
常量 logd
后按 Tab 键,自动补全TAG
常量