I am trying to create a custom plugin which i can apply in the gradle.build file. There are a few ways to do this but i am doing it using the buildSrc folder. This means the plugin will be tied to my build and not portable. from the docs the buildSrc folder should have the following properties:
buildSrc project You can put the source for the task class in the rootProjectDir/buildSrc/src/main/groovy directory. Gradle will take care of compiling and testing the task class and making it available on the classpath of the build script. The task class is visible to every build script used by the build. However, it is not visible outside the build, and so you cannot reuse the task class outside the build it is defined in. Using the buildSrc project approach separates the task declaration - that is, what the task should do - from the task implementation - that is, how the task does it.
But anyway my issue is below:
already i created a buildSrc directory in the root project path and created a groovy folder which is recognized by the IDE.
below is my fold structure and the error i am getting:
Now here is the task and the plugin itself:
RenameAppVersionNameTask.groovy:
package com.myplugins
import org.gradle.api.DefaultTask
import org.gradle.api.tasks.TaskAction
class RenameAppVersionNameTask extends DefaultTask {
@TaskAction
def run() {
project.configure(project) {
// Check if plugin works on an Android module
if (it.hasProperty("android")) {
// Iterate over app build variants (build types + flavors)
project.android.applicationVariants.all { variant ->
// Only change debug build type variants
if (variant.buildType.name == project.android.buildTypes.debug.name) {
// Rename versionName
def customVersionName = variant.mergedFlavor.versionName
variant.mergedFlavor.versionName = customVersionName + " custom"
}
}
}
}
}
RenameAppVersionNameTask() {
group = 'customPlugin'
description = 'Renames versionName of the app depends on the current git branch name'
}
}
CustomPlugin.groovy:
package com.myplugins
import org.gradle.api.Plugin
import org.gradle.api.Project
class CustomPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.task('renameAppVersionName', type: RenameAppVersionNameTask)
project.tasks.getByName('preBuild').dependsOn('renameAppVersionName')
}
}
The error i am receiving on gradle sync is:
Failed to sync Gradle project 'My GradlePluginApplication'
Error:Unable to load class 'CustomPlugin'.
Possible causes for this unexpected error include:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
<a href="syncProject">Re-download dependencies and sync project (requires network)</a></li><li>The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
<a href="stopGradleDaemons">Stop Gradle build processes (requires restart)</a></li><li>Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.</li></ul>In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.
also i noticed i cant even apply the plugin:
I confirmed android studio is using the gradle wrapper. also if needed the gradle-wrapper.properties is below:
distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists distributionUrl=https://services.gradle.org/distributions/gradle-2.10-all.zip