Tencent/tinker · error · GradleException

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

Error message

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

What it means

This GradleException is thrown by gradle/WeChatPublish.gradle when the wechatPublish script is applied to a project in which neither the 'com.android.library' nor the 'java' Gradle plugin has already been applied. The script branches at apply time on plugins.hasPlugin(...) to pick the right extension class (WeChatAndroidLibraryPublishExtension vs WeChatJavaLibraryPublishExtension); if neither plugin is present it cannot build a publishing extension, so it aborts. It is a pure plugin-ordering/configuration contract violation, not a runtime failure.

Source

Thrown at 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 /* Deprecated */

View on GitHub (pinned to 1b7ea02c23)

Solutions

  1. In the module's build.gradle, ensure 'java' (or 'com.android.library') is applied BEFORE the line 'apply from: <path>/WeChatPublish.gradle'.
  2. If the module is an Android app (com.android.application), move wechatPublish to a library module — the script only supports Java/Android library publishing.
  3. If you need it in a multi-module build, apply it selectively: subprojects { if (it.plugins.hasPlugin('java') || it.plugins.hasPlugin('com.android.library')) apply from: rootProject.file('gradle/WeChatPublish.gradle') }.
  4. If you control the script, use pluginManager.withPlugin('java') { ... } / withPlugin('com.android.library') { ... } hooks instead of an immediate hasPlugin check so apply order no longer matters.

Example fix

// before (app/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 the module (or a root allprojects hook) BEFORE applying the publish script
if (!plugins.hasPlugin('java') && !plugins.hasPlugin('com.android.library')) {
    throw new GradleException("wechatPublish requires 'java' or 'com.android.library' first")
}
apply from: rootProject.file('gradle/WeChatPublish.gradle')

Prevention

When it happens

Trigger: Applying 'gradle/WeChatPublish.gradle' (or a plugin that applies it) in a module where: (a) apply from: ...WeChatPublish.gradle appears BEFORE apply plugin: 'java' / 'com.android.library'; (b) the module only applies 'com.android.application' (an app module, not a library); (c) the module applies none of the supported plugins at all (e.g. a plain groovy/kotlin/server module — the source comment says 'TODO: Support more languages').

Common situations: Copying the publish script into an app module of the Tinker sample; reordering plugin/apply-from statements while refactoring root or module build scripts; using the script in an Android application project assuming it publishes APKs; hoisting the apply from into allprojects/subprojects blocks where some subprojects are not java/android-library modules.

Related errors


AI-assisted analysis of Tencent/tinker@1b7ea02c23 (2026-08-14). Data as JSON: /api/errors/8633016ba9e262b8. Report an issue: GitHub.