android studio build.gradle,Android Studio开发Android(一)——build.gradle文件详解

这篇博客详细介绍了Android Studio中Gradle构建项目的配置,包括顶级和app目录下的build.gradle文件。主要关注了编译SdkVersion、buildToolsVersion、应用ID、版本信息以及依赖管理。还提到了debug和release构建类型的区别,以及如何配置混淆规则和测试运行器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android Studio采用Gradle构建项目。项目中又两个build.gradle文件,一个在最外层的目录中,一个在app目录下。

最外层目录的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:4.0.0" //构建Android项目,需要引用

// 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

}

app目录下的build.gradle

apply plugin: 'com.android.application' //应用程序模块(直接运行),还有一个值可选com.android.library库模块(依赖别的应用程序模块)

android { //android闭包,配置项目构建的各种属性。

compileSdkVersion 29 //项目的编译版本

buildToolsVersion "30.0.0" //项目构建工具的版本

defaultConfig { //对项目更多细节配置

applicationId "com.et.helloworld" //项目的包名,如果想要修改包名,直接在这修改

minSdkVersion 23 //项目最低兼容的Android系统版本

targetSdkVersion 29 //在目标版本上做过充分的测试,系统为应用程序启用一些最新的功能和特性。

versionCode 1 //项目版本号

versionName "1.0" //项目的版本名。

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" //testInstrumentationRunner表明要使用AndroidJUnitRunner进行单元测试

}

buildTypes { //用于指定生成安装文件的相关配置,两个包,一个是debug,一个是release。debug闭包用于指定生成测试版安装文件的配置。release用于生成正式版安装文件的配置。debug可以忽略不写。

release {

minifyEnabled false //用于指定是否对项目的代码进行混淆

proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

//指定混淆时的规则文件。proguard-android-optimize.txt是Android SDK目录下的。proguard-rules.pro项目根目录下的。

}

}

}

dependencies { //指定当前项目所有的依赖关系。一共又三种依赖方式:本地依赖,库依赖,远程依赖。

implementation fileTree(dir: "libs", include: ["*.jar"]) //本地依赖声明,表示将libs目录下所有.jar后缀的文件都添加到项目构建路径中。

implementation 'androidx.appcompat:appcompat:1.1.0'

implementation 'com.google.android.material:material:1.1.0'

implementation 'androidx.annotation:annotation:1.1.0'

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'

testImplementation 'junit:junit:4.12'

androidTestImplementation 'androidx.test.ext:junit:1.1.1'

androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值