在使用 Android SDK 插件编译 Scala Android 多项目期间,X 已被定义为对象 X

X is already defined as object X during compiling Scala Android multiproject with Android SDK Plugin

最近我开始尝试使用 Scala 和 Android。最初我使用 jp.leafytree.android-scala 插件,但随着时间的推移,我遇到了越来越多的问题,我希望我可以通过使用 Android SDK 插件将项目移动到 SBT 来简单地解决这些问题。

目前我遇到以下错误:

[error] /media/Workspaces/Project/modules/core/target/android/generated/source/com/example/client/core/BuildConfig.java:6: BuildConfig is already defined as object BuildConfig
[error] public final class BuildConfig {
[error]                    ^
[error] /media/Workspaces/Project/modules/core/target/android/generated/source/com/example/client/core/R.java:10: R is already defined as object R
[error] public final class R {
[error]                    ^
[error] /media/Workspaces/Project/modules/core/target/android/generated/source/com/example/client/core/TR.scala:6: TypedResource is already defined as case class TypedResource
[error] case class TypedResource[A](id: Int)
[error]            ^
[error] /media/Workspaces/Project/modules/core/target/android/generated/source/com/example/client/core/TR.scala:7: TypedLayout is already defined as case class TypedLayout
[error] case class TypedLayout[A](id: Int)
[error]            ^
[error] /media/Workspaces/Project/modules/core/target/android/generated/source/com/example/client/core/TR.scala:9: TR is already defined as object TR
[error] object TR {
[error]        ^
[error] /media/Workspaces/Project/modules/core/target/android/generated/source/com/example/client/core/TR.scala:119: TypedFindView is already defined as trait TypedFindView
[error] trait TypedFindView extends Any {
[error]       ^
[error] /media/Workspaces/Project/modules/core/target/android/generated/source/com/example/client/core/TR.scala:124: TypedResource is already defined as object TypedResource
[error] object TypedResource {
[error]        ^
[error] 7 errors found

我不确定是哪个 SBT 设置导致了这个错误。我的配置是:

/
+-- modules/
| +-- core/ (library)
| +-- domain/ (library)
| +-- app/ (application)
|
+-- project/
  +-- Build.scala
  +-- Dependencies.scala
  +-- Settings.scala
  +-- plugins.sbt

project/plugins.sbt:

addSbtPlugin("com.hanhuy.sbt" % "android-sdk-plugin" % "1.5.12")

project/Build.scala:

import sbt._

object Build extends android.AutoBuild {

  import Dependencies._
  import Settings._

  lazy val core = project.from("core")
    .configureAsLibrary
    .configureModule

  lazy val domain = project.from("domain")
    .configureAsLibrary
    .configureModule
    .dependsOnLibraries(core)

  lazy val app = project.from("app")
    .configureModule
    .dependsOnLibraries(domain)
}

project/Dependencies.scala:

import sbt._

trait Dependencies {

  val scalaVersionUsed = "2.11.7"

  // resolvers
  val commonResolvers = Seq(
    Resolver sonatypeRepo "public",
    Resolver typesafeRepo "releases"
  )

  // ... libraries ...

  val mainDeps = Seq(appcompat, design, support, multidex, scalaLib, sqlDroid, slick, shapeless, joda, jodaConvert,
    facebook)

  val testDeps = Seq(mockito, spec2, spec2Core, spec2JUnit)
}

object Dependencies extends Dependencies {

  implicit class ProjectFrom(project: Project) {

    private val commonDir = "modules"

    def from(dir: String): Project = project in file(s"$commonDir/$dir")
  }

  implicit class DependsOnLibraries(project: Project) {

    import android.Dependencies.RichProject

    def dependsOnLibraries(projects: ProjectReference*): Project = project.androidBuildWith(projects:_*)
  }
}

project/Settings.scala:

import android.Plugin._
import sbt._
import sbt.Keys._

trait Settings {

  import Dependencies._

  private val modulesSettings = Seq(
    organization := "com.example",
    version := "0.1.0-SNAPSHOT",

    scalaVersion := scalaVersionUsed,

    resolvers ++= commonResolvers,

    libraryDependencies ++= mainDeps,
    libraryDependencies ++= testDeps map (_ % "test")
  )
}

object Settings extends Settings {

  implicit class LibraryConfigurator(project: Project) {

    def configureAsLibrary: Project = project.settings(androidBuildAar:_*)
  }

  implicit class ModuleConfigurator(project: Project) {

    def configureModule: Project = project.settings(modulesSettings: _*)
  }
}

它基本上只是将 project.settings(androidBuildAar:_*) 应用于 core 和 `domain,然后添加库依赖项和一些选项。

sbt clean 没有帮助。手动删除所有 target/ 目录及其内容也无济于事:在下一次 sbt 编译时,所有文件将再次生成,错误也会生成。它的原因是什么?我该怎么做才能解决这个问题?

androidBuild* 和 AutoBuild 不能一起使用。

对于你的情况,删除 AutoBuild

https://github.com/pfn/android-sdk-plugin/issues/88