
今天的越写悦快乐系列文章为大家带来Android开发必备的构建工具Gradle的基本使用和简要说明。犹记得2015年刚上手Android开发的时候还使用的是Maven来构建Android应用程序,使用的开发环境还是Eclipse,转眼间四年过去了,Android的原生开发和现有的跨平台开发融为一体,为构建快速响应的应用提供了不一样的性能,让用户真正感受到多快好省(有没有想要京东的广告🤣
),当然这也是现代软件工程和互联网产品追求的终极目标。
Gradle介绍
Gradle可以帮助开发者更快地构建、自动化和交付软件产品,让你的开发更容易。
支持的集成开发环境
支持从Maven迁移到Gradle
Maven和Gradle的对比
从一个Android项目入手来看Gradle的基本使用

从上图中我们可以获取什么信息呢?
- build.gradle - (项目名称) -
项目的基础配置
- build.gradle - (模块名称) -
App模块配置
- gradle-wrapper.properties -
Gradle版本配置
- proguard-rules.pro -
模块的混淆规则配置
- setting.gradle -
项目的模块声明
- local.properties -
当前电脑的Android SDK及NDK路径配置,一般情况下项目启动会自动识别配置好的路径
接下来我们看看项目的Gradle配置信息是怎么样的:


从buildscript
标签中我们可以看到在项目的Gradle配置中声明了子模块使用的第三方依赖版本,然后就可以在子模块直接引用此处的声明,可以做到统一声明、统一管理和统一更新的目的,也让你的项目配置更加集中化,而后续的allprojects
标签中则声明了远程仓库的地址,最近一个task
标签则表明了清理项目要执行的命令,也就是删除项目目录下build
目录。
声明使用阿里云仓库的
契机
来自于今天下午jcenter
仓库频繁出现502
的错误,不得已才添加阿里云的镜像仓库,为阿里云的镜像仓库点赞,也为中国有这样的互联网公司感到无比自豪和骄傲。
最后我们在子模块中使用项目模块中的第三方依赖库,写法如下:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation project(':treeadapter')
implementation "com.android.support:support-v4:$rootProject.ext.supportLibraryVersion"
implementation "com.android.support:appcompat-v7:$rootProject.ext.supportLibraryVersion"
implementation "com.android.support:cardview-v7:$rootProject.ext.supportLibraryVersion"
implementation "com.android.support:gridlayout-v7:$rootProject.ext.supportLibraryVersion"
implementation "com.android.support:recyclerview-v7:$rootProject.ext.supportLibraryVersion"
implementation "com.android.support:design:$rootProject.ext.supportLibraryVersion"
implementation "com.android.support:support-v13:$rootProject.ext.supportLibraryVersion"
implementation "com.android.support.constraint:constraint-layout:$rootProject.ext.constraintLayoutVerson"
implementation "com.android.support:support-fragment:$rootProject.ext.supportLibraryVersion"
implementation "com.android.support:animated-vector-drawable:$rootProject.ext.supportLibraryVersion"
// MVVMHabit
implementation "com.github.goldze:MVVMHabit:$rootProject.ext.mvvmHabitVersion"
// Pager Bottom Tab Strip
implementation "me.majiajie:pager-bottom-tab-strip:$rootProject.ext.pagerBottomTabStripVersion"
// Binding Collection Adapter
implementation "me.tatarka.bindingcollectionadapter2:bindingcollectionadapter:$rootProject.ext.bindingCollectionAdapterVersion"
implementation "me.tatarka.bindingcollectionadapter2:bindingcollectionadapter-recyclerview:$rootProject.ext.bindingCollectionAdapterVersion"
// Debug DB
implementation "com.amitshekhar.android:debug-db:$rootProject.ext.debugDbVersion"
// Debug Bridge Tool
implementation "com.facebook.stetho:stetho:$rootProject.ext.stethoVersion"
implementation "com.facebook.stetho:stetho-okhttp3:$rootProject.ext.stethoVersion"
implementation "com.facebook.stetho:stetho-js-rhino:$rootProject.ext.stethoVersion"
// Leak Canary
implementation "com.squareup.leakcanary:leakcanary-android:$rootProject.ext.leakCanaryVersion"
implementation "com.squareup.leakcanary:leakcanary-support-fragment:$rootProject.ext.leakCanaryVersion"
// Multi Dex
implementation "com.android.support:multidex:$rootProject.ext.multiDexVersion"
// Smart Table
implementation "com.github.huangyanbin:SmartTable:$rootProject.ext.smartTableVersion"
// Recycler Tree View
implementation "com.github.TellH:RecyclerTreeView:$rootProject.ext.recyclerTreeViewVersion"
// Android PDF Viewer
implementation "com.github.barteksc:android-pdf-viewer:$rootProject.ext.androidPdfViewerVerson"
// Smart Refresh Header
implementation "com.scwang.smartrefresh:SmartRefreshHeader:$rootProject.ext.smartRefreshLayoutVersion"
// Smart Refresh Layout
implementation "com.scwang.smartrefresh:SmartRefreshLayout:$rootProject.ext.smartRefreshLayoutVersion"
// Smart Refresh Footer
implementation "com.scwang.smartrefresh:SmartRefreshLayout:$rootProject.ext.smartRefreshLayoutVersion"
// Status Bar Compat
implementation "com.githang:status-bar-compat:$rootProject.ext.statusBarCompatVersion"
// Room Database
implementation "android.arch.persistence.room:rxjava2:$rootProject.ext.roomDatabaseVersion"
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.ext.roomDatabaseVersion"
// Unit Tests
testImplementation "junit:junit:$rootProject.ext.junitVersion"
testImplementation "org.mockito:mockito-core:$rootProject.ext.mockitoVersion"
// UI Testing
androidTestImplementation "com.android.support.test.espresso:espresso-core:$rootProject.ext.espressoVersion"
androidTestImplementation "com.android.support.test.espresso:espresso-intents:$rootProject.ext.espressoVersion"
androidTestImplementation "org.mockito:mockito-core:$rootProject.ext.mockitoVersion"
}
// Conflict Resolution for Android Support
configurations.all {
resolutionStrategy.eachDependency { DependencyResolveDetails details ->
def requested = details.requested
if (requested.group == 'com.android.support') {
if (!requested.name.startsWith("multidex")) {
details.useVersion '28.0.0'
}
}
}
}
个人感想
很荣幸通过以上知识点的梳理,让我们对Gradle的使用有了一个更加直观的认识,也让Android开发之路更加顺畅,起码我们在开发、构建和运行应用的过程中,对于Logcat中出现的错误有一个更加逻辑性的思考,而不是想不明白为什么会出现这样的错误,当然在使用Gradle构建Android应用的过程中会出现一些莫名其妙的错误,比如拉取第三方依赖库问题,DataBinding的绑定问题,R文件的生成问题,Support库的多版本冲突问题,网络请求的捕获问题,数据监听的异常问题,复杂页面的布局错乱问题,这些林林总总的问题或多或少地影响着我们的工作状态,偶尔会打击我们的信心,我们相信只有夯实基本功,才能在开发的过程中快速准确地定位问题,进而解决问题,我们的目标是提升工作效率,工作不是人生的全部,但是我们希望有更多的时间去陪伴家人和孩子,让我们一起成为更优秀的自己。若是我的文章对你有所启发,那将是我莫大的荣幸。
网友评论