Packaging Android Resource Files Within a Distributable Jar File

Packaging Android resource files within a distributable Jar file

I don't have any optimistic news for you. In general you cannot distribute your own package as easily as you would expect. The main problem is the autogenerated R file. You probably make some references to it - you have your layouts and drawables. User of your library will have his own R file - containing ids for his resources, not for the ones your library provides.


In case of drawable resources you could read them in classic way, without R file. But as far as I know you are not able to inflate layouts from external xml files.

So the ugly hack here would be to distribute your library together with all your resources, which user would have to copy to his "res" folder. To be honest, there is no good solution to your problem... sorry.
Regards!

Packaging drawable resources with a JAR?

How can I distribute a JAR with drawable resources, and have them appear in the dependent apps?

That is not presently supported. Creating Android library projects that can distribute resources is on the tools roadmap and will hopefully come out in the not-too-distant future.

How to distribute the android reusable code in a package?

As Nic Strong noted, there is nothing much built into the project to manage this, and current indications are that the upcoming changes to the SDK tools may only help a bit.

I am organizing some other tools to help deal with this problem. I hope to have developer documentation published in a few days.

Android Library : Include properties file within a distributable JAR file

If you want to access a resource file like that, you will probably want to build an aar instead of a jar. The aar includes all the resources, assets and manifest.

However, consider just defining the variable in your build.gradle and reference it directly from there.

Try using a buildConfigField instead

mylibrary/build.gradle

release {
buildConfigField "String", "LIB_VERSION", "1.0.1"
}

Then access it in code as

Log.d(TAG + ":" + BuildConfig.LIB_VERSION, "Something happened");

Java library (.jar) with resources in Android project

Unfortunately it can't be done. "In general you cannot distribute your own package as easily as you would expect. The main problem is the autogenerated R file. You probably make some references to it - you have your layouts and drawables. User of your library will have his own R file - containing ids for his resources, not for the ones your library provides." (from This SO answer)

How can I package a resource file into a runnable jar

You can not access a resource in a Jar file through a FileReader because it is not a file on the file system. Java does have a mechanism for accessing resources in your classpath (including in Jars): ClassLoader.getResourceAsStream(). You can use it with your default classloader:

BufferedReader br = new BufferedReader(new InputStreamReader(
ClassLoader.getSystemClassLoader()
.getResourceAsStream("test.txt")));

Note that getResourceAsStream returns null instead of throwing an exception, so you may want to get the stream first, check if for null, then either throw an exception or process it.

You will want to give the full path within the jar as your path.

Best way to package reusable code with drawable and string resources

Apparently the only solution is to distribute the other code as an Android project, then to reference that other project in Eclipse. Of course, that makes versioning and collaboration more difficult--my guess is that a team would need to establish a naming convention for these third-party projects they use in their workspaces. Perhaps add a version number to the project name, so that one app can use a more recent version of the library without breaking apps using an older version?

Android Jar libraries

Google just release a new version of the SDK that handles Shared Libraries!

https://developer.android.com/tools/projects/projects-eclipse.html#SettingUpLibraryProject

ADT 0.9.7 (May 2010)

Library projects:
The ADT Plugin now
supports the use of library projects
during development, a capability that
lets you store shared Android
application code and resources in a
separate development project. You can
then reference the library project
from other Android projects and, at
build time, the tools compile the
shared code and resources as part of
the dependent applications. More
information about this feature is
available in the Developing in Eclipse
with ADT document. If you are not
developing in Eclipse, SDK Tools r6
provides the equivalent library
project support through the Ant build
system.



Related Topics



Leave a reply



Submit