clojure-android / lein-droid

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

loading custom resources at runtime via clojure.java.io/resource #85

Closed jimpil closed 10 years ago

jimpil commented 10 years ago

Hi again,

I have been trying to load my own resources since yesterday unsuccessfully! I literally tried everything...Then this morning I found out that I am supposed to put my .txt files under /res/raw. I did that, but I still cannot read any of the text files... Presumably I am expected to use on of these 2:

(clojure.java.io/resource "xxx.txt")
(clojure.java.io/resource "raw/xxx.txt")

but none works...both bring back nil...What on earth am I missing? I am trying to find the source for clojure 1.5.1-jb, in order to discover what resource does (I am expecting it is different than the official clojure 1.5.1) but again I cannot find it...It must be something rather simple I guess...

Thanks in advance, Jim

ps: I am by no means implying that there is a bug...I am simply asking for advice :)

jimpil commented 10 years ago

after more googling I found this: http://stackoverflow.com/questions/2856407/android-how-to-get-access-to-raw-resources-that-i-put-in-res-folder

The second answer hints that I need to do activity.getResources().openRawResource(resourceName) from my own activity. So I wrote this little function:

(defn resource++ [fname]
 (-> a
    .getResources
   (.openRawResource fname)))

But it still doesn't work!!! Doing org.jimpil.namegen.main=> (.getResources a) results in this: CompilerException java.lang.NoClassDefFoundError: android/os/UserHandle, compiling:(NO_SOURCE_PATH:1:1)

Any ideas?

jimpil commented 10 years ago

Ok, problem solved:

(defn- raw-resource [fname]
 (let [context (.getApplicationContext a)
       ress    (.getResources context)
       int-id  (.getIdentifier ress fname "raw" (.getPackageName context))]
     (.openRawResource ress int-id)))

I feel this or something like this should be included in neko...

Jim

alexander-yakushev commented 10 years ago

Hey Jim,

It is great that you were able to solve the problem. Thing is, as you've might already seen, Dalvik/ART is not JVM, which means some of the approaches you used to employ won't work here. Handling resources is one of them. I recommend you familiarize Android platform better before jumping into Clojure/Android development, this will save you a lot of headache and confusion.

For instance, instead of calling .getIdentifier, you should be able to use your.namespace.R$raw.fname to get the resource ID. There is currently no keyword shortcut for raw resources in Neko, but there's no reason not to add them, I will give it a look.

jimpil commented 10 years ago

Hi Alex,

Yes indeed...The more I look at dalvik the more differences I am finding. However, for some reason I was expecting that the patched clojure version (jb) that comes with lein-droid incorporated all that...anyway, I was obviously wrong as clojure.java.io/resource does not work for android development.

Yes, you are right I can indeed call your.namespace.R$raw.fname instead of .getIdentifier(). Which of two is more evident though, is debatable ... :)

thanks again

alexander-yakushev commented 10 years ago

Our patched Clojure version only enables dynamic compilation on Android. For other things, we don't make any effort to make Android Clojure look like JVM Clojure, as Google doesn't make effort to make Android look like JVM. If they don't have enough resources to do that, sure don't we. There are solid differences, and it is better to embrace than to fight them.

jimpil commented 10 years ago

Sure thing! I totally understand this...apologies if I sounded critical in my previous message - that was not the point...You guys have done excellent work for which I am eternally grateful :)

alexander-yakushev commented 10 years ago

No worries, you didn't sound critical! I was just explaining the reasoning behind some decisions. I am glad that you enjoy the tools, and good luck writing Clojure-Android applications!