一、前置知识
1、发展历程
介绍:
- Android之父:Andy Rubin
- Android起源:基于Linux内核
- 所属公司:Google
- 第一部安卓手机:HTC
- 所属设备:手机、平板、车载等
- Android开发语言:JVM语言(Java、Kotlin)
2、配置环境
安装Android Studio:
- 记得勾选Android Virtual Device,然后一直next即可
- 默认安装C盘,如果需要安卓其他盘,请自行更改目录
配置Android Studio:
- 首次打开,选择自定义安装Customer Installer,可以自定义安装目录
- 其他点Next即可,碰到License Agreement,需要手动点击每个license,手动选择Accept
- 然后一直Next,等待下载完成
创建第一个项目:
- New Project —> Phone and Tablet —> Empty Activity(默认kotlin) / Empty Views Activity(默认kotlin,可以修改为Java)
- 根据需要自行修改:
- 项目名、包名、保存位置、SDK版本(API版本-安卓系统版本)、编译配置
创建安卓虚拟机:
- 右侧Device Manager创建虚拟机,选择机器和版本,创建完成点击运行即可
真机调试:PC与Android OS通过Android Debug Bridge进行通信
- 打开手机:开发者选项 —> USB调试
- 插上数据线连接手机和电脑:选择传输类型 - 传输文件/Android Auto
- 根据提示,点击确定允许USB调试
- Android Auto选择真机,点击运行,手机安卓发布的app,即可
3、项目结构
app目录:
- java目录:存放Java源代码
- res目录:存放静态资源(常量定义文件、应用名、布局、图标、主题)
- manifests子目录:存放XML-清单文件,AndroidManifest.xml,是App的运行配置文件
Gradle目录:
proguard-rues.pro
:该文件用于描述Java代码的混淆规则,防止源代码被反编译篡改泄漏build.gradle
:该文件分为项目级与模块级两种,用于描述App工程的编译规则,引用了哪些插件、依赖,打包时,去哪个仓库下载对应的依赖。settings.gradle
:该文件配置了需要编译哪些模块。初始内容为include':app'
,表示只编译app模块,gradle.properties
:该文件用于配置编译工程的命令行参数,一般无须改动。local.properties
:项目的本地配置文件,它在工程编译时自动生成,用于描述开发者电脑的环境配置,包括SDK的本地路径、NDK的本地路径等。
-
build.gradle配置文件注释:
// 应用插件声明,指定项目类型为 Android 应用 plugins {id 'com.android.application' }// Android 配置块,用于配置 Android 项目的构建和属性 android {// 定义项目的命名空间namespace 'com.example.myapplication'// 指定编译的 Android SDK 版本compileSdk 34// 默认配置块,包含应用程序的基本设置defaultConfig {// 应用程序的唯一标识符applicationId "com.example.myapplication"// 最小支持的 Android 版本minSdk 33// 目标 Android 版本targetSdk 34// 应用程序的版本代码,用于区分不同的应用程序版本versionCode 1// 应用程序的版本名称,显示在应用商店等地方versionName "1.0"// 指定测试运行器testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"}// 构建类型配置块,定义不同构建类型的设置buildTypes {release {// 是否启用代码缩小(minification)minifyEnabled false// 指定 Proguard 配置文件,用于代码混淆和优化proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'}}// 编译选项配置块,指定源代码和目标代码的兼容性compileOptions {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8} }// 依赖声明块,指定项目所需的外部库和依赖关系 dependencies {// AndroidX AppCompat 库,提供向后兼容性支持implementation 'androidx.appcompat:appcompat:1.6.1'// Material Design 库,提供现代的 UI 设计元素implementation 'com.google.android.material:material:1.9.0'// ConstraintLayout 库,用于灵活而复杂的布局implementation 'androidx.constraintlayout:constraintlayout:2.1.4'// 单元测试 JUnit 库,用于编写和运行单元测试testImplementation 'junit:junit:4.13.2'// Android 测试扩展库,提供额外的测试功能androidTestImplementation 'androidx.test.ext:junit:1.1.5'// Espresso UI 测试库,用于编写可读性强的 UI 测试androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' }
-
AndroidManifest.xml清单文件注释:
<?xml version="1.0" encoding="utf-8"?> <!-- Android 清单文件,定义应用程序的基本信息和配置 --> <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:roundIcon="@mipmap/ic_launcher_round"<!-- 支持 RTL(Right To Left)文字排列顺序布局 -->android:supportsRtl="true"<!-- 应用程序的主题 -->android:theme="@style/Theme.MyApplication"<!-- 工具属性,指定目标 API 版本为 31 -->tools:targetApi="31"><!-- Activity是一个应用程序组件,提供一个屏幕,用户可以用来交互完成任务--><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>
4、界面显示和逻辑处理
Android使用:XML描绘应用界面,Java代码书写程序逻辑
-
XML类似于HTML,Java类型与JS
-
好处:把App的界面设计与代码逻辑分开,进行解耦
- 使用XML文件描述APP界面,可以很方便地在Android Studio上预览界面效果。
- 一个界面布局可以被多处代码复用,反过来,一个Java代码也可能适配多个界面布局。
-
默认:
<?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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>