Migrating from Eclipse to Android Studio and library issues - android

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.

Related

Avoid code duplication in Android Studio modules

I've created a project and a library separately using Android Studio, each in a separate folder inside some directory.
I tried to add the library as a module to the project, and noticed that instead of just referencing the library like in Eclipse, the library was copied inside the project directory.
That means that if this happened N times for N projects, then I'll have N copies of the library and I'll need to update them all when any update is to be done.
I'm working on v 1.0.2 of Android Studio.
Any one has a better idea to do it?
Three options I know of:
You can specify the path to the external library:
Android studio add external project to build.gradle
Include the compiled jar file from the library in the libs directory of the N apps.
Publish the artifact (the jar from library project) to a gradle repository and then you can add dependencies to that project just like you would for the support library etc.
See http://gradle.org/docs/current/userguide/artifact_management.html

Importing android gradle project into eclipse

I need to import this library project into my eclipse. While i am trying to use this project it have some error. Can you help me on this. https://github.com/daimajia/AndroidImageSlider
That project is created for use with Gradle for Android. Eclipse does not support that yet. It is also distributed as an AAR, which Eclipse does not support. There are recipes for converting AARs into Eclipse-friendly Android library projects that you can try. Otherwise, you will need to reorganize the project code yourself to support the classic Eclipse-style project structure. Mostly, that will involve moving the contents of library/src/main/ into a regular Eclipse Android library project:
The res/ directory and AndroidManifest.xml file would go in the library project root directory
The java/ directory would be renamed src/ and also go in the library project directory
However, you will have to repeat this process each and every time the library's author updates the library, at least for those updates that you are interested in.
You may wish to see if there is an alternative library that meets your needs but is better packaged for use with Eclipse.
Basically there are some changes to be done to eclipse project before importing it to eclipse like src folder.
The project you posted may contains many error since it have two more android project dependency.
I tired to convert above project with its dependency :
Checkout Complete Source Code
There is one more project lib u need to add LIB

Library Project Supporting Eclipse and Android Studio

I'm working on a library project that provides access to a service. We started the project few months ago and we were supporting Eclipse only (since Android Studio was a prewview edition).
Now that Android Studio has become a "beta" version, and its popularity has increased greatly, we had the intention to support it as welll, but we are facing the problem of how to support both "styles" with the same base (project structure and code).
The library we are building has a UI that forced us to have the library as library project instead of just a simple jar. We have this project working with ANT to build the required files (jars) and packaging everthing in a library project.
Android Studio now introduces the .aar library files, that can also contain UI.
So our problem is finding examples of other library projects containing UI that are also supporting both IDE's. Wondering if someone else have face this same situation.
Is is possible to have a Library Project to support both IDEs? (Eclipse and Android Studio)
Thanks to #CommonsWare. When I looked at your projects I realize that we didn't need our project to be "Android Studio compatible". Since we wanted to share the project as an .aar file, I had only to make a build.gradle at the root of my library project and add the gradle folder (containing the gradle wrapper jars).
In this way I can use the console and create a .aar file using "./gradlew aR" command. Now I can distribute the library project for Eclipse users or the .aar file for Android Studio users.
I'm testing the .aar file, and the only problem I have right now is that classes inside a jar file within libs folder inside the .aar file are not recognized, just the classes present inside "classes.jar", but I think I would create another question here in SOF since is not relevant for this question.

Is it possible to share library projects between projects when using Android Studio and gradle?

I'm struggling to figure out how to import library projects into Android Studio in a fashion that makes them available to multiple projects. The documentation for the new build system implies that you need to import library projects into the root of the project you are working on:
Gradle projects can also depend on other gradle projects by using a multi-project setup. A multi-project setup usually works by having all the projects as sub folders of a given root project
(http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Multi-project-setup)
This is problematic since, as I mentioned above, I would like to keep frequently-used libraries accessible to all current and future projects. For example, I am currently trying to integrate Volley into a new project to evaluate it, with the assumption that I will want to use it in multiple other projects in the future. I cloned it to my "${PROJECT_ROOT}/" folder, which is the same level at which I created my test project, giving me:
${PROJECT_ROOT}/TestProject/Test
${PROJECT_ROOT}/volley
After trying to set up my project's build.gradle file in a variety of ways, the only way that I've managed to make the app compile is to move Volley into the TestProject 'main' project, giving me:
${PROJECT_ROOT}/TestProject/Test
${PROJECT_ROOT}/TestProject/volley
Not only does this prevent me from using Volley in other projects that are not a part of TestProject without duplicating it or cloning it a second time, but it means that git wants me to add it to the repo I've established at the root of TestProject.
How can I reference library projects in my Android Studio projects without including them in the projects themselves?
Couldn't you use git with submodules? e.g. In your .gitmodules file, you may add this:
[submodule "volley"]
path = volley
url = https://path/to/volley/repository
You can set the url to Volley's official repository, or to your own in-house version of volley. Other projects can be setup the same way and point to the same volley repository.
I think this way, other users can call git clone and all the dependent projects will be downloaded within the main project folder and they don't have to worry about downloading the library projects separately.
For Volley though, I would just compile it into a JAR file and stick it into the /libs folder of the main project. That is, if you don't need to modify its source.
[Update]
For library projects that you don't need to modify its source, you can try using Android Studio to compile them into AAR files for sharing. AAR file is like a JAR file to Android, so you can add them to your /lib source folder, or publish them to your local/intranet maven repository. If you choose the maven route, add your local/intranet repository in build.gradle, and reference the library project that you've published.
Hopefully in the future more Library projects owner will build their projects into AAR file and publish them to Maven Central Repository, so we can just reference them directly from the build.gradle file.

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.

Categories

Resources