android.registerTransform doesn't work when called from apply from: 'other.gradle'

350 views
Skip to first unread message

Malcolm Evershed

unread,
May 9, 2017, 5:05:13 AM5/9/17
to adt-dev
My app/build.gradle says at the end "apply from: 'other.gradle'". The other.gradle file is very simple and contains the following:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        // Needed for com.android.build.api classes
        classpath 'com.android.tools.build:gradle-api:2.3.1'
    }
}

import com.android.build.api.transform.QualifiedContent
import com.android.build.api.transform.Transform

// Stub implementation
class MyTransform extends Transform {
    @Override
    String getName() {
        return null
    }

    @Override
    Set<QualifiedContent.ContentType> getInputTypes() {
        return null
    }

    @Override
    Set<QualifiedContent.Scope> getScopes() {
        return null
    }

    @Override
    boolean isIncremental() {
        return false
    }
}

Class parameterClass

for (def method : android.getClass().getMethods()) {
    if (method.getName() == "registerTransform") {
        parameterClass = method.getParameterTypes()[0]
        println 'Class of first parameter to android.registerTransform: ' + parameterClass
    }
}

Class superClass = MyTransform.class.getSuperclass()
println 'Class of parameter that we want to pass to android.registerTransform: ' + superClass
println 'Are the names of the two types equal? ' + superClass.getCanonicalName().equals(parameterClass.getCanonicalName())
println 'Are the two types equal? ' + superClass.equals(parameterClass)

// This fails with:
// org.gradle.internal.metaobject.AbstractDynamicObject$CustomMessageMissingMethodException: Could
// not find method registerTransform() for arguments [MyTransform@cac15ac] on object of type
// com.android.build.gradle.AppExtension.
android.registerTransform(new MyTransform())

Why is this occuring? How can it be dealt with?

The printlns suggest that the superclass of MyTransform is a different type from the first parameter to registerTransform(), but I don't understand how that could be. The names match, but somehow the types are not the same.

Thanks.

Alex Waters

unread,
May 30, 2017, 7:54:17 PM5/30/17
to adt-dev
When you do apply from: 'other.gradle' it does weird classloader things such that the two scripts have different classloaders (and thus even though it's the "same" class, it's not seen as the same to the JVM - a very irritating aspect of Gradle build scripts).
Can you put that same code at the end of the file instead? If that works then it's likely this.
I can't suggest a work-around unfortunately other than not using "apply from" (if you do discover a nice fix please post it)

Malcolm Evershed

unread,
May 30, 2017, 8:05:53 PM5/30/17
to adt-dev
To work-around, you can put the code in a buildSrc project: https://docs.gradle.org/3.3/userguide/organizing_build_logic.html#sec:build_sources
Reply all
Reply to author
Forward
0 new messages