Skip to content

0、通过远程仓库引入框架

imyyq-pc edited this page Sep 23, 2020 · 7 revisions

LICENSE

快速引入

框架已发布到 jitpack 远程仓库中,按照以下步骤可快速使用框架,以下方式二选一去配置仓库源。

如需引入源码,请参照 Wiki - 通过 submodule 引入框架

(1) 拷贝框架的 dependencies.gradle 文件。(建议)

可以拷贝框架目录下的 dependencies.gradle 文件到你的项目根目录下,然后在你项目根目录的 build.gradle 中引用该 gradle 文件:

buildscript {
    apply from: 'dependencies.gradle'

    addRepository(repositories) // 这样可以使用 dependencies.gradle 声明好的远程仓库源

    dependencies {
        // 根据 AS 的版本而不同
        classpath "com.android.tools.build:gradle:x.x.x"
        // 使用 Kotlin 开发需要 KotlinPlugin
        classpath Deps.kotlinPlugin
    }
}

allprojects {
    addRepository(repositories)
}

addRepository 方法声明了许多的仓库源,包括阿里云的。如果你不喜欢这种方式,那就按下面的去配置。

(2) 不使用 dependencies.gradle 文件的方式。

在你项目根目录的 build.gradle 中,加入 jitpack:

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

可以加入框架了

好了,通过上面二选一的操作,仓库源添加完毕,现在可以在你的模块 build.gradle 中,加入框架:

dependencies {
    implementation 'com.github.imyyq-star:MVVMArch:1.0.6'
}

创建项目时默认添加的以下依赖可以删除了:

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

框架最新的版本号见上面的 jitpack 图标,或 jitpack 网站

模块(比如 app)目录下的 build.gradle 须启用如下功能

// Kotlin 需要增加 kapt plugin 才可以使用 DataBinding
apply plugin: 'kotlin-kapt'

android {

    // Gradle Plugin 4.0 及以上
    buildFeatures {
        dataBinding = true
        viewBinding = true
    }

    // Gradle Plugin 4.0 以下
    dataBinding {
        enabled = true
    }

    viewBinding {
        enabled = true
    }
    
    // 以上配置二选一,其中 viewBinding 是你有不需要用 DataBinding 的类时,才需要开启,不想用 DataBinding,可只继承自 ViewBindingBaseXxxxx 基类

    // 开启 Java 8 支持
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    // Kotlin 开发的话,也要开启 Java8 支持
    kotlinOptions {
        jvmTarget = "${JavaVersion.VERSION_1_8}"
    }

}

远程仓库依然支持功能动态化,即框架对有需要的第三方库进行了二次封装,你不使用的话无需关注,如果要使用就必须自行引入相关的库依赖。举个例子,框架提供了 RoomUtil 用于获取数据库实例,如果你有用到 RoomUtil,就必须加入依赖:

implementation "androidx.room:room-runtime:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"

否则使用 RoomUtil 会报找不到类错误。同理,Retrofit,Glide 也是一样的。

如果你通过上述第一种方式,即 dependencies.gradle 配置的仓库源,这样就可以很方便的配置某库了,比如 LeakCanary:

debugImplementation Deps.leakCanary2

下面列举了使用框架相应模块需要自行引入的库。在 dependencies.gradle 中都有所声明,详细看该文件内容,当然,你手动指定版本也是可以的。

1. 内存泄露检测

推荐用 LeakCanary。

debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.4'
// debugImplementation Deps.leakCanary2

2. 应用前后台监听

要使用 AppStateTracker 类,需引入如下库:

implementation "androidx.lifecycle:lifecycle-process:2.2.0"
// implementation Deps.lifecycleProcess

3. RecyclerView

要使用 com.imyyq.mvvm.binding.viewadapter.recyclerview 包下的类,需引入 RecyclerView

implementation "androidx.recyclerview:recyclerview:1.1.0"
// implementation Deps.recyclerView

4. 使用 Room 数据库和 RoomUtil 类

数据库推荐用 Room,框架提供了 RoomUtil 类,需引入:

implementation "androidx.room:room-runtime:2.2.5"
kapt "androidx.room:room-compiler:2.2.5"
// 要使用 rx,需引入:
implementation "androidx.room:room-rxjava2:2.2.5"

// implementation Deps.roomRuntime
// kapt Deps.roomCompiler
// implementation Deps.roomRxJava2

5. 使用 RxJava 相关的

如果你的项目是 Kotlin 开发的,推荐使用协程,如果你是 Java 开发的,推荐使用 Rx。

框架在 BaseViewModel 的 addSubscribe 等方法和 CommonObserver 类中封装了相关功能,要使用的话需引入 Rx:

implementation "io.reactivex.rxjava2:rxandroid:2.1.1"
implementation "io.reactivex.rxjava2:rxjava:2.2.10"

// implementation Deps.rxAndroid2
// implementation Deps.rxJava2

6. 网络请求

框架对请求的结果和解析封装成了 HttpRequest 类,要使用网络请求,需引入 Retrofit:

implementation "com.squareup.retrofit2:retrofit:2.9.0"
// json 解析
implementation "com.squareup.retrofit2:converter-gson:2.7.2"
// 使用 rx 
implementation "com.squareup.retrofit2:adapter-rxjava2:2.9.0"

// implementation Deps.retrofit2
// implementation Deps.retrofit2RxJava2
// implementation Deps.gson
// implementation Deps.gsonConverter

7. 图片加载

框架对 Glide 二次封装成 DataBinding 可用的,要使用到 ImageView 的 url 和 placeholderRes 等属性,就需引入 Glide:

implementation "com.github.bumptech.glide:glide:4.11.0"
kapt "com.github.bumptech.glide:compiler:4.11.0"

// implementation Deps.glide
// kapt Deps.glideCompiler

8. 权限申请

如果你使用的是 Rx,建议使用 RxPermissions:

implementation 'com.github.tbruyelle:rxpermissions:0.12'

如果使用了 Kotlin 开发,建议使用 livePermissions:

implementation 'com.ftd.livepermissions:livepermissions:1.0.2'
// implementation Deps.livePermissions

9. 内嵌加载中框架

框架封装了 LoadSir 第三方库作为内嵌加载中功能的基础,要使用 BaseViewModel 和 Activity/Fragment 中包含 LoadSir 相关的方法,需引入 LoadSir。

implementation 'com.kingja.loadsir:loadsir:1.3.8'
// implementation Deps.loadSir