Add Android plugin to gradle init script - gradle

I'm currently working on creating my own Gradle custom distribution and I want to add some default settings to all my Android projects. I tried to do this with the following init script:
initscript {
repositories {
mavenCentral()
google()
}
dependencies {
classpath "com.android.tools.build:gradle:7.1.1"
}
}
apply plugin: 'com.android.application'
gradle.allprojects {
android {
...
}
dependencies {
implementation 'junit:junit:4.12'
}
}
but with this I get Plugin with id 'com.android.application' not found. What am I missing here?

For gradle init script, you must use the fully qualified class name of the plugin instead of the id. So you will have to apply the plugin this way:
apply plugin: com.android.build.gradle.AppPlugin

Change the initscript to buildscript.
And also, does this build.gradle in root project/directy? Because you Android Gradle Plugin is applied in the gradle module not in the root.
So apply:
apply plugin: "com.android.application"
inside your module's build.gradle.

Make sure your Gradle version is compatible with your Android Gradle Plugin.
For more information you can follow the link below, if it helps you
plugin with id com.android.application not found

Related

Gradle DSL support for "latest.integration" version

I'm developing a custom plugin for gradle.
There is need to use latest version of plugin and it's already works with legacy gradle plugin application:
//build.gradle
buildscript {
...
dependencies {
classpath 'com.something:myPlugin:latest.integration'
}
...
}
apply plugin: com.something.my-plugin
Now I want to update my build script to use Gradle DSL, but it says that gradle cannot resolve "latest.integration" version for my plugin:
//setting.gradle
pluginManagement {
...
plugins {
id 'com.something.my-plugin' version "latest.integration"
}
...
}
//build.gradle
plugins {
id 'com.something.my-plugin'
}
Do anybody have a suggestion what I need to do? Or how I need to publish my plugin to support it?

How to use custom gradle plugin in library module project

I have a custom gradle plugin which has uploaded into jcenter, I can use it in my android project like:
root project build.gradle:
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.myproject:projectname:1.0.1'
}
app's build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.myproject.projectname'
myconfig {
......
}
It works fine. But when I use it in my android library module, It will show error message "Gradle DSL method no found:'myconfig()'"
library module's build.gradle
apply plugin: 'com.android.library'
apply plugin: 'com.myproject.projectname'
myconfig {
......
}
anyone known why?
I found the reason, in my gradle plugin I checked the project like this:
if (project.getPlugins().hasPlugin(AppPlugin)) {....}
so when I call it in library module, it can't goto the right branch.
put your jar/dependencies in a folder (suppose "libs")
then show the directory
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
compile name: 'jarFile'
}
or something like:
dependencies {
compile files('libs/something_local.jar')
compile 'package_name'
}

How to apply plugin to allprojects with new Gradle plugins mechanism?

Before Gradle 2.1 I could apply plugin to all projects by using allProjects closure (by prevoisly resolving the jar, of course):
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:3.0.1"
}
}
allprojects {
apply plugin: "com.jfrog.artifactory"
}
With new publishing mechanism it looks like the plugins closure can't be used inside allprojects:
allprojects {
plugins {
id "com.jfrog.artifactory" version "3.0.1"
}
}
fails with:
"Could not find method plugins() for arguments [build_xxxx_run_closure1_closure4#yyyyy] on root project"
What are the rules of using plugins closure? Is the plugin applied to current project only? If so, how can I apply it to all projects without repeating the plugins closure inside each build?
The new plugins {...} syntax cannot be used within a allprojects {...} or subprojects {...} closure. Additionally, it can only be used within build scripts (no script plugins, init scripts, etc). If you want to avoid having to apply the plugin to each project individually I'd suggest using the old notation. This is an issue the Gradle team is aware of and a solution will be introduced in future versions.
Update: Starting with Gradle 3.0 you can do this in a slightly modified way. You still have to explicitly use apply() but you no longer have to deal with all the buildscript { } nonsense to get the plugin on your classpath. This also allows you to conditionally apply plugins. Check out the Gradle 3.0 release notes for more information.
plugins {
id 'my.special.plugin' version '1.0' apply false
}
allprojects {
apply plugin: 'java'
apply plugin: 'my.special.plugin'
}

How to configure a plugin to depend on a specific version of gradle?

I am writing a set of Gradle plugins, but I want to control the specific versions of groovy and gradle that are used.
I don't want the plugins to depend on whatever versions of Gradle/Groovy are installed, like the following would do:
dependencies {
compile localGroovy()
compile gradleApi()
}
Another reason I don't want to use the local method - when you use a proper dependency specification, Gradle then knows about the source code for those libs and the IDE plugins can hookup the source automatically.
Below are the relevant sections of my build script:
allprojects { Project iProject ->
apply plugin: 'idea'
apply plugin: 'maven'
repositories {
jcenter()
}
}
subprojects { Project iProject ->
apply plugin: 'groovy'
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.3.2'
}
}
project(':eclipsei2g') {
group = 'eclipsei2g'
version = '0.0.1-SNAPSHOT'
dependencies {
compile 'org.gradle:gradle-core:2.0'
}
}
project(':g2idea13') {
group = 'g2idea13'
version = '0.0.1-SNAPSHOT'
dependencies {
compile 'org.gradle:gradle-core:2.0'
compile 'org.gradle-plugins:gradle-ide:2.0'
}
}
When I run this I get an error resolving the gradle-ide dependency:
Could not resolve all dependencies for configuration ':g2idea13:compile'.
> Could not find org.gradle:gradle-ide:2.0.
Searched in the following locations:
http://jcenter.bintray.com/org/gradle/gradle-ide/2.0/gradle-ide-2.0.pom
http://jcenter.bintray.com/org/gradle/gradle-ide/2.0/gradle-ide-2.0.jar
Required by:
g2idea13:g2idea13:0.0.1-SNAPSHOT
There doesn't seem to be anything on the jcenter repository since 0.9 for the plugins stuff.
I also tried 'org.gradle:gradle-ide:2.0'.
Is this even how I should be doing this? Is there another way to specify a specific gradle version? Am I just using the wrong repository? I couldn't even get gradle-core to resolve on mavenCentral(). Is there an official Gradle repository somewhere that I should be using?
gradleApi() is the way to go. There isn't currently a public list of dependencies for Gradle plugins.

Android Studio / gradle: "Could not create plugin of type 'LibraryPlugin'"

I have two modules in Android Studio.
Main is the application and Sub is a library module. Sub is referred from Main with compile project(':Sub') in the gradle script. That works when run from Android Studio. But when run from command line, gradlew says:
Could not create plugin of type 'LibraryPlugin'.
Caused by: java.lang.NoClassDefFoundError: org/gradle/api/artifacts/result/ResolvedComponentResult
This is the important parts in Main's build.gradle file:
apply plugin: 'android'
buildscript {
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
task wrapper(type: org.gradle.api.tasks.wrapper.Wrapper) {
gradleVersion = '1.11'
}
android {
buildToolsVersion '19.0.3'
}
dependencies {
compile 'com.android.support:support-v4:13.0.+'
compile project (':Sub')
}
The Sub gradle file is more or less identical, but has
apply plugin: 'android-library'
instead of 'android'
I have tried with gradle 1.9 and 1.10, but same result.
Anyone knows how to solve this?
Verify that your dependencies contains classpath 'com.android.tools.build:gradle:0.9.+' in each gradle.build file (or just put it in the base one and not declare it in the others). Update gradle/wrapper/gradle-wrapper.properties to point to gradle 1.11:
distributionUrl=http\://services.gradle.org/distributions/gradle-1.11-all.zip
If you have any other instances of the gradle-wrapper (such as if you originally made the library project on its own and later added an example app), verify that all instances are updated to point to the same version (each gradle-wrapper.properties file).

Resources