Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.allbus:gradle-android-scala-plugin:3.6.3-M2'
}
<dependency>
<groupId>com.github.allbus</groupId>
<artifactId>gradle-android-scala-plugin</artifactId>
<version>3.6.3-M2</version>
</dependency>
libraryDependencies += "com.github.allbus" % "gradle-android-scala-plugin" % "3.6.3-M2"
:dependencies [[com.github.allbus/gradle-android-scala-plugin "3.6.3-M2"]]
gradle-android-scala-plugin adds scala language support to official gradle android plugin. See also sample projects at https://github.com/saturday06/gradle-android-scala-plugin/tree/master/sample
<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->Table of Contents generated with DocToc
| Version | Scala | Gradle | Android Plugin | compileSdkVersion | comment | | -------- | ------- | ------ | ------------------- | ----------------- | ------------------ | | 3.6.3-M2 | 2.11.12 | 5.6.4 | 3.6.3 | 29 | experiment R.java | | 3.5.1 | 2.11.12 | 5.6.3 | 3.5.1 | 29 | | | 3.4.0 | 2.11.12 | 5.3 | 3.4.0 | 28 | | | 3.3.2 | 2.11.12 | 5.3 | 3.3.2 | 28 | without submodules | | 3.2.0-M1 | 2.11.12 | 4.9 | 3.2.0 | 28 | |
If you want to use older build environment, please try android-scala-plugin-1.3.2
3.6.3-M2 fix issue #5 (Resource linking fails in projects with multiple modules) Experiment with replace R.jar to generated R.java. If it not compiled use previous version. Important! Use import R only from current module package.
build.gradle
buildscript {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.github.AllBus:gradle-android-scala-plugin:3.5.1'
}
}
build.gradle
apply plugin: "com.android.application"
apply plugin: "jp.leafytree.android-scala"
The plugin decides scala language version using scala-library's version.
build.gradle
dependencies {
implementation "org.scala-lang:scala-library:2.11.12"
}
Default locations are src/main/scala, src/androidTest/scala. You can customize those directories similar to java.
build.gradle
android {
sourceSets {
main {
java {
srcDirs "path/to/main/scala"
}
}
}
}
The Scala Application generally suffers DEX 64K Methods Limit. To avoid it we need to implement one of following workarounds.
If your project doesn't need to run androidTest
, You can use proguard
to reduce methods.
Sample proguard configuration here:
proguard-rules.txt
-dontoptimize
-dontobfuscate
-dontpreverify
-dontwarn scala.**
-ignorewarnings
# temporary workaround; see Scala issue SI-5397
-keep class scala.collection.SeqLike {
public protected *;
}
From: hello-scaloid-gradle
Android comes with built in support for MultiDex. You will need to use
MultiDexApplication
from the support library, or modify your Application
subclass in order to support versions of Android prior to 5.0. You may still
wish to use ProGuard for your production build.
Using MultiDex with Scala is no different than with a normal Java application. See the Android Documentation and MultiDex author's Documentation for details.
It is recommended that you set your minSdkVersion
to 21 or later for
development, as this enables an incremental multidex algorithm to be used, which
is significantly faster.
build.gradle
repositories {
jcenter()
}
android {
defaultConfig {
multiDexEnabled true
}
}
dependencies {
implementation "org.scala-lang:scala-library:2.11.12"
implementation "com.android.support:multidex:1.0.3"
}
Change application class.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="jp.leafytree.sample">
<application android:name="android.support.multidex.MultiDexApplication">
</manifest>
If you use customized application class, please read next section.
To test MultiDexApplication, custom instrumentation test runner should be used. See also https://github.com/casidiablo/multidex/blob/publishing/instrumentation/src/com/android/test/runner/MultiDexTestRunner.java
build.gradle
android {
defaultConfig {
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
}
}
dependencies {
implementation "org.scala-lang:scala-library:2.11.12"
implementation "com.android.support:multidex:1.0.3"
}
Since application class is executed before multidex configuration, Writing custom application class has stll many pitfalls.
The application class must extend MultiDexApplication or override
Application#attachBaseContext
like following.
MyCustomApplication.scala
package my.custom.application
import android.app.Application
import android.content.Context
import android.support.multidex.MultiDex
object MyCustomApplication {
var globalVariable: Int = _
}
class MyCustomApplication extends Application {
override protected def attachBaseContext(base: Context) = {
super.attachBaseContext(base)
MultiDex.install(this)
}
}
You need to remember:
NOTE: The following cautions must be taken only on your android Application class, you don't need to apply this cautions in all classes of your app
MultiDex#install
be called! So the suggestion is to avoid static fields with types that can be placed out of main classes.dex file. override def onCreate = {
super.onCreate
val context = this
new Runnable {
override def run = {
variable = new ClassNeededToBeListed(context, new ClassNotNeededToBeListed)
MyCustomApplication.globalVariable = 100
}
}.run
}
This section is copyed from README.md for multidex project
You can configure scala compiler options as follows:
build.gradle
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"]
}
Complete list is described in http://www.gradle.org/docs/current/dsl/org.gradle.api.tasks.scala.ScalaCompileOptions.html
build.gradle
buildscript {
repositories {
jcenter()
maven { url 'https://jitpack.io' }
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.1'
classpath 'com.github.AllBus:gradle-android-scala-plugin:3.5.1'
}
}
repositories {
jcenter()
}
apply plugin: "com.android.application"
apply plugin: "jp.leafytree.android-scala"
android {
compileSdkVersion 29
defaultConfig {
targetSdkVersion 29
testInstrumentationRunner "com.android.test.runner.MultiDexTestRunner"
versionCode 1
versionName "1.0"
multiDexEnabled true
}
flavorDimensions "version"
productFlavors {
dev {
minSdkVersion 21 // To reduce compilation time
dimension "version"
}
prod {
minSdkVersion 15
dimension "version"
}
}
}
dependencies {
implementation "org.scala-lang:scala-library:2.11.12"
implementation "com.android.support:multidex:1.0.3"
}
tasks.withType(ScalaCompile) {
scalaCompileOptions.deprecation = false
scalaCompileOptions.additionalParameters = ["-feature"]
}