Excluding constraintlayout dependency in an implemented library


I am currently working on a task where I need to import a library created by someone else in my team. The problem is that there’s a bug in the library if you use constraint layout version 2.0.4 - one of the constraints gets messed up. However, this is solved if you downgrade to 2.0.1.

The problem is that my application uses constraint layout 2.0.4 in its “app” and “common” modules - if I downgrade the dependency to 2.0.1 in these two places, then the screen depending on the library works. If I keep the versions to 2.0.4 in my app and common modules, then the library also inherits 2.0.4 and the screen that depends on the library is buggy.

Is there any way in which I can determine the imported library to ignore my app’s usage of constraint layout 2.0.4 and continue to use its own implementation, which is 2.0.1?

The library is imported in the app module build.gradle file with

qaImplementation “ro.company.library:library-android-dev:$libraryVersionNo”

There’s also a canary and production version. From what I’ve seen, you can use the exclude keyword, but I’m not sure exactly how. Should the exclude belong to the library, where you would write in the constraint layout to be excluded? Should the exclude belong to the constraint layout, where you would write in the library to be excluded?

I’ve tried both and it doesn’t work. I’m not exactly sure what to write for the “group” or “module” of the exclude statement, either.


Not really.

You could grab the source code to ConstraintLayout and its related classes for one version, repackage it from androidx.constraintlayout to robert.constraintlayout, and then use the robert and androidx versions simultaneously (one in the library, one elsewhere). There used to be a tool named jarjar to help with that sort of renaming for compiled classes, but I don’t know if it is still around.

There can only be one androidx.constraintlayout.widget.ConstraintLayout class in your compiled app. So, the only way you are getting two is to rename one.

So then what is the exclude keyword used for?

For example, here:

implementation “androidx.cardview:cardview:1.0.0”, {
exclude group: ‘com.android.support’, module: ‘support-annotations’
exclude module: ‘support-annotations’

exclude says “ignore this transitive dependency; do not pull its contents into the app”. If that dependency is truly needed, something else will need to supply it.