Using the compileOnly scope in Android projects?

I am using Gradle 2.12 (or later) with the corresponding version of the Android Gradle plugin in my project. Gradle 2.12 introduced the compileOnly configuration, so why am I getting an error when I try to use it?

Could not find compileOnly () method for arguments

+8
android gradle gradle
source share
2 answers

Simple use of provided instead of compileOnly

See https://github.com/google/auto/issues/324#issuecomment-212333044

+3
source share

Take a look at the following sentence from the Gradle 2.12 release notes regarding the new compileOnly configuration (my emphasis):

Now you can declare dependencies that will only be used at compile time in conjunction with the Java plugin .

So, the Java Gradle plugin is a component that we need to consider when answering this question. We can find the compileOnly configuration declared in the Java Gradle plugin source code for new versions.

However, Android Gradle plugins do not redirect the Java Gradle plugin . In fact, I think Android plugins are a kind of "frankenplugin", with some features borrowed but not inherited from the Java plugin. The following pieces of source code support this idea.

From the base class of the Android plugin :

 project.apply plugin: JavaBasePlugin 

Android Gradle plugins therefore include functionality from the underlying Java Gradle plugin , rather than from the full Java Gradle plugin . In addition, there is an explicit check that the full Java Gradle plugin is not used with the Android Gradle plugin:

 // get current plugins and look for the default Java plugin. if (project.plugins.hasPlugin(JavaPlugin.class)) { throw new BadPluginException( "The 'java' plugin has been applied, but it is not compatible with the Android plugins.") } 

Based on this information, I assume that compileOnly just wasn't manually ported from the Java Gradle plugin to the Android Gradle plugin. It probably won’t appear until we get the Android Gradle plugin with the minimum version of Gradle set to 2.12 or higher.

+10
source share

All Articles