0

当我将对该scopt库的依赖项添加到我的 Scala 3 应用程序时,我不再能够使用Proguard. 在尝试运行可执行 jar 时,我只看到以下错误消息:

Exception in thread "main" java.lang.VerifyError: Bad invokespecial instruction: interface method reference is in an indirect superinterface.
Exception Details:
  Location:
    scala/collection/SeqFactory$Delegate.tabulate(ILscala/Function1;)Ljava/lang/Object; @9: invokespecial
  Reason:
    Error exists in the bytecode
  Bytecode:
    0000000: 2a1b 2c4e 3d59 4c1c 2db7 000a b0       

        at scopt.OptionDef.<init>(OptionDef.scala:52)
        at scopt.OptionDef.<init>(OptionDef.scala:63)
        at HelloWorld$.<clinit>(HelloWorld.scala:1082)
        at HelloWorld.main(HelloWorld.scala)

这是重现该问题的最小示例:

$ cat build.sbt
lazy val root = (project in file(".")).
  settings(
    name := "helloworld",
    scalaVersion := "3.1.1"
  )

libraryDependencies ++= Seq(
  "com.github.scopt" %% "scopt" % "4.0.1"
)

enablePlugins(SbtProguard)

Proguard / proguardFilteredInputs ++= ProguardOptions.noFilter((Compile / packageBin).value)

Proguard / proguardInputs := (Compile / dependencyClasspath).value.files

Proguard / proguardOptions += ProguardOptions.keepMain("HelloWorld")

Proguard / proguardOptions += ProguardConf.helloWorld

Proguard / proguardVersion := "7.2.0"

$ cat project/ProguardConf.scala
object ProguardConf {

  val helloWorld =
"""
-dontnote
-dontobfuscate
-dontwarn java.lang.invoke.MethodHandle
-dontwarn scala.AnyKind
-libraryjars <java.home>/jmods
"""
}

$ cat project/build.properties
sbt.version=1.6.2

$ cat project/plugins.sbt
addSbtPlugin("com.github.sbt" % "sbt-proguard" % "0.5.0")

$ cat src/main/scala/HelloWorld.scala
import scopt.OParser

case class Config(name: String)

object HelloWorld {

  private val parser = OParser.sequence(
    OParser.builder[Config]
      .opt[String]("name")
      .action((value: String, option: Config) => option.copy(name = value)))

  def main(args: Array[String]) = {
    OParser.parse(parser, args, Config("World")) match {
      case Some(config) => {
        println(s"Hello ${config.name}")
      }
      case None =>
    }
  }
}

我正在打包并像这样运行它:

$ sbt
sbt:helloworld> proguard:proguard
Ctrl-D
$ java -jar target/scala-3.1.1/proguard/helloworld_3-0.1.0-SNAPSHOT.jar 

任何线索我可能错过了什么?非常感谢!

4

0 回答 0