2

I have a test-utils module which contains testImplementation, androidTestImplementation and some rules and file reading extension code such as

fun getResourceAsText(path: String): String {
    return object {}.javaClass.classLoader!!.getResource(path)!!.readText()
}

inline fun <reified T> convertToObjectsFromString(input: String): T? {
    return Gson().fromJsonWithType<T>(input)
}

inline fun <reified T> Gson.fromJsonWithType(json: String): T? =
    fromJson<T>(json, object : TypeToken<T>() {}.type)

build.gradle.kts file test-utils module

import extension.addInstrumentationTestDependencies
import extension.addUnitTestDependencies

plugins {
    id(Plugins.ANDROID_LIBRARY_PLUGIN)
    id(Plugins.KOTLIN_ANDROID_PLUGIN)
    id(Plugins.KOTLIN_ANDROID_EXTENSIONS_PLUGIN)
    id(Plugins.KOTLIN_KAPT_PLUGIN)
}

android {

    compileSdkVersion(AndroidVersion.COMPILE_SDK_VERSION)
    defaultConfig {
        minSdkVersion(AndroidVersion.MIN_SDK_VERSION)
        targetSdkVersion(AndroidVersion.TARGET_SDK_VERSION)
        versionCode = AndroidVersion.VERSION_CODE
        versionName = AndroidVersion.VERSION_NAME
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }


    sourceSets {

        val sharedTestDir = "src/test-shared/java"

        getByName("test") {
            java.srcDir(sharedTestDir)
        }

        getByName("androidTest") {
            java.srcDir(sharedTestDir)
            resources.srcDir("src/test/resources")
        }
    }

    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }

}

dependencies {

    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))

    implementation(Deps.KOTLIN)
    implementation(Deps.ANDROIDX_CORE_KTX)

    addUnitTestDependencies()

    addInstrumentationTestDependencies()
}

I tried adding test-utils module using implementation(project(":test-utils")) and testImplementation(project(":test-utils"))

but when i run test from another module, for instance data module i get

Unresolved reference: convertToObjectsFromString

or simple string for folder

Unresolved reference: RESPONSE_JSON_PATH

which is const val RESPONSE_JSON_PATH = "response.json"

even though imports on this test class is not red and cmd + click is pointing the exact location.

Thracian
  • 6,939
  • 2
  • 33
  • 64
  • did you find a solution for this? – amp Nov 10 '20 at 17:58
  • @amp, yes i did. You can check it out in [this project](https://github.com/SmartToolFactory/PropertyFindAR/tree/develop/libraries). It works well with every module including dynamic feature modules. – Thracian Nov 10 '20 at 19:05

0 Answers0