Wire: Unable to use wire-gradle-plugin:3.5.0 using Gradle Kotlin DSL?

Created on 9 Nov 2020  路  12Comments  路  Source: square/wire

Hi All - apologies for the intrusion, I have what seems a very similar problem to the one which is described by @Globegitter in #1029 (responded to by @jrodbx) and I'm looking for a clear statement of whether I am doing the wrong thing, please?

  • I am building a small Kotlin app
  • I want to use Wire for Kotlin code generation from protobuf specs
  • I am using Gradle with the Kotlin DSL

output of a build

This is what happens:

$ gradlew shadowJar
FAILURE: Build failed with an exception.
* Where:
Build file '/Users/alecm/GitHome/code.repo/foo/build.gradle.kts' line: 13
* What went wrong:
Plugin [id: 'com.squareup.wire', version: '3.5.0', apply: false] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.squareup.wire:com.squareup.wire.gradle.plugin:3.5.0')
  Searched in the following repositories:
    Gradle Central Plugin Repository
    MavenRepo

settings.gradle.kts

After several experiments, my settings currently look like the attached, though I am not sure how much of it is really necessary.

rootProject.name = "foo"
pluginManagement {
    repositories {
        gradlePluginPortal()
        mavenCentral()
    }
}
dependencies { ...

build.gradle.kts

I have tried:

plugins {
    // both with-and-without "application"
    kotlin("jvm") version "1.4.10"
    id("com.squareup.wire") version "3.5.0" // both with-and-without "apply false"
    id("com.github.johnrengelman.shadow") version "6.1.0"
}
buildscript { // <- both with-and-without this
    repositories {
        mavenCentral()
    }
}

issue

Put simply, it looks like the plugin does not resolve; the plugin documentation for Groovy-Kotlin appear to be suspicious (because id 'com.squareup.wire' does not specify a version?) - and I can't find any other documentation on how to coerce the plugins block to look for the plugin elsewhere, or under a different, perhaps hyphenated, name?

Most helpful comment

FYI: To make the plugin resolve properly without the need for extra config in settings.gradle, just add gradle plugin publish plugin to your plugin gradle config as in this example. This will create and publish all required gradle markers properly for you.

All 12 comments

Notes:

  • if you are using shadowJar then using application is redundant.
  • in the spirit of shotgun debugging, I also tried:
dependencies {
implementation("com.squareup.wire:wire-gradle-plugin:3.5.0")
...

...which at least didn't cause a "cannot find" error, but didn't leave me with a wire {} configuration to work with.

more experimentation with assistance from @mpetuska, but still failing:

plugins {
    // application
    kotlin("jvm") version "1.4.10"
    id("com.squareup.wire") version "3.5.0" apply false
    id("com.github.johnrengelman.shadow") version "6.1.0"
}
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("com.squareup.wire:wire-gradle-plugin:3.5.0")
    }
}
apply(plugin = "wire-gradle-plugin")
// or else: apply(plugin = "com.squareup.wire:wire-gradle-plugin")

Trying this

plugins {
    kotlin("jvm") version "1.4.10"
    id("com.github.johnrengelman.shadow") version "6.1.0"
}
buildscript {
    repositories {
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath("com.squareup.wire:wire-gradle-plugin:3.5.0")
    }
}
apply(plugin = "com.squareup.wire")

...successfully loads, but then fails with e: /Users/alecm/GitHome/code.repo/psm/build.gradle.kts:58:1: Unresolved reference: wire when I try to use wire { ... }

Maybe related to #1828
Looks like @jrodbx is on it.

Hi @oldergod - thanks for the feedback. After some chat with @alokmenghrajani who pointed me at this repo by @Egorand for some inspiration, the state of my solitary build.gradle.kts is in this gist and I am at an impasse.

If I uncomment lines 48..52 the wire declaration is unrecognised:

  Line 48: wire {
           ^ Unresolved reference: wire

However if I attempt to activate (?) the plugin by uncommenting line 22, I experience:

Plugin [id: 'com.squareup.wire', version: '3.3.0', apply: false] was not found in any of the following sources:
- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'com.squareup.wire:com.squareup.wire.gradle.plugin:3.3.0')
  Searched in the following repositories:
    MavenRepo
    Gradle Central Plugin Repository

...and tweaks like removing both the version and apply result in slightly changes error messages but still no happy.

Something must still be missing, either in environment or in my approach?

Edit: tried adding implementation("com.squareup.wire:wire-runtime:3.3.0") to the deps in case lack of the runtime was stopping it declaring wire; no change :-(

Pinging @oldergod - I've posted a vanilla and "simple as I can make it" PoC at:

https://github.com/alecmuffett/wire-playground

The challenge is to amend this code and whatever else, so that we can amend Main to print a hexdump of a Dinosaur message. :-)

Thank you for help with this, really; I have spent about 20 hours on this now, trying different ways, and the closest I have gotten so far has required me to throw away all my build.gradle.ktsscripts, start over from scratch, and embrace both multiproject-gradle and multiplatform-kotlin... resulting in my being able to convert protofiles to Kotlin, but I can't work out how to restore enough of my project to do anything with them.

Comparing the last two snippets:

plugins {
    // application
    kotlin("jvm") version "1.4.10"
    id("com.squareup.wire") version "3.5.0" apply false
    id("com.github.johnrengelman.shadow") version "6.1.0"
}
buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("com.squareup.wire:wire-gradle-plugin:3.5.0")
    }
}
apply(plugin = "wire-gradle-plugin")
// or else: apply(plugin = "com.squareup.wire:wire-gradle-plugin")
plugins {
    kotlin("jvm") version "1.4.10"
    id("com.github.johnrengelman.shadow") version "6.1.0"
}
buildscript {
    repositories {
        mavenCentral()
        gradlePluginPortal()
    }
    dependencies {
        classpath("com.squareup.wire:wire-gradle-plugin:3.5.0")
    }
}
apply(plugin = "com.squareup.wire")

if you're using the plugins {} syntax to add the plugin, then I believe you'll need to add gradlePluginPortal() to your buildscript repositories block. The second snippet does so, but removed wire from the plugins block.

What happens if you left this:

plugins {
    id("com.squareup.wire") version "3.5.0" apply false

and added:

buildscript {
    repositories {
         ...
        gradlePluginPortal()
    }

I don't know why but Gradle tries to resolve com.squareup.wire:com.squareup.wire.gradle.plugin while the correct coordinates for the plugin are com.squareup.wire:wire-gradle-plugin.

You can fix this by adding the following to your settings.gradle.kts .

pluginManagement {
+  resolutionStrategy {
+    eachPlugin {
+      if (requested.id.id == "com.squareup.wire") {
+        // For some reason, Gradle does a lookup on the wrong coordinates:
+        // 'com.squareup.wire:com.squareup.wire.gradle.plugin' instead of the one below.
+        useModule("com.squareup.wire:wire-gradle-plugin:${requested.version}")
+      }
+    }
+  }
  repositories {
    mavenCentral()
    gradlePluginPortal()
  }
}

Seems to be part of Gradle's spec. https://docs.gradle.org/6.4.1/userguide/plugins.html#sec:plugin_markers

For future people: it's fixed with this diff. Props and thanks to @oldergod, and closing.

ps: I would RFE adding this to the documentation on https://github.com/square/wire/blob/master/wire-library/docs/wire_compiler.md for future travellers.

FYI: To make the plugin resolve properly without the need for extra config in settings.gradle, just add gradle plugin publish plugin to your plugin gradle config as in this example. This will create and publish all required gradle markers properly for you.

Was this page helpful?
0 / 5 - 0 ratings