在 Android Studio 中将 Scala 与 Java 结合使用 [英] Using Scala with Java in Android Studio

查看:35
本文介绍了在 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 已从 Purchase 更改为 Java 中定义的并且解析正常.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 构建工具,那么我建议您使用 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

这篇关于在 Android Studio 中将 Scala 与 Java 结合使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆