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

问题描述:

我在Android Studio 1.2.2中安装了1.5.2 Scala插件.我有一个用Java编码的现有(正在运行的)项目.我在Scala中添加了一个简单的类来替换现有的Java类,以进行尝试. IDE似乎可以验证Scala代码,但无法解析Java代码对类的引用.我不知道为什么我已经在该项目的服务器端成功混合了SBT下的语言.

I installed the 1.5.2 Scala plugin in Android Studio 1.2.2. I have an existing (functioning) project coded in Java. I added a trivial class in Scala to replace an existing Java class just to try it out. The IDE seems to validate the Scala code but the reference to the class from the Java code is unresolved. I don't know why. I've successfully mixed the languages under SBT on the server side of this project.

Java代码引用如下:

The Java code referencing looks like this:

package net.from.apprise;
import java.util.*;
import java.io.Serializable;
import org.json.*;
class ItemRelation extends SharableRecord implements Serializable, JSONable  {
    public static final String TAG = "IR";
    String listKey;
    String description;
    String barCode;
    Purch purch;

狙击...

Purch已从Java中定义的Purchase更改为确定". Scala代码是:

Purch was changed from Purchase which is defined in Java and was resolving OK. The Scala code is:

package net.from.apprise

class Purch {
   val whatever:Int = 1
}

尝试建立良率:错误:找不到符号类Purch

Purch类的Scala代码位于app/build/main/scala/net.from.apprise目录中.同样,Java源位于app/build/main/java/net.from.apprise中. AS会看到Scala代码,如果有则发出错误.但是类之间没有解决方案.

The Scala code for the Purch class lives in app/build/main/scala/net.from.apprise directory. Similarly, the Java source is in app/build/main/java/net.from.apprise. AS sees the Scala code, and issues errors if there are any. But no resolution between classes.

我需要做一些特别的事情还是我忽略了一些愚蠢的事情?配置AS?命名约定?

Do I need to do something special or am I overlooking something dumb? Configuration of AS? Naming convention?

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构建工具,那么我建议您使用 Grade 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.

下面是我的scala插件的骨架gradle文件的外观:

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