Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Scala with Java in Android Studio

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.

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;

snip...

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
}

Attempts to build yield: error: cannot find symbol class Purch

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.

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

like image 793
Bill Michaelson Avatar asked Jun 16 '15 17:06

Bill Michaelson


1 Answers

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".

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.

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"]
}

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

app/java/com.example
  /ItemRelation.java
  /Purch.scala
like image 118
RenatoIvancic Avatar answered Sep 24 '22 03:09

RenatoIvancic