安卓

项目引用Kotlin与databinding,ARouter,A

2019-01-09  本文已影响45人  Magic旭

个人项目的整体结构

project.png
推荐依赖API的做法

ext {

    //true 每个业务Module可以单独开发
    //false 每个业务Module以lib的方式运行
    //修改之后需要Sync方可生效
    isModule = false

    compileSdkVersion = 26
    minSdkVersion = 15
    targetSdkVersion = 26
    androidSupportLibraryVersion = '26.1.0'

    butterknifeVersion = '8.1.0'

    versions = [
            arouterApiVersion       : "1.4.1",
            arouterCompilerVersion  : "1.2.2",
            arouterannotationVersion: "1.0.4",
            arouterRegisterVersion  : "1.0.2",
            archLifecycleVersion    : "1.0.0",
            archRoomVersion         : "1.0.0",
    ]

    support = [
            'v4' : "com.android.support:support-v4:$androidSupportLibraryVersion",
            'annotations'           : "com.android.support:support-annotations:$androidSupportLibraryVersion",
            'compat'                : "com.android.support:support-compat:$androidSupportLibraryVersion",
            'appcompat'             : "com.android.support:appcompat-v7:$androidSupportLibraryVersion",
            'gridlayout'            : "com.android.support:gridlayout-v7:$androidSupportLibraryVersion",
            'recyclerview'          : "com.android.support:recyclerview-v7:$androidSupportLibraryVersion",
            'design'                : "com.android.support:design:$androidSupportLibraryVersion",
            'cardview'              : "com.android.support:cardview-v7:$androidSupportLibraryVersion",
            'preference'            : "com.android.support:preference-v7:$androidSupportLibraryVersion",
            'multidex'              : 'com.android.support:multidex:1.0.3',
            'constraintlayout'      : 'com.android.support.constraint:constraint-layout:1.0.2',
            'exifinterface'         : "com.android.support:exifinterface:$androidSupportLibraryVersion",
            'testrunner'            : 'com.android.support.test:runner:1.0.2',
            'room'                  : "android.arch.persistence.room:runtime:1.0.0",
            'core'                  : "com.android.support:support-core-utils:$androidSupportLibraryVersion",
            'lifecycle'             : "android.arch.lifecycle:extensions:${versions["archLifecycleVersion"]}",
            'lifecycle_compile'     : "android.arch.lifecycle:compiler:${versions["archLifecycleVersion"]}",
            'room'                  : "android.arch.persistence.room:runtime:${versions["archRoomVersion"]}",
            'room_compile'          : "android.arch.persistence.room:compiler:${versions["archRoomVersion"]}"

    ]

    third = [
            'arouter_api'           : "com.alibaba:arouter-api:${versions["arouterApiVersion"]}",
            'arouter_compiler'      : "com.alibaba:arouter-compiler:${versions["arouterCompilerVersion"]}",
            'arouter_annotation'    : "com.alibaba:arouter-annotation:${versions["arouterannotationVersion"]}",
            'arouter-register'      : "com.alibaba:arouter-register:${versions["arouterRegisterVersion"]}",
            'butterknife'           : "com.jakewharton:butterknife:$butterknifeVersion",
            'butterknife_compiler'  : "com.jakewharton:butterknife-compiler:$butterknifeVersion",

    ]
}

apply from:"dependencies.gradle"
buildscript {
    ext.kotlin_version = '1.2.71'

    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven{ url 'https://maven.aliyun.com/repository/google' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

allprojects {
    repositories {
        maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/' }
        maven{ url 'https://maven.aliyun.com/repository/google' }
    }
}

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

问题场景

原本java的项目引入Kotlin作为开发语言,在模块还没引用kotlin之前是采用databinding和ARouter进行ui绑定和模块界面跳转的,但是引入Kotlin后出现问题1:databinding冲突;问题2:ARouter提示跳转路径找不到;问题3:Android Architecture Components提示找不到ViewModelProviders

解决办法

问题一,与databinding冲突解决方法

在自己的模块的gradle文件进行配置,我是在module_main模块中引入Kotlin进行开发的,所以我需要在app(module_main)build.gradle文件中添加一下配置信息有

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
      ………………………………
    }

    buildTypes {
        ………………………………
    }

    dataBinding {
        enabled true
    }

}

dependencies {
   ………………………………
    kapt "com.android.databinding:compiler:3.1.0"
}

kapt {
    generateStubs = true
}

repositories {
    mavenCentral()
}
问题二,ARouter提示找不到跳转路径的解决办法

这个仅使用与你项目本来是可以用ARouter来进行跳转的,在跳转到Kotlin的Activity时候提示找不到路径的场景。

  1. 原本模块在纯java模块下的ARouter的gradle配置信息如下
android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    defaultConfig {
        //rootProject.ext是依赖与dependencies.gradle定义的属性
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        //Arouter路由配置(每个Module都需要添加,要不然无法跳转)
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [ moduleName : project.getName() ]
            }
            annotationProcessorOptions {
                arguments = [AROUTER_MODULE_NAME: project.getName()]
            }
        }
    }

    buildTypes {
        release {
            ………………
        }
        debug {
           ………………
        }
    }
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
     …………………………
    //Arouter路由,因为common模块(通用模块)引用了arouter_api,所以该模块不需要再引用,这里要自己区分api与implementation
//    implementation rootProject.ext.third["arouter_api"]
    implementation project(':common')
    annotationProcessor rootProject.ext.third["arouter_compiler"]
}
  1. 模块添加Kotlin代码后,在没有修改gradle配置信息情况下,导致ARouter提示找不到相关的UI的路径。原因是因为Kotlin的插件与Java不同,Java不支持Kotlin的代码,但是Kotlin的插件却完美支持Kotlin与Java代码。下面我们需要修改gradle信息如下
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    defaultConfig {
        ……………………
        //Arouter路由配置(每个Module都需要添加,要不然无法跳转)
//        javaCompileOptions {
//            annotationProcessorOptions {
//                arguments = [ moduleName : project.getName() ]
//            }
//            annotationProcessorOptions {
//                arguments = [AROUTER_MODULE_NAME: project.getName()]
//            }
//        }
        //如果存在kotlin和java代码必须配置下面的代码
        kapt {
            arguments {
                arg("moduleName", project.getName())
                arg("AROUTER_MODULE_NAME",project.getName())
            }
            generateStubs = true
        }
    }

    buildTypes {
       …………
    }

    dataBinding {
        enabled true
    }

}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
   …………………………
    //Arouter路由,因为common模块引用了arouter_api,所以该模块不需要再引用
//    implementation rootProject.ext.third["arouter_api"]
    implementation project(':common')
//    annotationProcessor rootProject.ext.third["arouter_compiler"]
    kapt rootProject.ext.third["arouter_compiler"]
   ……………………
    kapt "com.android.databinding:compiler:3.1.0"
}

kapt {
    generateStubs = true
}

repositories {
    mavenCentral()
}

这里需要弄清楚kapt与annotationProcessor区别可以查看KAPT(Annotation Processing for Kotlin)是什么

问题三,ViewModelProviders提示找不到

google新推出MVVM+Android Architecture Components,优势在于不用管理Activity与Fragment的生命周期,当数据更新时候会自动通知到ui上,当然它这个实现还是基于观察者模式的,订阅-发布过程。作为一名优(懒)秀(惰)程序员当然要引入自己项目中啦。

  1. gradle的配置信息
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
    …………………………
    }
    buildTypes {
       ………………
    }
    dataBinding {
        enabled true
    }
}

dependencies {
    ……………………
    //liveData 如果有kotlin代码,annotationProcessor必须用kapt代替,要不功能无法正常使用m
    implementation rootProject.ext.support["lifecycle"]
    annotationProcessor rootProject.ext.support["lifecycle_compile"]
    implementation rootProject.ext.support["room"]
    annotationProcessor rootProject.ext.support["room_compile"]
    ……………………
}
kapt {
    generateStubs = true
}
repositories {
    mavenCentral()
}

问题来了,javaUI跳转到Kotlin写的UI上App闪退,项目报错信息:Failed resolution of: Landroid/arch/lifecycle/ViewModelProviders,google了很多方法之后都没用,突然想到ARouter的解决方法,有点共同性,灵机一动,我就把上面的annotationProcessor替换成了kapt,然后项目就成功运行了。真是机(傻)智(逼)如(shi)我(wo)。其实核心问题就是没弄懂annotationProcessor的作用。想学习如何使用Android Architecture Components的童鞋可以去github的官方demo学习,链接如下android-lifecycles

希望能帮到大家增加对android的热爱,fighting……

上一篇下一篇

猜你喜欢

热点阅读