In my android application I have to separately implement a certain functionality and needs to make a library file(.jar) out of it.
Main idea is then I can distribute that jar file, so that other applications can easily integrate this functionality using the jar file within their apps.
Following I have indicate the Minimum and Target SDK versions that are in the Manifest file.
android:minSdkVersion="7"
android:targetSdkVersion="15"
I know I can create a library project to implement that specific functionality and have a reference for it from my main project. And then to distribute the jar file that creates under the bin folder of the library project.
I have couple of questions reagrding this.
1) Since I didn't find any good tutorial explaining this thing, bit not sure if this is the way to go (Distributing the jar file creates under bin folder).
2) Also the jar file that creates under the bin folder of the library project is with the same project name(Eg:- LibraryProjectName.jar). Is it okay if I rename it for what I want before I distribute it?
3) Are there any other alternative or good ways of doing this?
Any help would be highly appreciated. Thanks in advance.
Since I didn't find any good tutorial explaining this thing, bit not sure if this is the way to go(Distributing the jar file creates under bin folder).
I wouldn't. That JAR is an artifact of consuming the library project and may or may not be suitable for third parties. Besides, if you really need an Android library project, the JAR is insufficient.
Is it okay if I rename it for what I want before I distribute it?
JAR names can be whatever you want.
Are there any other alternative or good ways of doing this?
First, do not create an Android library project unless you need to ship resources along with the code (or JAR). And, in that case, you will need to distribute the JAR and all the resources (and the manifest and pretty much everything else in the project).
Second, create your own JAR, such as by adding a <jar> operation to your Ant script. That way, you are in control over exactly what goes in there, how it got compiled, etc., rather than making assumptions about the JAR that the build system created as a by-product.
For example, here is a jar target from one of my CWAC projects:
<target name="jar" depends="debug">
<jar
destfile="bin/CWAC-EndlessAdapter.jar"
basedir="bin/classes"
/>
</target>
Related
How should external libraries be included into Android projects?
I see this documentation from Google:
http://developer.android.com/tools/support-library/setup.html#libs-with-res
...which says they should be kept outside the source tree for the project, and referenced as dependencies.
The guide for Facebook libraries says the same thing:
https://developers.facebook.com/docs/android/getting-started/facebook-sdk-for-android/
What about when the project is going into source control, and will be worked on by multiple developers? Is it possible to be sure other developers will have the correct versions of libraries if they're not included in source control?
It seems as though it might be better to check in the whole tree of these external libraries under say an "external" folder in the project and then reference them as libraries from there? The above links don't say this is wrong, but is there any reason not to do that?
I could not find anything against this approach, but maybe my search skills are off.
Thanks!
You have basically tree options (referring to git):
Putting the source or binaries into your git repository.
You can create/clone extra repositories and link these as submodule into your main repository.
Use gradle/android-studio to maintain remote binary dependencies.
In my opinion, option 3. is the best. It speeds up build time and reduces the date saved in your internal repository. Referencing most open source projects, googles libraries and even the Facebook API is just a one liner in your build.gradle file.
For internal libraries or anything not uploaded to some maven repository, you can create a local maven repository and link that.
And in the end, you have the option 2. to create a library submodule within git and gradle to handle it efficiently.
If you want to stick to eclipse + ant, try 2. first.
At least ant will work out of the box for building all things.
Setting up eclipse is a bit more difficult but can be done.
Option 1. is easy to implement, but It might get messy at some point.
Copy jar file in android project libs forlder and right click on jar file and click on bulid path-> add to build path.
If you want to add jar file then copy your jar file and put in to libs folder, and if you want to add external library then import your library project go to project properties and select android tab and add external library with add button.
I have a project that uses some resources.I want to create a library from it and publish it.I create a jar file with export option of eclipse,but it did not work.Then I search the we b and it seems that way works if and only if project does not use resources.But I saw this post.Here CommonsWare saya there is a way to create a jar file from a project that uses resources.But that answer has two link that do not open any page on the web and I could not test CommonsWare's answer.So my question is:
Is there any way to create jar library file from project that uses resources?
Note:
I read docs that say:
If you have source code and resources that are common to multiple
Android projects, you can move them to a library project so that it is
easier to maintain across applications and versions.
But as I said before,I want to publish my jar and docs say we can not create jar file from library project.And so I can not publish it.
Here CommonsWare saya there is a way to create a jar file from a project that uses resources.
Not in that answer. You can tell that by actually reading the answer.
But that answer has two link that do not open any page on the web
Sorry, Google reorganized their site and broke the original links. The answer has been updated with current links.
Is there any way to create jar library file from project that uses resources?
No.
You can create an Android library project that includes a JAR instead of Java source code. AFAIK, this recipe still works:
Create an Android library project, with your source code, resources, and such, and get it working
Compile the Java source (e.g., via Ant) and turn it into a JAR file
Create a copy of your original Android library project to serve as a distribution Android library project
Place the compiled JAR from step #2 and put it in libs/ of the distribution library project from step #3.
Delete everything in src/ of the distribution library project (but leave the now-empty src/ directory there)
Distribute the distribution library project (e.g., ZIP it up)
And the new Gradle-based build system supports the AAR package for distributing libraries and such, though I have not played with this yet.
I am working on an android library, and wish to export a JAR file that I can distribute for others to use in their apps. I don't want to distribute the source code as it contains details on posting to my web server.
I have tried using the JAR file that is created in the bin directory and copying the jar file to my project and referencing it within my project and ticking the export button.
When I try and run my project referencing the library that I've copied, my app throws an exception with NoClassDefFoundError. I've done some Googling and everything I have found suggests you have to provide the source code and let the user import into their IDE and then reference that project into their app which I don't want to do. It must be possible as other companies provide JAR files for libraries that can be included.
Thanks for your help.
I don't want to distribute the source code as it contains details on posting to my web server.
Bear in mind that anyone who wants to can get that data out of the JAR.
It must be possible as other companies provide JAR files for libraries that can be included.
AFAIK, this recipe still works:
Create an Android library project, with your source code, resources, and such, and get it working
Compile the Java source (e.g., via Ant) and turn it into a JAR file
Create a copy of your original Android library project to serve as a distribution Android library project
Place the compiled JAR from step #2 and put it in libs/ of the distribution library project from step #3.
Delete everything in src/ of the distribution library project (but leave the now-empty src/ directory there)
Distribute the distribution library project (e.g., ZIP it up)
This effectively gives you what you see with the Play Services SDK -- a library project with no source code, but instead a JAR in libs/, along with the resources and such.
I will be reconfiming this recipe tomorrow and will try to remember to update this answer if I find that it needs adjusting for the current crop of tools.
And the new Gradle-based build system supports the AAR package for distributing libraries and such, though I have not played with this yet.
UPDATE
This recipe works, so long as the library project does not itself have dependencies upon another JAR or library project. In those cases, things seem to get messed up in the build process -- everything can compile, but class references from the dependencies cannot be resolved at runtime.
Did you try putting your jar file in libs folder?And if you are exporting a jar library for android be sure it has no /res folder. As you know you can't reference to your res folder from a jar therefore you have to use library project to reference your res folder (drawable,xml,ect...)On the other hand you cant make your code safe (the part you say about posting to your web service) by using it as jar since it is so easy to retrieve by reverse engineering. you better use some encoding (like base64 or any algorithm that bouncycastle provides)
I am creating an Android library, and I would like to create a distributable jar without revealing source code. The library does not use resources like layouts, images, etc. I understand that I can just copy the .jar automatically generated in the bin folder of an Eclipse Android project if I check 'is Library' under project properties.
Is there anything else I should be aware of? How does the client project know which permissions my library requires. Should I include those permissions in the Manifest of my library?
I understand that I can just copy the .jar automatically generated in the bin folder of an Eclipse Android project if I check 'is Library' under project properties.
I wouldn't, as you don't know how that JAR was created, and you don't know how that JAR might change as the tools change. That JAR is a side-effect of Android's internal build process and is not designed to be a production artifact. Create your own production JAR yourself (e.g., Ant <jar> task).
How does the client project know which permissions my library requires.
You tell them via well-written documentation.
Should I include those permissions in the Manifest of my library?
Since your JAR does not contain your manifest, that will not help.
My objective is to create a single distributable jar file for Android projects which includes a few other jar files. As I understand a "standard" jar file is not allowed to have other jar files inside, so guess I need to learn a trick here.
I have been trying to set up One-Jar for this, but I keep hitting
issues. Are there any dev guides for using One-jar with Android projects (using Eclipse)?
Are there any other good alternatives out there I should look at?
jarjar is pretty good at this. When you use it together with Maven its super easy to make a large collection of dependencies a single jar.
Although, there's nothing stopping you including multiple jars as dependencies in an Android project.