Recently I saw it is possible to include assets in the Android Library with the update on November 2018 on the Official documentation here:
https://developer.android.com/studio/projects/android-library
But it just describes briefly that "assets" is an optional library that could be included in the Android libraries. It does not cover the way how to access them.
Right now I have a project with several Android libraries. Each library contains a level or it is contained on a level of abstraction. In order to have good isolation, ideally each library should be able to use the code and assets which is contained by its own Android library.
So, my question is, since now we can include assets, in my case a json file, inside of the Android library, is there any way to access those assets directly from the code of the Android library itself, without using the code from the app library?
Thanks in advance
P.d.: Here is the source code of my project if anyone is interested:
https://github.com/jiahaoliuliu/chutoro/tree/feature/companiesList
Looks like you want to use the AssetManager.
After importing
import android.content.res.AssetManager;
and initializing it
AssetManager assetManager = getAssets();
you can load assets by using
InputStream input = assetManager.open("PersistentDestinations.json");
Please note that you have to convert the InputStream in order to pass it to e.g. a JSONObject. (e.g. see here)
Related
I am currently creating an android library that uses json commands for communication with another library. I would like to extract these commands from code and save them in separate files for better readability.
Where do I save those files when there is no asset directory and how do I read them?
Edit: I have found an answer to my question:
how to access resources in a android library project
Have a look at Internal Storage. The files saved here are only available to your app. It uses the Java File API to read an write using the FileInputStream and FileOutputStream
UPDATE:
As per the discussion in the comments, OP was looking for a method to ship a JSON file with the library. In light of that:
I'm not sure if library modules support raw resources. If they do, you might want to use that but it will increase the size significantly. You could also fetch the file from a server the first time you're the library is used, keeping track of that using a SharedPreference entry.
I know there are two well-known folders where one can put resources.
The first is the /assets folder, the documentation says:
Contains file that should be compiled into an .apk file as-is. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
The second is the /res folder, the documentation says:
Contains application resources, such as drawable files, layout files, and UI string. See Application Resources for more information.
Now i was wondering, there is a third option to create a resource folder "New > Folder > Java Resources Folder"
To me i looks like some part of the Android Plugin in IntelliJ because it has a little Android symbol in front of it .
Any ideas what the use of it could be? I couldn't find any documentation about it.
My first guess would be to use it in situations where you want to supply resources to a JVM Test.
In standard java world
Resources can be embedded directly in "your source tree" and used with Class's method getResource (see java documentation for a more precise description).
In android world
This practice is not recommanded (do not work at all, because such resources are removed from generated APK). You can still declare Java resources folders (see build.gradle :
sourceSets {
main {
resources.srcDirs = ['src/main/java/yourresourcesfolder']
}
}
And the result in the apk :
Conclusion
As resources's folder tree is removed,using java's resource folder become from my point of view, useless (in an android projet). Using android's asset folder is a better choice (also avoid resource's name conflict, but it's an another story :D )
This post is a bit old, but I want to bring an answer that explain one use case for the java resources directory on Android. If this folder exists in the options of a project it's because something can be done with it...
Enters the Service Loader, that helps to connect other services (aka libraries) into your main app, it can be used as a Gateway for your Android library to extend the functionality of a feature, exposing only the interfaces or abstract classes, with the Service Loader providing the implementation instead of your project having direct access to them.
Here is an example of how it's implemented. So, in summary the folder (META-INF/services) and files you have to create in order to use the Service Loader on an Android App, NEED to be inside this Java Resources Folder, otherwise your provider won't be able to see any implementation.
You can see it working in my sample here if you want to check it out: https://github.com/difereto-globant/test-library-feature/tree/1.0.9.
We have a c++ project on android, which will be built into .a then release the .a to our users.
The problem is, we have many resource files, and these resources' path is necessary because some 3rd-party libs need them.
The limitation on android is: if we want to put a file in Android APK, we must package it in res or assets and it is not single file any more, so how can we organize our resources?
Our plan is to require our users to package our resources in APK's Assets, then we can extract them to other folder, but it's not user-friendly, do you have any better idea?
thanks
C++ android project, it sounds me weird, may be, you would like to say that you have c++ arm crosscompiled static library .a which it is able to load in an Android project.
I guess you want to share a library which uses many resources, and you need the elegant way to distribute.
Try to create a .jar file which it has native functions:
Firstly create your core class exporting functions and define your
public native functions to JNI.
Don't use activities, if you need to access to activity context, pass
through parameters.
If you want to use resources, your layouts will not be able to use in
your parent android project.
You can use drawable files and you can add layout files programatically (with activity context).
If you are going to use some resources such as xml config files, put
in the res/raw folder or in assets folder.
If you have good Java and JNI skills, this task is not so difficult.
Hope the advices help you.
Cheers.
I think you need to develope a NativeActivity. If you follow the proper documentation, you could create a working app to do whatever you want. If you want to open assets, process, you can do it, and launch this app from another one.
If you really want a single library to be used, Just do it in c/cpp, and initialize it with a JNI function call providing the asset path and aassetmanager, and use the AAssetManager object in native. You will need to include jni.h, of course you will need to crosscompile the library using the NDK toolchain, and gradle or cmake toolchain scripts.
I want to know if we can create just one Localization folder for my application in MVVMCross (Android, IOS, Core) to change the language.
In fact, i watching this video/tuto to change my Application Language with MVVMCross: https://youtu.be/VkxHtbJ_Tlk
I downloaded the sample too and i can see, his folder BabelResources is in his Android project, in his IOS Projet, in WPF etc...
I can't create just one ressource folder and create just one JSON file with my translation (In my PCL for example)
Thank for your help
http://opendix.blogspot.dk/2013/05/using-resx-files-for-localization-in.html
or`
http://enginecore.blogspot.dk/2013/09/localization-in-xamarin-mvvmcross_18.html
or
http://blog.giovannimodica.com/post/internationalization-of-a-cross-platform-application
Hope one of them can help you on your way :) Alternatively, you could add the translation file in for example the android project and add it as link in the ios project.
Here's an alternative using simple text files in your PCL: https://github.com/xleon/I18N-Portable
The video tutorial you're referring to is using linked files in other projects. So you setup up only one resource file and use it in all the platform projects by linking to the file (instead of copying it)
You can find the source here: https://github.com/MvvmCross/MvvmCross-Tutorials/tree/master/Babel
First of all I'd like to mention I am very new to Java programming. This Android project is my first experience with Java. So for one task I need to strip HTML tags from a string. I was thinking about using this answer Remove HTML tags from a String to do this task. In order to do this I need to be able to import javax.swing.text.html.* and javax.swing.text.html.parser.*.
I went to the properties of my project and attempted to add this library to my project. I read that this was included in the rt.jar file which was located at /usr/lib/jvm/java-6-openjdk/jre/lib/rt.jar
I was able to use the imports but it seems like it added a million libraries I am not using. Will this negatively effect my application? What is the correct way to add just the one extension?
You don't. Android doesn't use Swing. You'll need to use Android's API to create your UI and perform text parsing. Consider android.text.Html as a simple way to strip HTML from a string.