I'm using Gradle 2.12 (or newer) with an appropriate version of the Android Gradle plugin in my project. Gradle 2.12 introduced the compileOnly
configuration, so why do I get an error when I try to use it?
Could not find method compileOnly() for arguments
Note the following sentence from the Gradle 2.12 release notes regarding the new compileOnly
configuration (my emphasis):
You can now declare dependencies to be used only at compile time in conjunction with the Java plugin.
So the Java Gradle plugin is a component we need to consider when answering this question. We can find the compileOnly
configuration declared in the Java Gradle plugin source code for new enough versions.
However, the Android Gradle plugins do not direct extend the Java Gradle plugin. In fact, I believe the Android plugins represent a sort of 'frankenplugin', with some functionality borrowed but not inherited from the Java plugin. The following chunks of source code support this idea.
From the base Android plugin class:
project.apply plugin: JavaBasePlugin
The Android Gradle plugins therefore incorporate functionality from the base Java Gradle plugin, not from the full Java Gradle plugin. Moreover, there is an explicit check that the full Java Gradle plugin is not applied alongside an 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, my guess is that compileOnly
has just not been manually ported from the Java Gradle plugin to the Android Gradle plugin yet. It probably won't appear before we get an Android Gradle plugin with minimum Gradle version set at 2.12 or higher.
Simple use provided
instead of compileOnly
See https://github.com/google/auto/issues/324#issuecomment-212333044
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With