介绍:
安装Android Studio:
配置Android Studio:
创建第一个项目:
创建安卓虚拟机:
真机调试:PC与Android OS通过Android Debug Bridge进行通信
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_8
targetCompatibility 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>
Android使用:XML描绘应用界面,Java代码书写程序逻辑
XML类似于HTML,Java类型与JS
好处:把App的界面设计与代码逻辑分开,进行解耦
默认:
<?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_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>