13

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?

10
  • Not sure how AS handle this this, but isn't the convention to use / instead of . for the actual path? E.g. app/build/main/java/net/from/apprise on the filesystem? Commented Jun 16, 2015 at 18:01
  • Yes, thanks, that is the convention for Java, although it should not matter for Scala as class definitions can appear anywhere. And in fact my post above is wrong - the path for the Java (but not the Scala) uses slashes. AS displays the slashes as "." in the UI for some reason, so I was thrown by that. I'm going to make it more consistent to see what happens. Commented Jun 16, 2015 at 18:09
  • Moved the Scala to app/src/main/scala/net/from/apprise to correspond to Java convention. No effect. Commented Jun 16, 2015 at 18:13
  • 1
    I've been advised on IRC to set it all up with SBT and then import it. Commented Jun 17, 2015 at 14:57
  • 1
    See scala-android.org/quickstart for some good doc on how to do this. Commented Jul 7, 2018 at 16:07

2 Answers 2

12

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
Sign up to request clarification or add additional context in comments.

1 Comment

Can you still separate the Scala and Java files though? As in app/java/com.example/ItemRelation.java and app/scala/com.example/Purch.scala?
2

when you follow @Renatol steps (which is great), you will encounter an error :

Could not find matching constructor for: org.gradle.api.internal.tasks.DefaultScalaSourceSet(java.lang.String, org.gradle.api.internal.file.BaseDirFileResolver)

so take a look at this answer to continue the process :

gradle-android-scala-plugin gives "Could not find matching constructor" error

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.