Magnus
Magnus

Reputation: 18728

Import multiple Eclipse projects using the same library projects to Android Studio without ending up with duplicate modules?

I have an Eclipse project structure that looks something like this:

MyApplicationProject1
MyApplicationProject2
MyApplicationProject3
MyLibraryProject
MyLibraryProject1
MyLibraryProject2
MyLibraryProject3

Each application project uses MyLibraryProject, which in turn uses the other three library projects.

When importing these projects into Android Studio it seemed to work as expected at first. Until I took a look in the Android Studio project/module folders. Holy crap what a mess, I've now ended up with a file structure that looks something like this:

MyApplicationProject1
   myApplicationProject1
   myLibraryProject
   myLibraryProject1
   myLibraryProject2
   myLibraryProject3

MyApplicationProject2
   myApplicationProject2
   myLibraryProject
   myLibraryProject1
   myLibraryProject2
   myLibraryProject3

MyApplicationProject3
   myApplicationProject3
   myLibraryProject
   myLibraryProject1
   myLibraryProject2
   myLibraryProject3

In other words - my Eclipse library projects have been duplicated for each application project, which of course is ridiculous. How on earth can the Android Studio developers think that I'd want to maintain 3 separate copies of all my library projects?

So how do I solve this? I have searched and found some solutions mostly referring to the Android Studio import process, which got me into this mess. All I want is to be able to have one copy of each library project and import them into my different application projects.

By the way, I'm using Mac OSx if it makes any difference.

Upvotes: 1

Views: 689

Answers (1)

Magnus
Magnus

Reputation: 18728

Here's what I ended up doing:

  1. I created a new "dummy" project in Android Studio.

  2. In Eclipse, I removed all references to the library projects from MyLibraryProject, so they wouldn't get imported by Android Studio.

  3. In the dummy project I imported my library projects as modules (File->New->Import Module)

  4. I moved the folders containing my new modules from the project folder to my Android Studio projects root folder.

  5. In Eclipse, I removed all references to MyLibraryProject from my application projects (under Project properties -> Android), so it wouldn't get imported by Android Studio.

  6. I imported the Eclipse application projects into Android Studio (using File->New->Import Project)

    I now had a folder structure that looked something like this:

 

Android Studio Projects
    MyApplicationProject1
    MyApplicationProject2
    MyApplicationProject3
    MyLibraryProject
    MyLibraryProject1
    MyLibraryProject2
    MyLibraryProject3

 

  1. I edited the settings.gradle file in my application projects, like this:

 

include ':MyApplicationProject1'

include ':MyLibraryProject'
project(':MyLibraryProject').projectDir = new File(settingsDir, '../MyLibraryProject')

include ':MyLibraryProject1'
project(':MyLibraryProject1').projectDir = new File(settingsDir, '../MyLibraryProject1')

include ':MyLibraryProject2'
project(':MyLibraryProject2').projectDir = new File(settingsDir, '../MyLibraryProject2')

include ':MyLibraryProject3'
project(':MyLibraryProject3').projectDir = new File(settingsDir, '../MyLibraryProject3')

 

  1. I edited the build.gradle file for MyLibraryProject like this:

 

dependencies {
    compile project(':MyLibraryProject1')
    compile project(':MyLibraryProject2')
    compile project(':MyLibraryProject3')
}

 

  1. I edited the build.gradle file for the module (not the project gradle file) MyApplicationProject1 like this:

 

dependencies {
    compile project(':MyLibraryProject')
}

 

As you can see, I've had to add all the libraries to the projects settings.gradle file. I tried creating a new settings.gradle file for MyLibraryProject and including the other three library projects/modules there, but that didn't work. Not a big deal though.

This setup seems to work like a charm, hopefully it helps someone else who struggles with this.

Upvotes: 1

Related Questions