且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Android Studio 中将 Scala 与 Java 结合使用

更新时间:2021-12-04 02:35:47

Scala 插件 只是帮助您编写 Scala 代码的工具.它非常聪明,可以在 IDE 中从 java(和其他方式)引用 Scala 类,我认为您的意思是IDE 似乎验证了 Scala 代码".

The Scala Plugin for Android Studio or IntelliJ is only a tool for helping you at writing scala code. It is so smart it makes possible to reference the scala classes from java (and other way around) in IDE, that I think you meant with "IDE seems to validate the Scala code".

如果您正在开发 android 应用程序并使用 gradle 构建工具,那么我建议您使用 Gradle Scala 插件.它正在与 gradle 的官方 android 插件一起使用.然后,此插件会制作指令,将您的 Scala 文件构建为以 .class 结尾的字节码,用于运行应用程序.

If you are developing an android application and you use a gradle build tool then I recommend to you that you use the Gradle scala plugin. It is working with official android plugin for gradle. This plugin is then making instructions for building your scala files into bytecode with .class ending which is used in running application.

下面是我的 gradle 框架文件对于 Scala 插件的样子:

Below is how my skeleton gradle file looks like for scala plugin:

buildscript {

    repositories {

        mavenCentral()
    }
    dependencies {

        classpath 'com.android.tools.build:gradle:1.2.3'

        // scala from https://github.com/saturday06/gradle-android-scala-plugin
        classpath "jp.leafytree.gradle:gradle-android-scala-plugin:1.4"
    }

}

android { 

    ... 
}

apply plugin: 'com.android.application'
apply plugin: "jp.leafytree.android-scala"

dependencies {

    ...
    compile 'org.scala-lang:scala-library:2.11.6'
}

// Configuration for scala compiler options as follows
tasks.withType(ScalaCompile) {
    // If you want to use scala compile daemon
    scalaCompileOptions.useCompileDaemon = true
    // Suppress deprecation warnings
    scalaCompileOptions.deprecation = false
    // Additional parameters
    scalaCompileOptions.additionalParameters = ["-feature"]
}

而且.java和.scala文件可以混在同一个项目目录下,如:

And .java and .scala files can be mixed in the same project directory, as in:

app/java/com.example
  /ItemRelation.java
  /Purch.scala