Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply the com.android.application plugin using the new plugins DSL?

Tags:

android

gradle

I have a project configured through the triple of:

  • Declare repository in top-level build.gradle:

    buildscript { repositories { google() }}
    allprojects { repositories { google() }}
    
  • Declare classpath dependency, so plugin artifact gets downloaded from the appropriate repository, in top-level build.gradle

    buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.1.3' }}
    
  • Then, in the app build.gradle file, apply the plugin:

    apply plugin: 'com.android.application'
    

I want to migrate this to use the new plugins DSL as enabled by the PluginsDependenciesSpec.

I then:

  • Declared the repository in settings.gradle:

    pluginManagement { repositories { google() }}
    
  • Declared the plugin dependency in the app build.gradle:

    plugins { id "com.android.application" version "3.1.3" }
    

But this fails to resolve:

FAILURE: Build failed with an exception.

  • Where: Build file '…/build.gradle' line: 2

  • What went wrong: Plugin [id: 'com.android.application', version: '3.1.3'] was not found in any of the following sources:

  • Gradle Core Plugins (plugin is not in 'org.gradle' namespace)

  • Plugin Repositories (could not resolve plugin artifact 'com.android.library:com.android.library.gradle.plugin:3.1.3')

Searched in the following repositories:

  • Google
  • BintrayJCenter
  • maven(https://maven.fabric.io/public)
  • Gradle Central Plugin Repository

What am I missing to get Gradle to connect the dots here, so that it can connect the plugin.id to the appropriate JAR fetched from the appropriate repository?

like image 332
Jeremy W. Sherman Avatar asked Aug 03 '18 17:08

Jeremy W. Sherman


People also ask

How do I use Android plugins?

Run Android Studio. From the menu bar, select Android Studio > Preferences. Under IDE Settings, click Plugins and then click Install plugin from disk. Navigate to the folder where you downloaded the plugin and double-click it.

What is plugin DSL?

The plugins DSL provides a succinct and convenient way to declare plugin dependencies. It works with the Gradle plugin portal to provide easy access to both core and community plugins. The plugins DSL block configures an instance of PluginDependenciesSpec. To apply a core plugin, the short name can be used: Example 1.


1 Answers

Within the pluginManagement you can use resolutionStrategy and force usage of specific artifact from the google() repository.

pluginManagement {
    repositories {
        google()
    }

    resolutionStrategy {
        eachPlugin {
            if (requested.id.id in ['com.android.application', 'com.android.library']) {
                useModule("com.android.tools.build:gradle:${getProperty('version.plugin.android')}")
            }
        }
    }
}

Then you can apply android plugins with using new plugins DSL:

plugins {
    id 'com.android.application'
}

or

plugins {
    id 'com.android.library'
}

Here is the documentation.

like image 83
franta kocourek Avatar answered Sep 29 '22 19:09

franta kocourek