18

Android is great platform. Scala is great language. Intellij Idea is great IDE.

How all of them can work together?

Note: It's a self answer. But if you have more info, please share it here.

1

3 Answers 3

26

Yes they can.

Prerequisites: Scala SDK installed. Scala Intellij plugin installed. Android Intellij plugins enabled.

No SBT and third party plugins needed. No Gradle. Gradle support is not yet compatible with Scala plugin. Inform me if it is.

  1. Create new project: In Intellij > New project > Android : Application module > ...Name project, Select API level, Create MyActivity as suggested > Finish
  2. Create Scala Library: Open Project Structure > Platform Settings > Global Libraries > New Global Library > Java > Browse Scala SDK /lib > Select scala-library.jar [Optionally add link to docs and sources]
  3. Create Scala Compiler Library: Open Project Structure > New Global Library > Java > Browse Scala SDK /lib > Select scala-library.jar, scala-compiler.jar, scala-reflect.jar
  4. Add Scala to project module: Open Project Structure > Modules > Expand [your main module] > + Add > Scala > Select "Compiler library" previously added (should be listed in drop down list)
  5. Add Scala Library to project Module: Open Project Structure > Modules > Select [your module] : Dependencies > + Add > Library > Select "Scala-lib" (previously added to Global libraries)
  6. Add proguard (lately discovered that Proguard is not required for testing anymore): Add the following to your proguard-project.txt > Scala Proguard. Open Project Structure > Modules > Expand [your main module] > Android : Proguard > Enable "Run Proguard" > Add your proguard-project.txt (having default Android SDK android-proguard.txt)
  7. Create Scala Activity class: (Note: You already have the Java version of MyActivity created by template: /src/[your_package]/[MyActivity.class]). Right click /src/[your_package] > New > Scala class > Name it [MyActivity] > Ok. Copy all code from java MyActivity.class into newly created MyActivity.scala > Intellij suggests to "Convert code from Java" > Ok. Now you have fully functional MyActivity.scala. Remove java MyActivity.class (without safe delete option) > Check AndroidManifest.xml still linked to MyActivity.
  8. Enjoy: Run you app in emulator or device. If something goes wrong (as usual) please review your steps. Than think yourself. Than ask help.
7
  • Maybe you could structure your answer a bit better (sub-items). It's more like a wall of text.
    – ziggystar
    May 16, 2014 at 13:57
  • @ziggystar, why don't use structure it yourself at your machine? you'd use it only once, the next time you won't need to read it hear again because you'll remember it.
    – Incerteza
    May 16, 2014 at 14:05
  • 1
    2,3 and 4 can't be done the same way in Android Studio. How can I do that?
    – Incerteza
    May 16, 2014 at 16:04
  • 1
    @Alex I would recommend to use Intellij Idea 13. Android Studio uses Gradle by default which is not compatible with Scala plugin yet. May 16, 2014 at 22:44
  • That makes sense. However, the question remains.
    – Incerteza
    May 17, 2014 at 0:18
7

I use successfuly Scala 2.11.7 + Android Studio + Gradle with this plugin gradle-android-scala-plugin and current config:

build.gradle

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven {
            url "http://saturday06.github.io/gradle-android-scala-plugin/repository/snapshot"
        }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.1'
        classpath 'jp.leafytree.gradle:gradle-android-scala-plugin:1.3.2'
        classpath "org.scala-lang:scala-library:2.11.7"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

app/build.gradle

apply plugin: 'com.android.application'
apply plugin: "jp.leafytree.android-scala"


android {
    compileSdkVersion 22
    buildToolsVersion "19.1.0"
    defaultConfig {
        applicationId "com.example.adray.myapplication"
        minSdkVersion 15
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }

    lintOptions {
        abortOnError false
    }

    sourceSets {
        main {
            scala {
                srcDir "src/main/scala" // default: "src/main/scala"
            }
        }

        androidTest {
            scala {
                srcDir "src/androidTest/scala" // default: "src/androidTest/scala"
            }
        }
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }

        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.github.satyan:sugar:1.3'
    compile 'org.scala-lang:scala-library:2.11.7'
    compile 'com.google.android.gms:play-services:7.5.0'
    compile 'com.android.support:appcompat-v7:22.2.0'
}

repositories {
    mavenCentral()
    jcenter()
}

proguard-rules.pro

-dontoptimize
-dontobfuscate
-dontpreverify
-dontwarn scala.**
-dontwarn android.support.v4.app.**
-dontwarn android.support.v4.view.**
-dontwarn android.support.v4.widget.**
-ignorewarnings
# temporary workaround; see Scala issue SI-5397
-keep class scala.collection.SeqLike {
    public protected *;
}
0
0

See this discussion for more opinions about this question.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.