Search code examples
androidandroid-gradle-pluginandroid-library

Not to expose the jar file that I depend on


I have an AAR library that depends on a certain lib.jar. I added it in my AAR by doing:

dependencies {
    implementation files('myexternallibs/lib.jar')
    ...
}

In the resulting AAR, I can still access the classes in lib.jar. Based on many answers here in SO, the keyword implementation should keep this from happening. Anything I missed?


Solution

  • I changed implementation to compileOnly. It worked.

    Note from documentation:

    If you use this configuration, then your library module must include a runtime condition to check whether the dependency is available, and then gracefully change its behavior so it can still function if it's not provided.

    To do this, you need to check the classpath like so:

    try {
        Class.forName("com.company.ClassName");
    }
    catch (ClassNotFoundException ex) {
        // class is not available
    }