Gradle 是一个灵活的构建工具,广泛应用于 Android 开发中。它基于 Groovy 或 Kotlin DSL(领域特定语言)编写脚本,能够高效地管理项目的构建、依赖和发布过程。通过配置 build.gradle 文件,可以轻松定义项目的构建流程和依赖关系。
// build.gradle apply plugin: 'com.android.application' android { compileSdkVersion 30 defaultConfig { applicationId "com.example.myapp" minSdkVersion 16 targetSdkVersion 30 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } } dependencies { implementation 'com.android.support:appcompat-v7:30.0.0' implementation 'com.android.support.constraint:constraint-layout:2.0.4' testImplementation 'junit:junit:4.13.2' } • apply plugin: 应用 Android 插件,定义这是一个 Android 应用项目。
• android: 配置 Android 项目的编译选项。
• compileSdkVersion: 指定编译时使用的 Android SDK 版本。
• defaultConfig: 定义应用的默认配置,如应用 ID、最低和目标 SDK 版本、版本号等。
• buildTypes: 定义不同的构建类型,如 release 和 debug。
• dependencies: 声明项目的依赖库。
• ./gradlew build: 构建项目。
• ./gradlew clean: 清理项目。
• ./gradlew assembleDebug: 构建 debug 版本的 APK。
• ./gradlew test: 运行测试。