I am trying load .json files from resources so I can use them in unit tests.
I have created a resources file and marked it as a ‘resources root’ directory, and placed my .json files within it. As the files are to be used for tests only, I do not wish to place them in the /res directory as it unnecessarily bloat the app.
I am attempting to open the resources file with the following code, however inputStream returns as null.
InputStream inputStream = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("file.json”);
I have set the following in the .iml file that should denote the directory as resources but it seems to have had no effect.
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
Does anyone have any ideas where I maybe going wrong? Thanks in advance!
you can use assest folder or raw folder under resource folder
When the APK file for the app is built, it does not include directories that you create outside of "res" and "asset". Only files inside the APK are deployed to the device or emulator when testing. So there is no way to test a file that you do not include in the APK.
Files that are placed in the tests directory do not seem to be accessible. So AFAIK, you'll have to put the file in the app APK and then remove it later. Or, even better, it is possible to use gradle to produce a version of the app APK for testing and a different one for release. The release version could exclude the file.
Related
I followed This thread
and copied "MyFile.txt" file to data/data/package/files/ through DDMS>File Explorer and hit run. The emulator displays the content of file, but, when I copy apk and run on device, it throws an exception java.io.FileNotFoundException: data/data/package/files/myfile.txt failed ENOENT.
Is it because data/data/package/files/myfile.txt is not copied to apk file?
I tried res/raw/myfile.txt it works well.
Also, if there is any way to read a file which is in the root?
Thanx.
Yes, your file wouldn't get copied to the apk this way.
The data directory is the runtime storage area for the app. It only gets created after you install your app on a device/emulator.
So, this kind of packaging your data would never work.
One way to package your data is by putting it into the assets folder. However, if you would like to edit/change the file once the application is installed, this method also would not work for your situation.
But, if you just want a read only version of this file, putting it into assets folder would work fine.
I have a requirement to dynamically extract the content of a Jar file to a local directory. Remaining part of application will use these content. Everything working well in my eclipse development environment. However following peace of code returns null when it comes into JNLP launch.
InputStream stream = VLCLibManager.class.getClass().getClassLoader().getSystemResourceAsStream("XXX.jar");
I already did following :
Manifest file of JAR that contains VLCLibManager.class updated with proper class-path entries
My XXX.jar is placed under /lib directory of JNLP. It's getting downloaded correctly
Have the entry (jar href="lib/XXX.jar"/>>) in XYZ.jnlp file
Any help appreciated as I'm stucked with this issue for the past few days.
Finally I resolved the issue. Thought of publishing here as it would help others who face similar issue.
I did below :
I packed resources that I wanted as a Zip file and placed into /resources directory of my maven project
Maven compiler plugin packs this zip file along with resultant jar.
So I can load the zip file to my java code using
YourClass.class.getResourceAsStream("/XXXX.zip")
This loads resources to java program. You can unzip as you wanted and use it whereever needed
I have planned to create an SDK in android, that means to share my class files and resource(layouts) to another application that requires them. If I export my application as JAR it does not include the resource files and R.class. I know currently Android does not support to packaging resource file via JAR but I searched the net and found one solution if compile our resource files with source code means we can access the resource in some other application.
So for that reason I did it in the following way:
copied all res folders content into my source folder
src
->com.mypackege
-> res--->here i put my res folder
finally my src folder look like
src
com.mypackage
>myfile.java
drawable-hdpi
drawable-ldpi
drawable-mdpi
drawable-xhdpi
com.mypackage.drawable
com.mypackage.layout
com.mypackage.menu
com.mypackage.values
and i build my project i got all class files including resource and
r.class in my bin
then i create jar manually by jar -cvf mysample.jar com/mypackage/*
the final jar contain all classes and resource of my project in side
mypackage.
then I add mysample.jar into another application say testapp, here I put the JAR in asset folder(because of layout )and add to build path. And also I configure the build path
In this application I call the myfile.class from mysample.jar which is needed to load the layout from that JAR.
The problem is here it doesn't see resource under the JAR file it only see the resource in current project res folder and though NULL pointer Exception
But it calls the R.java from JAR file. Ad also i tried to put xmlns:custom="http://schemas.android.com/apk/res-auto" in my manifest file it is also not working.
I hope I exactly come to close this issue, it may be a resource mapping problem.
do you have any idea to resolve this?
You can't store resources in jar. It's possible, but it's tricky, so for now Android provides just Android Library. Otherwise you can use compiled source code and resourses separately (by providing resources ID's to your library)
I have an android app project that uses an android lib project from third party (don't have source). The "ant clean release" generates the apk and the app runs well.
Now a java property file is added into the lib project, locates under the root of the project folder. Build and install apk, log shows the newly added property file is not loaded. Unzip and dex2jar the apk, cannot see the property file from anywhere. So seems like the property file is not included in the apk build. So my question is how to include the property file into apk, should I do anything extra? Thanks for the help.
If you can configure the location where the 3rd party library reads the config file from, you can put it in res/assets. The files in this directory will be packaged into your .apk file and installed as files in the Android file system.
I had the excact same problem. The solution i found usefull is explained here:
http://www.maxters.net/2012/08/android-source-apk-x-res/
You will have to modify 3 files under build/core, then you can add any file/folder to your apk after updating your Android.mk file.
I want to add DLL's, images, textfiles etc to my project as resources so when I export it, the jar contains the resources so they can be used. I am using eclipse.
Problem is I have no idea how to add it. I've tried adding DLLs/pics to the src folder in the project, but when I export the jar, it is not located there
I've looked at How to make a JAR file that includes DLL files? but it only explains how to extract it, not how to add it to the project and build.
EDIT: I am using an applet to open the jar by the way, sorry for missing it!
Cheers
How are you opening the file in java?
Class.getResourceAsStream(name)?
If you are packaging the code in a jar, then you need to use that command. (as opposed to new File(name), which will get the file in the same directory as your jar)
If the file is not physically in your jar, you can check by changing .jar to .zip and extracting it, then check out this doc http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html
Usually in an eclipse project, the src folder is the wrong place to put non-sourcecode-content.
You should try moving to maven as your build system, as it is highly customizable and provides you with folders inside your project for exactly that purpose. (src/main/resources)