Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android/Scala project in IntelliJ 14 compiles, but crashes when launched not finding Scala class

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?

like image 496
Phil Avatar asked Jun 20 '15 22:06

Phil


1 Answers

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.

like image 91
aga Avatar answered Oct 13 '22 23:10

aga