Creating jar Library of Android project? - android

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.

Related

Shared Library On Android Studio

I'm switching from Elcipse to Android Studio. In Eclipse I have some "library projects" (no jar files, but Android projects marked as "library") that are used by several apps. In Eclipse I used to have this library project in only one place, and I liked to modifiy the library only once, and see the effect on all the other projects depending on it.
In Android Studio, when I add a library module, all the code is copied inside the app. I don't like it, because it's easier to maintain having it only in one place. Is there any way to reference the original library instead of copy it inside the project?
I've been reading a lot of about this, but I can't find the right solution. These are the ways I'm thinking to do it:
a) Aar files and local maven repository: I would compile the library module into an aar file, and I'll save in an local maven repository, so I can reference it on my projects. But this has a drawback: every time I want to modify the library, I need to recompile to an aar file before I can test the changes in my project.
b) Symbolic links: Instead of copy all the library module inside the app folder, I'm thinking in creating symbolic links to the original folder. So the library will stay in only one folder. But I think this approach is so tricky, and there must be a better way to do it.

Don't want to manually copy android JNI(.so) and Jar libraries to App project each time

I'm building and Eclipse to make an Android library I want to distribute to developers.
TedLibJni:
It uses the Android NDK and so it compiles down to a .so file.
TedLibJar:
It also has a Java interface that binds the then extern'd calls in the JNI, so it has a Jar library associated with it.
TedDroidApp:
The concensus is that I need to manually copy both TedLibJni.so and TedLibJar.jar to lib/armeabi of this App for it to be used.
Question: Is there any way that TedDroidApp can pick up the externally located .so or .jar files? It seems crazy that I would have to manually copy and paste these files accross each time I iterate them.
Use an Android library project for the JNI code and the JAR. You can then attach the Android library project to other projects. With the new Gradle-based build system, you can package the library project up as an AAR and obtain it from an artifact repository as well.
CWAC-AndDown, my wrapper around the C hoedown library, works this way.

How to include external libs in Android source controlled project?

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.

Create a JAR for Android Library Distribution

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)

How to distribute an Android Library

I've been spinning a jar for android library project and including this jar in my other apps. But on developer.android.com, I see this statement that I can't distribute a library in a jar:
You cannot export a library project to a JAR file
A library cannot be distributed as a binary file (such as a JAR file).
This will be added in a future version of the SDK Tools.
I really don't understand what does that mean.
It is possible to create an Android library project that does not
include source code. The limitations are:
You still have to ship the resources.
You have to rewrite your code to avoid using R. values, as they
will be wrong. You will have to look up all resource IDs using
getResources().getIdentifier() and/or reflection.
I have the instructions in The Busy Coder's Guide to Advanced Android
Development (http://commonsware.com/AdvAndroid), though the
instructions are new enough that none of my free versions have them.
Quoting some of the instructions from the current edition:
"You can create a binary-only library project via the following steps:
Create an Android library project, with your source code and such –
this is your master project, from which you will create a version of
the library project for distribution.
Compile the Java source (e.g., ant compile) and turn it into a JAR file
Create a distribution Android library project, with the same
resources as the master library project, but no source code
Put the JAR file in the distribution Android library project's libs/
directory
The resulting distribution Android library project will have everything a
main project will need, just without the source code."
Personally, I'd just wait a bit. I am hopeful that the official
support for library-projects-as-JARs will be available soonish.
It means that (at the current time) you must distribute your entire project folder. Rather than just a jar file like you can for java libraries.
When someone wants to use your library they will import your project into eclipse, and then in project properties->android they will add your project as an android library.
A few common ways used to distribute a Library project are by using git, or zipping your project folder and making it available online.

Categories

Resources