Detecting clashing classes from the same Gradle multi-project

Hello!

I have been coming across the following case in a Gradle multi-project. I have 2 sub-projects A and B, and B is importing A as a dependency.

In sub-project A I have the following file:

package com.example.server

fun testProjectA() { println("Project A") }

in sub-project B I have the following files:

package com.example.server

fun testProjectB() { println("Project B") }
fun main () {
    testProjectA();
    testProjectB();
}

This code compiles well, but it will throw a NoSuchMethodError when calling testProjectA, and this is happening because the compiled code will result in the exact same class, and at runtime the JVM will pick the first class, so it will be the class from sub-project B, so only testProjectB will be available at runtime.

This issue can be simply resolved by changing the class name or moving it under a different package

But do you think the compiler could catch this and not compile the code? Or maybe there are other strategies that could be set in place. What do you think?

It doesn’t actually answer your question, but I think it is generally a bad idea to use the same package in two modules. You may get many kind of problems, including the one above.

2 Likes