Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android + Scala + Intellij 13

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.

like image 989
Markus Marvell Avatar asked May 16 '14 13:05

Markus Marvell


People also ask

Does IntelliJ work with Scala?

To start working with Scala in IntelliJ IDEA you need to download and enable the Scala plugin. If you run IntelliJ IDEA for the first time, you can install the Scala plugin when IntelliJ IDEA suggests downloading featured plugins. Otherwise, you can use the Settings | Plugins page for the installation.

How do I enable Scala in IntelliJ?

Open IntelliJ IDEA, go to File Menu --> Plugins --> [ Or directly press Ctrl+Alt+S ] Click on "Browse repositories" button and enter "Scala". Select Scala plugin to install it.

Does IntelliJ Community Edition support Scala?

Support for Scala, SBT and Hocon is available for free in IntelliJ IDEA Community Edition, while support for Play Framework and Scala. js is available only in IntelliJ IDEA Ultimate.


2 Answers

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.
like image 146
Markus Marvell Avatar answered Oct 13 '22 01:10

Markus Marvell


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 *;
}
like image 23
adray Avatar answered Oct 13 '22 02:10

adray