Scala on Android with IntelliJ Idea ClassNotFoundException

I tried using sbt and maven for android-scala and I quite like maven for doing the whole release thing(ProGuard-ing the scala standar lib etc). But I want lightweight recompile cycle for development inside my IDE - IntelliJ IDEA.

The problem is scala files wont't compile. I configured my project like this:

  • created android project
  • added scala facet
  • set scala compiler to global
  • added dependency on scala library(global) and set it to "provided"

I added a scala object and referenced it from java Activity. ClassNotFoundException and force close. It compiles cleanly(and quite quckly).

I created a plain old java project the same way and using same global libraries and it worked. An inspection of the apk revealed it contained scala source(!) file. So apparently it didn't event try to compile.

Oh, and my phone has scala preinstalled with scala installer from play store.

I use IntelliJ IDEA 11 community. I first tried this a few months ago and it worked.

update it works on a different machine.

Answers


You might have to fiddle with your "proguard.cfg" file.

Here's the one I use from a project using Scala 2.10 and Android with the IntelliJ IDE (change the first option to match your package name):

##
## Keep classes of named package
##
-keep public class **.keithpinson.**
## ## ## ## ## ## ## ## ## ## ## ## ##


##
## RELEASE ONLY
##
##-optimizationpasses 3
##-overloadaggressively
##-repackageclasses ''
##-allowaccessmodification
##-optimizations !code/simplification/arithmetic
##         ,!field/*,!class/merging/*,!code/allocation/variable
##-keepattributes *Annotation*

##
## DEBUG ONLY
##
-dontobfuscate
-dontoptimize
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod
## -printmapping mapping.txt


##
## ALL VERSIONS
##
-dontpreverify
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontusemixedcaseclassnames


## Keep Limited Serializable Capability
##-keepclassmembers class * implements java.io.Serializable {
##    static long serialVersionUID;
##    private void writeObject(java.io.ObjectOutputStream);
##    private void readObject(java.io.ObjectInputStream);
##    java.lang.Object writeReplace();
##    java.lang.Object readResolve();
##}

## Keep All Serializable Capability
##-keep class * implements java.io.Serializable { *; }"





##
## ANDROID SETTINGS
##
-dontwarn com.google.common.collect.MinMaxPriorityQueue
-dontwarn com.google.common.primitives.UnsignedBytes$*

-dontnote android.app.backup.BackupAgentHelper
-dontnote com.android.vending.licensing.ILicensingService

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keep public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

# R classes may be built out of sequence so don't proguard them
-keep public class **.R { *; }
-keepclassmembers class **.R$* {
    public static <fields>;
}

# Removes all calls to Log. Delete the methods you want to keep.
-assumenosideeffects class android.util.Log {
    public static int v(...);
    #public static int d(...);
    public static int i(...);
    #public static int w(...);
    #public static int e(...);
    #public static int wtf(...);
}

## Fixes ==> Warning: ... can't find referenced class javax.xml.stream.events.XMLEvent
## -dontwarn javax.xml.**


## If using Guice
## -keep class com.google.common.base.Finalizable* {
##   <fields>;
##   <methods>;
## }
##
## -keep class com.google.common.base.internal.Finalizer {
##   <fields>;
##   <methods>;
## }


##
## SCALA SETTINGS
##
-dontwarn **$$anonfun$*
-dontwarn scala.android.**
-dontwarn scala.beans.ScalaBeanInfo
-dontwarn scala.collection.generic.GenTraversableFactory
-dontwarn scala.collection.immutable.RedBlack$Empty
-dontwarn scala.concurrent.forkjoin.**
-dontwarn scala.reflect.**
-dontwarn scala.sys.process.**
-dontwarn scala.tools.**,plugintemplate.**


#(org.xml.sax.EntityResolver)Class.forName(variable).newInstance()
-dontnote org.xml.sax.EntityResolver

#(org.apache.james.mime4j.storage.StorageProvider)Class.forName(variable).newInstance()
-dontnote org.apache.james.mime4j.storage.DefaultStorageProvider

-dontnote scala.android.app.Activity


## Keep All Scala Classes
##-keep class scala.** { *; }

-keep class scala.android.package**
-keep class * extends scala.android.app.Activity

## Fixes ==> Warning: ... can't find referenced class sun.misc.Unsafe
-libraryjars libs/jsr305-2.0.1.jar
-dontwarn sun.misc.Unsafe


-keep class * extends scala.runtime.MethodCache {
    public <methods>;
}

-keepclassmembers class * {
    ** MODULE$;
}

-keepclassmembernames class scala.concurrent.forkjoin.ForkJoinPool {
    long eventCount;
    int workerCounts;
    int runControl;
    scala.concurrent.forkjoin.ForkJoinPool$WaitQueueNode syncStack;
    scala.concurrent.forkjoin.ForkJoinPool$WaitQueueNode spareStack;
}

-keepclassmembernames class scala.concurrent.forkjoin.ForkJoinWorkerThread {
    int base;
    int sp;
    int runState;
}

-keepclassmembernames class scala.concurrent.forkjoin.ForkJoinTask {
    int status;
}

-keepclassmembernames class scala.concurrent.forkjoin.LinkedTransferQueue {
    scala.concurrent.forkjoin.LinkedTransferQueue$PaddedAtomicReference head;
    scala.concurrent.forkjoin.LinkedTransferQueue$PaddedAtomicReference tail;
    scala.concurrent.forkjoin.LinkedTransferQueue$PaddedAtomicReference cleanMe;
}

Seems idea will use the system proguard configuration to optimize the jar when uncheck the Run ProGuard on Android facet, simple uncheck it seems don't work, so you need use some customized config for proguard, just try this, it work for me, please replace REPLACE WITH YOUR PACKAGE with you package

Of course, you may try Keith's config.

-dontobfuscate
-dontoptimize
-dontpreverify

-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers

-dontwarn

#scala.Enumeration accesses a field 'MODULE$' dynamically
-dontnote scala.Enumeration

#(org.xml.sax.EntityResolver)Class.forName(variable).newInstance()
-dontnote org.xml.sax.EntityResolver

#(org.apache.james.mime4j.storage.StorageProvider)Class.forName(variable).newInstance()
-dontnote org.apache.james.mime4j.storage.DefaultStorageProvider

-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
            SourceFile,LineNumberTable,Annotation,EnclosingMethod

-keep public class REPLACE WITH YOUR PACKAGE

https://plus.google.com/106066742926663025812/posts/41XoDPeDEqc


Need Your Help

JQuery library module export

javascript jquery module umd

I am trying to understand how jQuery sets itself up.

Unable to use gradient along with image in CSS

css css3

Unable to use gradient along with image.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.