How to include external libs in Android source controlled project? - android

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.

Related

How to make libs editable? Possible? (coding amateur)

I use the achartengine libs and downloaded the achartengine-1.0.0.jar from google. I imported the library into my code and recognized that I can ONLY read it but I'd like to apply some patches. How can I make a library editable?
I use Android Studio.
In the jar-File are only the compiled .class-Files, no .java-Files.
If you want to edit or patch, you need the src-.java-Files. Here you can check out the SVN-Project:
https://code.google.com/p/achartengine/source/checkout
Here you can see the files, which you would need, if you want to edit the library:
https://code.google.com/p/achartengine/source/browse/#svn%2Ftrunk%2Fachartengine%2Fsrc%2Forg%2Fachartengine%253Fstate%253Dclosed
Most Android Libraries have a pom.xml file which means that it should be built by maven. Install it, and navigate to the directory containing that file. Then mvn install. Reading the output, you will see where the artifacts(the jar) is stored.

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.

Creating jar Library of Android project?

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.

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)

Creating a single distributable jar file for Android projects which includes other jar files

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.

Categories

Resources