Tencent/matrix · error · GradleException

This plugin must be applied after "java" or…

Error message

This plugin must be applied after "java" or "com.android.library" plugin

What it means

The WeChatPublish Gradle plugin inspects the plugins already applied to the project to decide which extension class to register (Android library, Android application, or plain Java). If neither 'java' nor 'com.android.library' (nor an Android application plugin) has been applied before this script runs, it cannot determine the publication type and throws a GradleException immediately at configuration time.

Solutions

  1. Apply the appropriate plugin first: add plugins { id 'com.android.library' } (or 'java') in the module's build.gradle before 'apply from: ...WeChatPublish.gradle'
  2. Reorder build script lines so 'apply from: gradle/WeChatPublish.gradle' comes after the android/java plugin application
  3. If the module should not publish, remove the WeChatPublish apply line instead of keeping the plugin without a base plugin

Example fix

// before (module build.gradle)
apply from: rootProject.file('gradle/WeChatPublish.gradle')
apply plugin: 'com.android.library'

// after
apply plugin: 'com.android.library'
apply from: rootProject.file('gradle/WeChatPublish.gradle')
Defensive patterns

Strategy: validation

Validate before calling

// in module build.gradle, before applying WeChatPublish.gradle
assert plugins.hasPlugin('com.android.library') || plugins.hasPlugin('java') : 'Apply java or com.android.library plugin before WeChatPublish'

Prevention

When it happens

Trigger: Applying 'wechatPublish' (WeChatPublish.gradle) in a module whose build.gradle applies no 'java', 'java-library', 'com.android.library', or 'com.android.application' plugin, or applying it before the plugin block that adds them.

Common situations: Adding the publish script to a brand-new empty module, to an Android module where the android plugin is applied after the publish script line, or copying the script into a Kotlin/Multiplatform/agp-less project.

Understand the failure class

Background: "not installed", "pip install", "required for": how missing-dependency errors surface across open-source libraries — this error's family across 34 libraries.

Related errors


AI-assisted analysis of Tencent/matrix@3b8293bd65 (2026-09-08). Data as JSON: /api/errors/6294fad5e76c159e. Report an issue: GitHub.

Appendix: source

Thrown at matrix/matrix-android/gradle/WeChatPublish.gradle:13

def extensionClass

// Detect supported plugin modules
if (plugins.hasPlugin('com.android.library')) {
    // Android library mode
    extensionClass = WeChatAndroidLibraryPublishExtension.class
} else if (plugins.hasPlugin('java')) {
    // Java library mode
    extensionClass = WeChatJavaLibraryPublishExtension.class
} else {
    // TODO: Support more languages
    throw new GradleException('This plugin must be applied after "java" or "com.android.library" plugin')
}

// Register wechatPublish extension
extensions.create('wechatPublish', extensionClass, project)
ext.artifactId = name

class WeChatPublishExtension {

    boolean isSnapshot = true
    private String versionSuffix = ''
    protected boolean printModules = false

    boolean withJavadoc = true
    boolean withSources = true
    boolean withNativeSymbols = true
    boolean withDependencies = true

    boolean publishToBintray = false

View on GitHub (pinned to 3b8293bd65)