@[toc]android
一辈子清贫怎敢入繁华,俩袖清风怎敢误佳人。
重构书中,有这么一句话:api
好代码,老是要经历多个阶段,从匆忙赶工上线,到慢慢细致打磨,折腾的过程,美好的结果。app
经历过的项目,大部分都是一个 app 包下一应俱全,而今借此机会,从单一模块要逐渐演变,第一步,模块化搞起~模块化
通过瞎折腾后,目前结构以下:工具
Propost
通过一番折腾以后,的确比以前顺眼了许多,随之而来带来的问题是,每一个 module 下都有对应的 build 文件,每一个 build 文件都有一些基本的依赖库,想一想往后还要分离各类 module,相关的管理怎么作?gradle
Step 1:项目根目录下建立 config.gradleui
在此处,首先要明确共有依赖都有哪儿些:spa
So,此处抽取信息以下:code
ext { /** * Android 基本配置项 */ android = [ // 编译 SDK 版本 compileSdkVersion: 29, // Gradle 编译项目工具版本 buildToolsVersion: "29.0.3", // 最低兼容 Android 版本 minSdkVersion : 23, // 最高兼容 Android 版本 targetSdkVersion : 29, // 当前版本编号 versionCode : 1, // 当前版本信息 versionName : "1.0" ] /** * 基础依赖版本 - 相似 support 库等 */ def dependVersion = [ appcompat : "1.1.0", constraintlayout: "1.1.3" ] /** * 经常使用依赖 */ dependencies = [ // basic "kotlinStdlibJdk7": "org.jetbrains.kotlin:kotlin-stdlib-jdk7:${kotlin_version}", "appcompat" : "androidx.appcompat:appcompat:${dependVersion.appcompat}", "coreKtx" : 'androidx.core:core-ktx:1.2.0', "constraintlayout": "androidx.constraintlayout:constraintlayout:${dependVersion.constraintlayout}", // test "junit" : 'junit:junit:4.12', "testJunit" : 'androidx.test.ext:junit:1.1.1', "testEspressoCore": 'androidx.test.espresso:espresso-core:3.2.0' ] }
Step 2:为项目根目录下 build 添加依赖
// Top-level build file where you can add configuration options common to all sub-projects/modules. apply from: "config.gradle" buildscript { // ... } // ...
Step 3:调整 module 中 build.gradle 原有使用方式
// ... android { def androidRoot = rootProject.ext.android compileSdkVersion androidRoot.compileSdkVersion buildToolsVersion androidRoot.buildToolsVersion defaultConfig { applicationId "your package name" minSdkVersion androidRoot.minSdkVersion targetSdkVersion androidRoot.targetSdkVersion versionCode androidRoot.versionCode versionName androidRoot.versionName // ... } // ... } /** * implementation:不会向下传递,仅在当前 module 生效; api:向下传递,所依赖的 module 都可使用 */ dependencies { def androidDependencies = rootProject.ext.dependencies implementation fileTree(dir: 'libs', include: ['*.jar']) implementation androidDependencies.kotlinStdlibJdk7 implementation androidDependencies.appcompat implementation androidDependencies.coreKtx implementation androidDependencies.constraintlayout testImplementation androidDependencies.junit androidTestImplementation androidDependencies.testJunit androidTestImplementation androidDependencies.testEspressoCore // 模块化部分导入部分 // helper implementation project(path: ':helper') // weight implementation project(path: ':weight') // 经常使用三方依赖导入部分 // ... }
ummm,爽了不少。
点滴积累,跟着鸡老大~
万一某天优秀了呢~
哈哈哈