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
- Apply the appropriate plugin first: add plugins { id 'com.android.library' } (or 'java') in the module's build.gradle before 'apply from: ...WeChatPublish.gradle'
- Reorder build script lines so 'apply from: gradle/WeChatPublish.gradle' comes after the android/java plugin application
- 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
- Always apply the base plugin ('java' or 'com.android.library') at the top of the module build file
- Apply publish scripts last, after all plugin blocks
- Centralize the apply order in a convention script so modules can't get it wrong
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
- This plugin must be applied after "com.android.library"…
- Matrix Plugin, Android Application plugin required.
- Cannot find
- Debugging APK not exist
- 'apk' property not defined in
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 = falseView on GitHub (pinned to 3b8293bd65)