Android dependency is set to compileOnly/provided

2020-03-01 10:24发布

问题:

I'm using com.android.tools.build:gradle:3.1.1 with the latest Gradle version (https://services.gradle.org/distributions-snapshots/gradle-4.8-20180417000132+0000-all.zip).

When I use compileOnly dependencies some of them won't compile, some will. E.g.

compileOnly "com.android.support:support-v4:27.1.1"

works perfectly while

compileOnly "com.facebook.stetho:stetho:1.5.0"

gives a compile error:

Android dependency 'com.facebook.stetho:stetho:1.5.0' is set to compileOnly/provided which is not supported

I was under the impression than any dependency can be compileOnly. Nothing indicates otherwise (https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations). Both of these libraries have transitive dependencies.

Any help would be greatly appreciated.

回答1:

As an experiment, I created a new Android Studio 3.1.1 project. Then, I added a lib module to it as a plain Java library module. I could add compileOnly project(":lib") to the app module, and it compiled. I changed the lib module to be an Android library module (apply plugin: 'com.android.library') with a minimum manifest, and now compileOnly project(":lib") gets the error that you do: "Android dependency 'project :lib' is set to compileOnly/provided which is not supported".

Since there were no other material changes in the lib module, the compileOnly limitation is on Android library modules.

My guess is that it is unclear what "compile only" means for manifest entries, resources, assets, etc. So, they officially punted.

I filed an issue, requesting documentation of this limitation. My requests for documentation usually fall on deaf ears.



回答2:

At the dawn of "Dynamic feature modules", compileOnly Android library modules could make sense, to allow easy access to the feature module from the base app when it is installed.

That's why I created this feature request: https://issuetracker.google.com/issues/109894265

Feel free to star it and comment with your use cases.



回答3:

I had a similar issue on a project with many libraries.

I have a libX that I implement in debug with debugImplementation project(':libX') to work with sources, but in release build I target published version releaseImplementation "com.company:libX:1.0.0".

After a refactor, I got a similar error during a release sync about my libX.

Android dependency 'com.company:libX:1.0.0' is set to compileOnly/provided which is not supported.

However, I wasn't using any compileOnly...

The problem was due to one lib which was using libX always as source, (implementation project(':libX')). So in release, gradle was confused as it was implementing libX as sources in some libs, and as published lib in other.