How can I integrate scala library in android proje

2019-09-20 15:53发布

问题:

This question already has an answer here:

  • Scala with android studio 1 answer
  • What is the simplest way to mix Java+Scala in an Android project using Gradle? 2 answers

I'm trying to integrate a Scala library in an android project. All the articles and previous answers seem to be outdated now.

Any help will be really appreciated. Thanks

回答1:

Scala source code is intended to be compiled to Java bytecode, so that the resulting executable code runs on a Java virtual machine. Scala provides language inter-operability with Java, so that libraries written in both languages may be referenced directly in Scala or Java code.

So a Scala library is just a jar (zip of compiled *.class byte-code files), and you must already know how to load a jar file.

Add scala-library dependency to Android Studio using Gradle

Add the Scala plugin buildscript dependency to your build.gradle:

    buildscript {
        repositories {

            jcenter()
            mavenCentral()
            maven { url 'https://jitpack.io' }
        }

        dependencies {
            classpath 'com.android.tools.build:gradle:2.3.0'
            classpath 'com.github.xingda920813:gradle-android-scala-plugin:android-gradle-2.3.0'

        }
    }
//Apply plugin
apply plugin: "com.android.application"
apply plugin: "jp.leafytree.android-scala"

The plugin decides scala language version using scala-library's version.

In your build.gradle:

dependencies {
    compile "org.scala-lang:scala-library:2.11.8"
    //**put your library here**
    compile "<yourlibrary>:<yourlibrary version>"
    //OR you can:
    //compile project(":lib1")//your library directory
}

See gradle-android-scala-plugin



标签: android scala