13

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.

Emanuel Moecklin
  • 28,488
  • 11
  • 69
  • 85
  • Why would either of those be used `compileOnly`? That says "compile against the library, but do not include the contents in the APK". – CommonsWare Apr 17 '18 at 23:09
  • That's just an example. In reality I'd use releaseCompileOnly for the Stetho library. I wanted the example to be as simple as possible. – Emanuel Moecklin Apr 17 '18 at 23:11
  • another example (a real one) would be: productionCompileOnly 'com.bugsee:bugsee-android:1.11.9' with production being the flavor I'd use for production builds – Emanuel Moecklin Apr 17 '18 at 23:13
  • Using Gradle 4.4, I get the same error with the `compileOnly "com.android.support:support-v4:27.1.1"` from your question. Given the error message, perhaps the limitation is on AARs. I have used `compileOnly` successfully with AutoValue, but that's a JAR, as that's a pure-Java library. – CommonsWare Apr 17 '18 at 23:16
  • I guess that's true even if it's not documented anywhere. Not sure why such an important fact wouldn't be mentioned at all (not here: https://developer.android.com/studio/build/build-variants.html#configure-dependencies, nor here: https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html). – Emanuel Moecklin Apr 17 '18 at 23:25
  • 1
    Well, my AAR idea is just a theory. [This answer](https://stackoverflow.com/a/47818602/115145) reflects my thinking. In terms of why such a limitation would not be documented... *lots* of things in Android are not documented. – CommonsWare Apr 17 '18 at 23:28

3 Answers3

11

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
4

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.

Louis CAD
  • 10,965
  • 2
  • 39
  • 58
0

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.

Loïc Dumas
  • 1,036
  • 10
  • 18