How to reference third-party classes when custom Tasks


(任强强) #1

hello, everybody
now , i use a third-party plugin with apply plugin 'xxx' in AndroidStudio, this plugin has a task , we called TaskA , and the TaskA has a input properties , the properties is a extension-object, we called propA, and i want define a task with TaskA’s Type ,so my code like this:

task customTask(type: TaskA){
  def p = new propA(project)
  propA p
}

this code is in a custom gradle file , i use apply from xxx.gradle to use it , when i run gradlew customTask in commandLine,it report a error :unable to resolve class com.xxx.xxx.TaskA, so what should i do ? please help me ,thx


(任强强) #2

I make sure that the plugin classpath has been configured in the right way.


(任强强) #3

there are my real code:

task addBaseChannel(type: com.leon.plugin.task.RebuildApkChannelPackageTask, dependsOn: 'addYingyongbaoChannelAfterJiagu') {
    def extension = new com.leon.plugin.extension.RebuildChannelConfigurationExtension(project)
    extension.baseReleaseApk = new File("${project.buildDir}/outputs/apk/base/release", getBaseApkName())
    extension.releaseOutputDir = new File("${project.buildDir}/channelRoot/channelDir")
    extension.channelFile = new File("${rootProject.rootDir}", "channelFile.txt")
    mRebuildChannelExtension = extension
}

when i run gradlew addBaseChannel,it report a error unable to resolve class com.leon.plugin.extension.RebuildChannelConfigurationExtension .
I make sure that the plugin classpath has been configured in the right way.

buildscript {

    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.leon.channel:plugin:2.0.1'
    }
}

(Chris Doré) #4

I’m not 100% clear on the classloader details, but everything I’ve seen shows that the gradle script included via apply from: needs to declare its own buildscript classpath dependencies in order to function. It does not inherit from build.gradle’s buildscript.


(任强强) #5

so, what should i do ? you mean i need add dependencies about third-party lib in custom.gradle?
code like this ?

// custom.gradle
task addBaseChannel(type: com.leon.plugin.task.RebuildApkChannelPackageTask, dependsOn: 'addYingyongbaoChannelAfterJiagu') {
    def extension = new com.leon.plugin.extension.RebuildChannelConfigurationExtension(project)
    extension.baseReleaseApk = new File("${project.buildDir}/outputs/apk/base/release", getBaseApkName())
    extension.releaseOutputDir = new File("${project.buildDir}/channelRoot/channelDir")
    extension.channelFile = new File("${rootProject.rootDir}", "channelFile.txt")
    mRebuildChannelExtension = extension
}

buildscript {

    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.leon.channel:plugin:2.0.1'
    }
}

(任强强) #6

yeah,right , when i add dependencies about third-party lib in custom.gradle , everything goes well, thanks a lot