1

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.

a.p.
  • 3,248
  • 7
  • 30
  • 48
  • 1
    what libraries are you using? Most of them are in .aar and available in maven central. You need to configure build.gradle in android studio – Raghunandan May 12 '15 at 16:53
  • I have entered an explanation on my original question, it should make the issue clear. Some libraries are from Google, for example the SlidingMenu library, some are libraries I developed in Eclipse, so they are standard Eclipse projects marked as library. What do you mean by available in .aar and in maven central? Can you explain this? How is the build.gradle file configured in Android Studio? – a.p. May 13 '15 at 12:14

2 Answers2

1

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

letz
  • 1,762
  • 1
  • 20
  • 40
  • But if I have my main application opened in Android Studio, how can I import an library? What should I do with the files that are already imported under the application folder (this happens when I import the main project from Eclipse to Android Studio - every library project is copied under the main application folder). – a.p. May 13 '15 at 12:02
  • Note that I have entered an explanation on my original question, it should make the issue clear. – a.p. May 13 '15 at 12:14
  • I've edited my answer, if you want to include local libs you can do it with compile project cmd – letz May 13 '15 at 17:59
  • But if I want the common library module/project to be outside the path of the main application (so that it can be used by more than one applications) the above should not work. I think that the solution is the one described here: http://stackoverflow.com/questions/17479076/android-studio-add-external-project-to-build-gradle – a.p. Jun 07 '15 at 14:48
  • Is there anything I need to do in order for the imported module to be 'recognized' by the application using it? The above method works, I can see the imported module at the same level as the application (the root), but any classes in the imported module that are referenced by the main application are not recognized when building the module. I have checked the reference to it in the application and it looks ok. – a.p. Jun 09 '15 at 12:31
0

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.

a.p.
  • 3,248
  • 7
  • 30
  • 48