dependency-analysis-gradle-plugin icon indicating copy to clipboard operation
dependency-analysis-gradle-plugin copied to clipboard

Incorrect advice for testFixtures configuration in android project

Open zepurplez opened this issue 11 months ago • 1 comments
trafficstars

Plugin version 2.5.0

Gradle version 8.11

JDK version 17

(Optional) Kotlin and Kotlin Gradle Plugin (KGP) version 1.9.24

(Optional) Android Gradle Plugin (AGP) version 8.5.2

(Optional) reason output for bugs relating to incorrect advice

------------------------------------------------------------
You asked about the dependency ':kt-lib'.
You have been advised to remove this dependency from 'testFixturesImplementation'.
------------------------------------------------------------

Shortest path from :lib to :kt-lib for debugCompileClasspath:
:lib
\--- :kt-lib

Shortest path from :lib to :kt-lib for debugRuntimeClasspath:
:lib
\--- :kt-lib

Shortest path from :lib to :kt-lib for debugUnitTestCompileClasspath:
:lib
\--- :kt-lib

Shortest path from :lib to :kt-lib for debugUnitTestRuntimeClasspath:
:lib
\--- :kt-lib

Shortest path from :lib to :kt-lib for debugAndroidTestCompileClasspath:
:lib
\--- :kt-lib

Shortest path from :lib to :kt-lib for debugAndroidTestRuntimeClasspath:
:lib
\--- :kt-lib

Shortest path from :lib to :kt-lib for releaseCompileClasspath:
:lib
\--- :kt-lib

Shortest path from :lib to :kt-lib for releaseRuntimeClasspath:
:lib
\--- :kt-lib

Shortest path from :lib to :kt-lib for releaseUnitTestCompileClasspath:
:lib
\--- :kt-lib

Shortest path from :lib to :kt-lib for releaseUnitTestRuntimeClasspath:
:lib
\--- :kt-lib

Source: debug, main
-------------------
* Uses 1 class: com.example.utils.SomeUtils (implies implementation).

Source: release, main
---------------------
* Uses 1 class: com.example.utils.SomeUtils (implies implementation).

Source: debug, test
-------------------
(no usages)

Source: release, test
---------------------
(no usages)

Source: debug, android_test
---------------------------
(no usages)

Describe the bug So i have two modules, the first one is just pure kotlin with one util class

object SomeUtils {
    fun blabla() = println("Hey")
}

the second is android module with enabled testFixtures option and android.experimental.enableTestFixturesKotlinSupport=true

build.gradle

plugins {
    alias(libs.plugins.android.library)
    alias(libs.plugins.kotlin.android)
    id("com.autonomousapps.dependency-analysis") version "2.5.0"
}

android {
    namespace = "com.example.lib"
    compileSdk = 34
    testFixtures.enable = true

    defaultConfig {
        minSdk = 26
    }

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
}

dependencies {
    implementation(project(":kt-lib"))
    testFixturesImplementation(project(":kt-lib"))
}

one class in src/main

import com.example.utils.SomeUtils

interface SomeClass {
    fun yo()

    class Impl : SomeClass {
        override fun yo() = SomeUtils.blabla()
    }
}

and stub implementation in src/testFixtures

import com.example.utils.SomeUtils

class StubSomeClass : SomeClass {

    override fun yo() {
        SomeUtils.blabla()
    }
}

When I run projectHealth task I get the following message

Unused dependencies which should be removed:
  testFixturesImplementation(project(":kt-lib"))

If you follow the advice and remove the dependency, code in src/testFixtures won't compile

zepurplez avatar Nov 26 '24 05:11 zepurplez

Thanks for the report.

autonomousapps avatar Dec 02 '24 23:12 autonomousapps

The issue still occurs in version 2.19. I've prepared small project to reproduce it.

You just have to execute

./gradlew :app:projectHealth

Plugin still advices to remove dependency in testFixture configuration.

TestFixturesReproducer.zip

zepurplez avatar Jun 27 '25 07:06 zepurplez

The problem arises because you use testFixtures in application module. If you convert app to use com.android.library no advice is produced, as expected.

The fix for this is simple, although I don't think there is any use case to have testFixtures in application module.

Anyway, I created a PR to fix this. The decision whether to merge it is up to the maintainer.

Mexator avatar Jun 27 '25 11:06 Mexator

Resolved via https://github.com/autonomousapps/dependency-analysis-gradle-plugin/pull/1475.

autonomousapps avatar Jun 27 '25 21:06 autonomousapps

I've found another one issue with testFixtures. If there is a mixed code(java, kotlin) in the testFixtures sourceSet it still will give an incorrect advise.

TestFixturesReproducer2.zip

You can try it with ./gradlew :android-library:projectHealth

Should i open a new issue?

zepurplez avatar Jun 30 '25 12:06 zepurplez

I've found another one issue with testFixtures. If there is a mixed code(java, kotlin) in the testFixtures sourceSet it still will give an incorrect advise.

TestFixturesReproducer2.zip

You can try it with ./gradlew :android-library:projectHealth

Should i open a new issue?

I think, yes. This problem is not related to "Android test fixtures", as the original issue, but to the java-test-fixtures plugin. I'd like to tackle it but I'm not sure, how yet.

Mexator avatar Jun 30 '25 16:06 Mexator

https://github.com/autonomousapps/dependency-analysis-gradle-plugin/issues/1479

zepurplez avatar Jul 01 '25 06:07 zepurplez