Use of the 'compileOnly' scope in Android projects?

后端 未结 2 1039
猫巷女王i
猫巷女王i 2021-02-14 14:42

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 w

相关标签:
2条回答
  • 2021-02-14 15:19

    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.

    0 讨论(0)
  • 2021-02-14 15:31

    Simple use provided instead of compileOnly

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

    0 讨论(0)
提交回复
热议问题