Using Scala with Java in Android Studio

后端 未结 2 2320
鱼传尺愫
鱼传尺愫 2021-02-20 09:27

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 Jav

相关标签:
2条回答
  • 2021-02-20 09:41

    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

    0 讨论(0)
  • 2021-02-20 09:48

    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
    
    0 讨论(0)
提交回复
热议问题