Can I use project libraries in the library project? - android

I have a main project, it has some libs (like library-x, library-y, library-z) and a library project. In the library project I import the same libraries with the same versions of the main project (for example library-x and library-y).
When I compile the project, in my application will it duplicate my libraries or adt will take only one library if it is the same project?
Is there any way to use the project's libraries in the library project?
Thank you.

If I understood your question correctly, you can just include your dependencies in the library project and then let the library project export its dependencies. The main project then only includes the library project, but also can access its dependencies.
This can change depending on your development environment, however. With Gradle, for example, you can safely add multiple libraries from Maven and it will automatically remove duplicates.

Related

Migrating from Eclipse to Android Studio and library issues

I have decided to migrate my projects from Eclipse to Android Studio and I am finding a lot of difficulties in doing so. The biggest problem I have is the following:
I have several applications that are using some common projects (libraries). When importing an application into Android Studio, it detects the referenced libraries and adds them to the application (copies the folders of the libraries under the application folder). This is something I do not want to do because the libraries are common for more that 1 applications so I want them to be at the same directory level as the application folder and just reference them from the application. How is this done in Android Studio? In Eclipse, you would just add a reference from the project properties. Furthermore, how do I import a library project in Android Studio? I would like to import each of the libraries as separate projects and build them individually.
Is there any good tutorial about this? I have a lot of experience with Eclipse but I am completely new to Gradle.
Here is an example of what I mean:
I have two projects, ProjectA and ProjectB. They both use some library projects developed by me, LibA and LibB. All 4 projects are under the same folder called Applications - the two libraries are therefore common and if I change one of them I only need to recompile both projects. After importing ProjectA into Android Studio (having the latest version), the import process created the following structure. Under the base folder, Studio Applications, I have a folder for the project named ProjectA. Under folder ProjectA, I have another folder, ProjectA (where the main project files are) and two more folders, LibA and LibB. What I would like to have is have the two library projects imported separately (as library projects in Android Studio) and under the main application folder have only the ProjectA folder (and the rest of the standard gradle folders, ie, .gradle, .idea, build etc).
How can this be done? I need two things: First, import a library project from Eclipse into Android Studio and then import a project but instead of having the libraries copied under the project path, reference them.
Just import your apps with no project libs (I'm assuming the libs are available in maven)
After that you just need to add the dependencies in gradle file
// your gradle file inside your app folder
dependencies {
compile 'com.squareup.okhttp:okhttp:2.3.0' // example of importing okhttp
compile 'com.facebook.android:facebook-android-sdk:4.1.1' // example importing facebook sdk
compile project(':name_of_local_project') // local project
}
and if you want to include a project of another folder (different project)
add this to settings.gradle
project(':module1').projectDir = new File(settingsDir, '../another_path')
It's a simpler approach, and you get the ability to update the lib without much work
The problem was that I had proguard enabled for the library modules as well. Proguard was obfuscating class and function names in the library modules and they were not found in the app that was referencing them. Moving/merging the proguard files in the app was the solution.

Project libraries which include jars (such as support v7 and play services) aren't working correctly

I switched over to Android Studio a few months ago, but only until recently did I ever add or remove library modules from my project. I've run into a problem where simply including the library module (such as google play services) is insufficient in getting my app to compile.
I made sure the library module uses its own jar as a dependency. The references jar can be seen here:
But unless I include both the library project AND this jar library onto my main app module as a dependency, the app will not compile. I was under the impression that just including the library itself ought to be enough, because it includes all the res/ files AND the jar itself which contains the java files.
I get it to work by doing the following:
But shouldn't just the 1 project library/module be enough? If you look you can see I need to do the same with Android v7's appcompat library.
You don't need to add the the compiled module (the .jar file) in the dependencies of itself module.
In other words, once you import your module to your project, the only other thing you should have to do is to add it as a module dependency to your project.
When you add the module library, Android Studio will automatically generate/add the .jar file to libraries and then to your project's dependencies.
Module Dependencies
Google Play Services Library
Main Project Module Dependencies

Android Dex: UNEXPECTED TOP-LEVEL EXCEPTION: Already added

My app depends on a library project. This library project depends on the Android Compatibility Package V4. I have NOT exported the library project's dependency. In my own project, I've added ACL V13 as a dependency, but when compiling I get the error that essentially there's a duplicate dependency. I thought not exporting the library project's dependency would resolve this issue, but it's not.
How can I resolve this?
EDIT
Also, according to Android tools docs:
Special case for android-support-v4.jar and android-support-v13.jar.
We make a special case for these two libraries because -v13 contains a
full version of -v4 inside. If both are found, then only -v13 will be
used.
So it should just work...
If you use exactly the same lib jar in different projects (libs project or standard projects), the apk tools get rid automatically of the duplicate ones.
The problem starts when you have two libs with the same name but not at the same revision.
The android-support-v4.jar and android-support-v13.jar files can be from different revision since there always have the same name in different support package release (http://developer.android.com/tools/extras/support-library.html#Notes)
I would check that you use exactly the same revision of the support libs in your main project and in your lib project.
To make sure, copy the two support libraries jars (located in the android-sdk folder) to your projects libs folder.
{android-sdk}/extras/android/support/v4/android-support-v4.jar
AND
{android-sdk}/extras/android/support/v13/android-support-v13.jar
It should get rid of the problem.
Delete the v4 package from the library.
Clean the Projects.
In my case, I had just updated one of the JARs in the libs/ folder to a new version.
All it took to fix it was to run Build -> Rebuild Project, i.e. issue a 'clean' task before the 'assembleDebug' etc.
Well, in my story I had to delete module dependencies which were the same, add it in library, and then add this library to each module as a dependence.
So:
Module a : Depends on android-v4 (and has it in it's lib folder)
Module b : Depends on android-v4 (and has it in it's lib folder)
Add to libraries of project "android-v4" (for ex from 1st module). Delete it in module b lib folder.
Delete in modules a and b dependence of android-v4
Add to each module dependence -> library -> android-v4 (which you created in 2 step)
I'm not sure if it is the right solution but it works.
Have you tried to change the dependency of the library from v4 to v13 and export it?
Sometimes simply removing v4 support library does not work, you also need to rebuild or the project.

Build library project

all.
I've created an android library project and it's works perfectly when i reference it from main project. But when i build the library project apart it doesn't contains R.java and resources. Is there way to build a library project with resources and R.java?
It's not possible now.
Now we 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.
There is some restrictions in this solution:
We still have to ship the resources.
We have to rewrite our code to avoid using R. values, as they
will be wrong. We will have to look up all resource IDs using
getResources().getIdentifier() and/or reflection.
I use Eclipse and never manually build my Android Library Project independently, but I think the development considerations stated on official dev guide here should answer your question:
Each library project creates its own R class
When you build the dependent application project, library projects are compiled and merged with the application project. Each library has its own R class, named according to the library's package name. The R class generated from main project and the library project is created in all the packages that are needed including the main project's package and the libraries' packages.
Update with Another note quoted from the official dev giude Library Projects:
However, a library project differs from an standard Android application project in that you cannot compile it directly to its own .apk and run it on an Android device. Similarly, you cannot export the library project to a self-contained JAR file, as you would do for a true library. Instead, you must compile the library indirectly, by referencing the library in the dependent application and building that application.

How to use github libraries that has no gradle installation?

I've seen lots of libraries in github that has no dependencies for adding them to a project.
The questions are:
How can I use these libraries?
How can I add them to my project?
For instance, this is a gitbub project that I don't know how to use those:
https://github.com/zzhouj/Android-SlidingUpPanel
Normally, you would download a JAR file - if any - or download the project and build the JAR yourself (like you would do for Volley project). But if there is no JAR support, like Android-SlidingUpPanel, just download it and add it as a module in your project.
Try doing this way;
download android-support-v4.jar, add to your LIBS folder,
build project.

Categories

Resources