3

I have an android application that has a dependent library I created, referenced in build.gradle via compile {artifact_name}. The library needs to access a resource file in its resources dir(src/main/resources). Here is what I want to accomplish, but can't:

URL url = helper.class.getClassLoader().getResource("myfile.json"); FileReader fr = new FileReader(url.getPath()); 

myfile.json is in src/main/java/resources of my library. this led to an app crash saying "jar:file:/data/app/com.my.app/base.apk!/myfile.json" not found when trying to create the FileReader.

However, it can be found if I run it in a client class in the library itself.

3
  • 1
    Android doesn't use jar file resources. Until recently (maybe a year ago) you couldn't do it at all- if your project had resources it needed to be included as source in your app. Now you can, but you need to use an android library module, and probably put the resources in there rather than in the jar itself. Commented Jan 15, 2016 at 0:56
  • Yeah, I'm not sure what you're gaining by trying to use JAR resources as opposed to just about anything else (Android resources, Android assets, downloading something from a server, etc.). JAR resources have been funky on Android from the beginning, and I rarely see them used successfully. Commented Jan 15, 2016 at 0:59
  • question revised to be clearer. Commented Jan 15, 2016 at 18:47

1 Answer 1

2

Updated (I have some more time to give full answer)

In your case ClassLoader of helper class have an access to your file (there are why you get "jar:file:/data/app/com.my.app/base.apk!/myfile.json" in your FileReader instance).

But FileReader don't understand full-qualified URLs (only URLs with scheme file: or without any scheme, but you have outer scheme jar: - file accessible from jar file).

To read data in this case you should1 use combination of InputStreamReader and Class.getResourceAsStream:

ClassLoader cl = helper.class.getClassLoader(); InputStream is = cl.getResourceAsStream("myfile.json"); Reader r = new InputStreamReader(is); 

PS in common situations you don't need access ClassLoader directly - Class will access it by itself. Only rare cases should use access to ClassLoader and in this situations you should really understand Classes and ClassLoaders very clear. I found out some usefull information: Classes method (both getResource and getResourceAsStream) checks file only in same package while ClassLoaders methods checks (additionally) at root path (as I remember some of them checks in both places and today I checked that Class don't check root of jar file).

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, but this does not work, the inputstream turns out null. ive updated my question be to clearer.
Hmm... I've found some additional info... In your case you rightly used ClassLoader's method... I'll write it in answer now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.