How to isolate a dependency to a Java module only, even if the module is used in an Android module

A Java module is using log4j for testing, but that causes error in DEX generation in another Android module that uses the api of the Java module.

How can the dependencies in the Java module be “isolated” (like a private declaration in Java) so that it does not create this unwanted effect? Do I move the Log4j library to a test module, or what?

(Probably a question related to my ignorance, sorry).