7

I created a new Android project in Intellij 14, then added Scala SDK 2.11.6 to it (scope provided was the only option that worked for me). The project runs fine if I don't use any Scala class. But once I use, say, string interpolation, as soon as the code is run, the app crashes with this error:

06-20 18:36:27.277    1995-1995/com.pcn.android.games.jacks D/AndroidRuntime﹕ Shutting down VM
06-20 18:36:27.289    1995-1995/com.pcn.android.games.jacks E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.pcn.android.games.jacks, PID: 1995
    java.lang.NoClassDefFoundError: Failed resolution of: Lscala/StringContext;
            at com.pcn.android.games.jacks.MyActivity.onCreate(MyActivity.scala:16)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "scala.StringContext" on path: DexPathList[[zip file "/data/app/com.pcn.android.games.jacks-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
            at com.pcn.android.games.jacks.MyActivity.onCreate(MyActivity.scala:16)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access$800(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
    Suppressed: java.lang.ClassNotFoundException: scala.StringContext
            at java.lang.Class.classForName(Native Method)
            at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
            at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
            at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
            ... 15 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

I use IntelliJ 14.1.3, Java 1.7.0_80. My proguard-project.txt file is empty, but this persists whether I turn on Proguard or not.

What have I done wrong that prevents Scala classes from being seen at runtime?

Phil
  • 5,595
  • 5
  • 35
  • 55
  • From the Scala On Android page, http://macroid.github.io/ScalaOnAndroid.html which build system are you using: sbt with Android plugin or Gradle with gradle-android-scala-plugin; based on your SO history - I'm guessing sbt with Android plugin but just to make sure. – Morrison Chang Jun 25 '15 at 04:39
  • @MorrisonChang actually it's IntelliJ 14 (not Android Studio) with whatever that's built in, not sbt or gradle. – Phil Jun 25 '15 at 15:35
  • Do you familiar with this [project](https://github.com/yareally/android-scala-intellij-no-sbt-plugin)? – Eugene Jun 29 '15 at 13:03
  • 1
    You are using the 'provided' scope, which means that the lib won't be in intellij's runtime classpath when you run the app, but compilation will succeed. You have to include the lib at runtime. Consider use SBT or Gradle for dependency management – Laurynas Tretjakovas Jun 30 '15 at 10:31
  • Do you have Scala SDK on your system? In addition to your SDK added in IDE. – Garry Jul 01 '15 at 08:46

1 Answers1

4

What have I done wrong that prevents Scala classes from being seen at runtime?

Hard to tell without looking into your build files and the way your app is building. I suggest you move to the Gradle build system and use one of the android-scala plugins, such as this one. I used it about 3-4 months ago in one of my apps and it worked as a charm.

Gradle-based builds will save you a lot of the time, because it's officially supported by Google and it's used by many people (thus you'll be more likely to get the help from community). It also supports some features not supported by Intellij-based build system, such as build flavors/splits and is constantly optimized by Google engineers to get the fastest build times possible.

aga
  • 27,954
  • 13
  • 86
  • 121