kotlin的组件化与mvvm

一、项目结构

项目使用kotlin语音,整体框架为:MVVM + 组件化 + ARouter + Retrofit + RxJava + AAC。如下图:
kotlin的组件化与mvvm_第1张图片

app

主工程,依赖该项目下的所有module。

common_lib

项目中依赖的库,以及所有的工具类。

common_res

项目中公共的资源文件,减轻common_lib负担。common_lib中依赖该res

module_login

登录模块,项目中有多个模块,这里就不创建那么多了。

二、项目配置

工程的build.gradle
buildscript {
	// 引用自己的build.gradle文件,主要用于管理各个依赖库的版本。
    apply from:"config.gradle"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath deps.gradle.build
        classpath deps.gradle.kotlin_gradle_plugin
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()
        maven { url "https://jitpack.io" }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

config.gradle

ext.deps = [:]
def version = [:]
// dataBinding
version.dataBinding = '3.0.1'
// Glide
version.glide = '4.8.0'
// Kotlin
version.kotlin = '1.3.11'
// Support
version.support = '27.1.1'
version.constraint_layout = '1.1.3'
// Retrofit
version.retrofit = '2.4.0'
version.adapter_rxjava2 = '2.4.0'
version.converter_gson = '2.4.0'
// RxJava
version.rxkotlin = '2.3.0'
version.rxandroid = '2.1.0'
// ARouter
version.arouter_api = '1.4.0'
version.arouter_compiler = '1.2.1'
// EventBus
version.event_bus = '3.1.1'
// Test
version.runner = '1.0.2'
version.espresso_core = '3.0.2'
version.junit = '4.12'
// applicationId
version.mainApplicationId = 'com.android.app'
version.loginModuleApplicationId = 'com.android.login'
// Config
version.compileSdkVersion = 27
version.minSdkVersion = 15
version.targetSdkVersion = 27
version.versionCode = 1
version.versionName = "1.0"
version.gradle = '3.0.1'
ext.deps.version=version

def arouter=[:]
arouter.api="com.alibaba:arouter-api:$version.arouter_api"
arouter.arouter_compiler="com.alibaba:arouter-compiler:$version.arouter_compiler"
ext.deps.arouter=arouter

def dataBinding=[:]
dataBinding.data_compiler="com.android.databinding:compiler:$version.dataBinding"
ext.deps.dataBinding=dataBinding

def gradle=[:]
gradle.build="com.android.tools.build:gradle:$version.gradle"
gradle.kotlin_gradle_plugin="org.jetbrains.kotlin:kotlin-gradle-plugin:$version.kotlin"
ext.deps.gradle=gradle

def glide = [:]
glide.runtime = "com.github.bumptech.glide:glide:$version.glide"
glide.compiler = "com.github.bumptech.glide:compiler:$version.glide"
ext.deps.glide=glide

def kotlin = [:]
kotlin.kotlin_stdlib_jdk = "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$version.kotlin"
ext.deps.kotlin=kotlin

def support = [:]
support.appcompat = "com.android.support:appcompat-v7:$version.support"
ext.deps.support=support

def retrofit = [:]
retrofit.runtime = "com.squareup.retrofit2:retrofit:$version.retrofit"
retrofit.adapter_rxjava2 = "com.squareup.retrofit2:adapter-rxjava2:$version.adapter_rxjava2"
retrofit.converter_gson = "com.squareup.retrofit2:converter-gson:$version.converter_gson"
ext.deps.retrofit=retrofit

def rxjava = [:]
rxjava.rxkotlin = "io.reactivex.rxjava2:rxkotlin:$version.rxkotlin"
rxjava.rxandroid = "io.reactivex.rxjava2:rxandroid:$version.rxandroid"
ext.deps.rxjava=rxjava

def test=[:]
test.junit="junit:junit:$version.junit"
test.runner="com.android.support.test:runner:$version.runner"
test.espresso_core="com.android.support.test.espresso:espresso-core:$version.espresso_core"
ext.deps.test=test

app的build.gradle

apply plugin: 'com.android.application'
// kotlin 依赖
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {

    compileSdkVersion deps.version.compileSdkVersion

    defaultConfig {
        applicationId deps.version.mainApplicationId
        minSdkVersion deps.version.minSdkVersion
        targetSdkVersion deps.version.targetSdkVersion
        versionCode deps.version.versionCode
        versionName deps.version.versionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kapt {
        generateStubs = true
    }
   // 使用DataBinding
    dataBinding{
        enabled = true
    }
}

dependencies {
    testImplementation deps.test.junit
    androidTestImplementation deps.test.runner
    androidTestImplementation deps.test.espresso_core

    // DataBinding
    kapt deps.dataBinding.data_compiler

    implementation project(':common_lib')
    // isRunAlone 在gradle.properties中声明 isRunAlone=false
    if (!isRunAlone.toBoolean()){
        implementation project(':module_login')
    }
}

common_lib的build.gradle

apply plugin: 'com.android.library'
// kotlin
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {

    compileSdkVersion deps.version.compileSdkVersion

    defaultConfig {
        minSdkVersion deps.version.minSdkVersion
        targetSdkVersion deps.version.targetSdkVersion
        versionCode deps.version.versionCode
        versionName deps.version.versionName
        multiDexEnabled true
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    kapt {
        arguments {
            arg("AROUTER_MODULE_NAME", project.getName())
        }
        generateStubs = true
    }

    lintOptions {
        abortOnError false
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    dataBinding {
        enabled = true
    }
}

dependencies {
    api fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation deps.test.junit
    androidTestImplementation deps.test.runner
    androidTestImplementation deps.test.espresso_core

    implementation project(':common_res')

    api deps.support.appcompat

    api deps.glide.runtime
    kapt deps.glide.compiler

    api deps.kotlin.kotlin_stdlib_jdk

    api deps.retrofit.runtime
    api deps.retrofit.adapter_rxjava2
    api deps.retrofit.converter_gson

    api deps.rxjava.rxkotlin
    api deps.rxjava.rxandroid

    api deps.arouter.api
    kapt deps.arouter.arouter_compiler

    api 'com.android.support:multidex:1.0.3'

    // DataBinding
    kapt deps.dataBinding.data_compiler
}

module_login的build.gradle

// 如果是用组件运行的话,依赖则需要使用application。默认为library
 if (isRunAlone.toBoolean()) {
     apply plugin: 'com.android.application'
 }else{
     apply plugin: 'com.android.library'
 }

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {

    compileSdkVersion deps.version.compileSdkVersion

    defaultConfig {
        if (isRunAlone.toBoolean()) {
            applicationId deps.version.loginModuleApplicationId
        }
        minSdkVersion deps.version.minSdkVersion
        targetSdkVersion deps.version.targetSdkVersion
        versionCode deps.version.versionCode
        versionName deps.version.versionName
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
	 // 以组件化运行的时候,需要添加AndroidManifest.xml文件
     sourceSets {
         main {
             if (isRunAlone.toBoolean()) {
                 manifest.srcFile 'src/main/module/AndroidManifest.xml'
             } else {
                 manifest.srcFile 'src/main/AndroidManifest.xml'
             }
         }
     }

     kapt {
         arguments {
             arg("AROUTER_MODULE_NAME", project.getName())
         }
         generateStubs = true
     }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

     dataBinding{
         enabled = true
     }
}

dependencies {
    androidTestImplementation deps.test.runner
    androidTestImplementation deps.test.espresso_core
    testImplementation deps.test.junit

    implementation project(':common_lib')
    kapt deps.arouter.arouter_compiler

    // DataBinding
    kapt deps.dataBinding.data_compiler
}

三、注意事项

1.关于Arouter

所有module(依赖common_lib)的build.gradle文件中都必须添加:

android{
	...
	kapt {
	       arguments {
	           arg("AROUTER_MODULE_NAME", project.getName())
	       }
	       generateStubs = true
	   }
  }
dependencies {
	kapt deps.arouter.arouter_compiler
}

2.关于DataBinding
在app module 和library module 中都要设置

android{
	dataBinding{
	        enabled = true
	}
}	 

depencies{
	// DataBinding
    kapt deps.dataBinding.data_compiler
}

3.关于App中的build.gradle
如果在App module中是以runtimeOnle的方式依赖library module的话,编译能通过,但是在实际运行的时候会出现DataBinding类找不到的错误,原因是dataBinding库,通过apt生成了App中唯一的一个android.databinding.DataBinderMapperImpl 类,而runtimeOnly 的方式,会导致这个类中,生成的对应关系,缺少了library module 中部分。

你可能感兴趣的:(android,MVVM,kotlin,ARouter)