项目包含多个android模块时未生成dagger测试组件

时间:2019-03-24 02:15:55

标签: android dagger-2 dagger

<form action="luckysevens.html" onsubmit="return rollDice()">
        <p>Starting Bet:  $ <input type="number" name="startingBet"id="startingBet" /></p>
        <input type="submit" name="play" value="Play" />    
</form>

我有一个包含Android Studio 3.4 kotlin 1.3.0 dagger 2.21 presentation模块的项目。我正在尝试在数据模块中创建测试组件。我可以为演示模块生成组件。我正在使用kotlin-kts构建gradle。

对于演示模块,我有以下data

build.gradle.kts

对于演示模块,我具有以下应用程序:

plugins {
    id("com.android.application")
    kotlin("android")
    kotlin("kapt")
}

android {
    compileSdkVersion(Versions.compileSdkVersion)

    defaultConfig {
        applicationId = "nz.org.westforce.mobileui"
        minSdkVersion(Versions.minSdkVersion)
        targetSdkVersion(Versions.targetSdkVersion)
        versionCode = Versions.versionCode
        versionName = Versions.versionName

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        getByName("release") {
            isMinifyEnabled = true
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
}

dependencies {
    implementation(Libraries.appCompat)
    implementation(Libraries.kotlinStdlib)
    implementation(Libraries.daggerAndroid)
    implementation(Libraries.daggerSupport)
    kapt(Libraries.daggerCompiler)
    kapt(Libraries.daggerProcessor)

    androidTestImplementation(TestLibraries.runner)
    androidTestImplementation(TestLibraries.espressoCore)

    testImplementation(TestLibraries.junit)
    testImplementation(Libraries.daggerAndroid)
    testImplementation(Libraries.daggerSupport)

    kaptTest(Libraries.daggerCompiler)
    kaptTest(Libraries.daggerProcessor)

    implementation(project(":data"))
}

组件

class WestforceCreditUnionMobileuiApplication
    : Application(), HasActivityInjector {

    @Inject
    lateinit var dispatchingAndroidActivityInjector: DispatchingAndroidInjector<Activity>

    override fun onCreate() {
        super.onCreate()

        DaggerWestforceCreditUnionMobileuiComponent.builder()
            .application(this)
            .build()
            .inject(this)
    }

    override fun activityInjector(): AndroidInjector<Activity> =
        dispatchingAndroidActivityInjector
}

以上操作正常,并且生成了@Singleton @Component(modules = [AndroidSupportInjectionModule::class]) interface WestforceCreditUnionMobileuiComponent { @Component.Builder interface Builder { @BindsInstance fun application(application: WestforceCreditUnionMobileuiApplication): Builder fun build(): WestforceCreditUnionMobileuiComponent } fun inject(application: WestforceCreditUnionMobileuiApplication) }

现在在我的DaggerWestforceCreditUnionMobileuiComponent模块中,我可以尝试创建测试组件,但是dagger不会生成测试组件类。

我在data目录中具有以下测试组件

java/test/package/di

在我的测试课中:

@Singleton
@Component(modules = [TestNetworkModule::class])
interface TestWestforceCeditUnionComponent {
    fun inject(webServicesImpTest: WebServicesImpTest)
}

我在这里没有指定模块,因为它会使这里的代码膨胀太多。

对于数据模块中的build.gradle.kts:

class WebServicesImpTest {

    @Inject
    private lateinit var webServicesImp: WebServicesImp

    @Test
    fun setUp() {
        /* the DaggerTestWestforceCreditUnionComponent is not generated */
    }
}

我正在使用以下dagger.android依赖项:

import org.gradle.kotlin.dsl.implementation

plugins {
    id("com.android.library")
    id("kotlin-android")
}

android {
    compileSdkVersion(Versions.compileSdkVersion)

    defaultConfig {
        minSdkVersion(Versions.minSdkVersion)
        targetSdkVersion(Versions.targetSdkVersion)
        versionCode = Versions.versionCode
        versionName = Versions.versionName

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        getByName("release") {
            isMinifyEnabled = true
            proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
        }
    }
}

dependencies {
    implementation(Libraries.kotlinStdlib)
    implementation(Libraries.appCompat)
    implementation(Libraries.daggerAndroid)
    implementation(Libraries.daggerSupport)
    kapt(Libraries.daggerCompiler)
    kapt(Libraries.daggerProcessor)

    testImplementation(TestLibraries.junit)
    testImplementation(TestLibraries.assertJ)
    testImplementation(TestLibraries.mockitoKotlin)

    testImplementation(Libraries.daggerAndroid)
    testImplementation(Libraries.daggerSupport)
    kaptTest(Libraries.daggerCompiler)
    kaptTest(Libraries.daggerProcessor)
}

我尝试重建项目并尝试运行以下任务 const val daggerAndroid = "com.google.dagger:dagger-android:${Versions.daggerAndroidVersion}" const val daggerCompiler = "com.google.dagger:dagger-compiler:${Versions.daggerAndroidVersion}" const val daggerProcessor = "com.google.dagger:dagger-android-processor:${Versions.daggerAndroidVersion}" const val daggerSupport = "com.google.dagger:dagger-android-support:${Versions.daggerAndroidVersion}"

一切都成功构建,

一个人以为我在使用不同的android模块时正在思考,也许数据模块在演示模块中看不到compileDebugUnitTestSources

非常感谢您的任何建议

1 个答案:

答案 0 :(得分:4)

您忘记添加

kotlin("kapt")

plugins模块build.gradle.kts中的:data部分。

然后,Gradle继续处理有关您的@Provides缺少WebServicesImp的错误。另外,您应该修复缺少的Context提供程序,在我的示例中,我只是删除了Context的使用。这应该添加到TestNetworkModule

@Singleton
@Provides
fun provideRetrofit(okHttpClient: OkHttpClient): Retrofit {
    return Retrofit.Builder()
        .baseUrl("http://www.holidaywebservice.com")
        .client(okHttpClient)
        .addConverterFactory(SimpleXmlConverterFactory.createNonStrict(Persister(AnnotationStrategy())))
        .build()
}
@Reusable
@Provides
fun provideWebServices(retrofit: Retrofit): WebServices {
    return retrofit.create(WebServices::class.java)
}

@Reusable
@Provides
fun provideWebServicesImp(webServices : WebServices): WebServicesImp {
    return WebServicesImp(webServices)
}

此后DaggerTestWestforceCeditUnionComponent成功生成。