ARouter(二)Kotlin和Java配置

ARouter(一)简单使用

我们在使用Arouter进行组件化开发时,可能是多人开发有人用Java有人使用Kotlin,在build.gradle配置时有一定的差别,在此记录:

Java版:

if (isModule.toBoolean()) {
    apply plugin: 'com.android.library'
} else {
    apply plugin: 'com.android.application'
}

def cfg = rootProject.ext.configuration
def libs = rootProject.ext.libraries
android {
    ...

    defaultConfig {
       ...
        if (!isModule.toBoolean()) {
            applicationId "module包名"
        }
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [AROUTER_MODULE_NAME: project.getName()]
            }
        }
    }
    sourceSets {
        main {
            if (isModule.toBoolean()) {
                manifest.srcFile 'src/main/AndroidManifest.xml'
                java {
                    exclude 'debug/**'
                }
            } else {
                manifest.srcFile 'src/main/java/debug/AndroidManifest.xml'
            }
        }
    }

   ...
}

dependencies {
   
    implementation libs.arouter
    annotationProcessor libs.arouter_annotation
    implementation project(path: ':common')
}

Kotlin版:

if (isModule.toBoolean()) {
    apply plugin: 'com.android.library'
} else {
    apply plugin: 'com.android.application'
}
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
def cfg = rootProject.ext.configuration
def libs = rootProject.ext.libraries
kapt {
    arguments {
        // ARouter 配置
        arg("AROUTER_MODULE_NAME", project.getName())
    }
}
android {
   ...

    defaultConfig {
      ...
        if (!isModule.toBoolean()) {
            applicationId "module包名"
        }
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    sourceSets {
        main {
            if (isModule.toBoolean()) {
                manifest.srcFile 'src/main/AndroidManifest.xml'
                java {
                    exclude 'debug/**'
                }
            } else {
                manifest.srcFile 'src/main/java/debug/AndroidManifest.xml'
            }
        }
    }

   ...
}

dependencies {
    implementation libs.kotlin
    implementation libs.anko
    implementation libs.arouter
    kapt libs.arouter_annotation
    implementation project(path: ':common')
}

区别在于javaCompileOptions 和kapt,Kotlin中按Java的配置会出现There's no route matched错误!

你可能感兴趣的:(问题收集)