clojure-android / lein-droid

A Leiningen plugin for building Clojure/Android projects
Eclipse Public License 1.0
645 stars 56 forks source link

Using CardView throws `ExceptionInInitializerError` due to `RuntimeException: Stub!` at compile time. #150

Open rabipelais opened 8 years ago

rabipelais commented 8 years ago

If I run lein droid doall and the main file contains (defelement :card-view :classname android.support.v7.widget.CardView :inherits :frame-layout)

I get the following error:

Exception in thread "main" java.lang.ExceptionInInitializerError, compiling:(my/app/main.clj:17:1)
    at clojure.lang.Compiler.analyze(Compiler.java:6553)
    at clojure.lang.Compiler.analyze(Compiler.java:6495)
    at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3801)
    at clojure.lang.Compiler.analyzeSeq(Compiler.java:6735)
    at clojure.lang.Compiler.analyze(Compiler.java:6534)
    at clojure.lang.Compiler.analyze(Compiler.java:6495)
    at clojure.lang.Compiler.compile1(Compiler.java:7328)
    at clojure.lang.Compiler.compile(Compiler.java:7400)
    ...
Caused by: java.lang.ExceptionInInitializerError
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at clojure.lang.RT.classForName(RT.java:2174)
    at clojure.lang.RT.classForName(RT.java:2183)
    at clojure.lang.Compiler.resolveIn(Compiler.java:7010)
    at clojure.lang.Compiler.resolve(Compiler.java:6973)
    at clojure.lang.Compiler.analyzeSymbol(Compiler.java:6934)
    at clojure.lang.Compiler.analyze(Compiler.java:6516)
    ... 31 more
Caused by: java.lang.RuntimeException: Stub!
    at android.graphics.RectF.<init>(RectF.java:5)
    at android.support.v7.widget.CardViewEclairMr1.<init>(CardViewEclairMr1.java:27)
    at android.support.v7.widget.CardView.<clinit>(CardView.java:80)
    ... 39 more

note that compiling with RecyclerView or others produce no errors.

alexander-yakushev commented 8 years ago

From the stacktrace it seems like CardView contains static initialization that requires Android runtime. This is connected to the old-standing bug in Clojure: importing classes triggers their static initializers (see http://dev.clojure.org/jira/browse/CLJ-1315, http://dev.clojure.org/jira/browse/CLJ-1743).

The only thing I can recommend right now is to extend CardView in your project using Java (like, creating MyCardView.java class with just public class MyCardView extends CardView {}) and using it. As far as I understand the problem, it should work.

rabipelais commented 8 years ago

And then use defelement with :classname MyCardView?

alexander-yakushev commented 8 years ago

Yes.

rabipelais commented 8 years ago

Sadly, this didn't seem to work. Still getting the same error

alexander-yakushev commented 8 years ago

Meh, that's unfortunate. What if you replaced android.support.v7.widget.CardView in your code with (resolve 'android.support.v7.widget.CardView)?

rabipelais commented 8 years ago

Still the same error. It seems I will have to use a patched compiler.