6

How can I setup a shared source code library project?

To be clear, I have an Eclipse project (source code) that is a library. It is used in multiple applications. I need to import it into Android Studio. I need to be able to see its source code in the same Android Studio window as the application's source. All without moving the library's source files into the applications directory.

This post is NOT a duplicate of How do I add a library project to Android Studio? . That post discusses adding a jar library not a source code library. The solutions are similar, but not the same. Mine is more succinct, and hopefully with correct grammar making it easier to read.

Community
  • 1
  • 1
plevintampabay
  • 550
  • 4
  • 18
  • 4
    Enough! You can do what you want with this question. I have more important things to do than please you. The 2nd paragraph is very clear. Anyone having the problem I had, will do a search on the web and find this question/answer. In a few minutes they will be getting their project setup instead of the many many hours it took me. I am sorry I attempted to help the community. – plevintampabay Aug 21 '15 at 20:39
  • 1
    Agree with the poster. This can save several of us in the same boat suffering though days of agony due to the short sightedness of Google in making Android Studio. – Brian Reinhold Jan 05 '16 at 10:44
  • and the 'help' that you sometimes get with stack overflow. for some reason everybody thinks that because it's newer, it's better. It is not better, it is different. there are things that android studio does better than eclipse but after 2 days I'm already finding plenty of things eclipse does better than android studio and this is one of them. – stu Aug 16 '18 at 22:15

2 Answers2

10

The Google enforced file structure puts library projects under the directory of the application project. This is very poor design when that library might be used by multiple applications.

I found an excellent article that gave an alternate way to deal with library projects. However a couple details were missing.

The project is being imported from an Eclipse project. Also, the files were in CVS source control. Here are the steps.

1) export the Eclipse projects from CVS. Do not just get the files, because that will put a CVS directory in every folder, which will make a mess when importing to Android Studio.

2) for each library project and the application project, edit the .classpath file and delete the references to dependent library projects.

3) for each project, import it into Android Studio.

4) for each new Android Studio project, edit the settings.gradle file located in the project folder. Let's say the project you are editing is called projA and it depends on a library project called projLib. Add the following to the top of settings.gradle for projA:

include ':projLib'
project(':projLib').projectDir = new File('../projLib/projLib')

The path in "new File()" is relative to the folder containing the settings.gradle file being edited.

5) at this point, Gradle will want to sync. Do so.

6) now right click on projA in the project explorer, and click "Open Module Settings". Click the Dependencies tab, and the "+" button. Select "Module Dependency" and select the module projLib.

At this point, you should see both projA and projLib in the project explorer, and both projects should build without error (assuming they were buildable before the import).

plevintampabay
  • 550
  • 4
  • 18
  • 1
    I did exactly what you suggested and now I get the "Error:Configuration with name 'default' not found." error, do you have any idea to solve it? – Tony Dec 09 '15 at 15:05
  • I'm sorry you are having trouble Tony. I have not seen that error. There have been several updates to Studio since I wrote this post. Maybe something has changed. I know I used the above several times and so have many people. Maybe try starting from a clean workspace and go through the steps carefully again. If that doesn't work, then they changed something. Please post your solution, when you have it. – plevintampabay Dec 09 '15 at 20:11
  • Actually I have tried creating new workspace and import two new projects from them and it didn't work and then I tried to create two new projects inside the android studio and applied same method and got same error. Do your projects still work on new version of Android Studio? – Tony Dec 10 '15 at 09:06
  • I had setup 3 different workspaces with 8 projects in each, months ago. All projects were imported from Eclipse projects. All three workspaces still work as desired. I'm running the latest Android Studio (since it so nicely pesters you when there is an update to get). – plevintampabay Dec 10 '15 at 12:55
  • This is an excellent post; clearly the moderators are not familiar with the headaches Google created when coming up with Android studio. But there is one thing I want to be clear on; you are also importing the Android project from an Eclipse project as well as the library project, correct? – Brian Reinhold Jan 05 '16 at 12:12
  • In step 3 how do you import the library project? I cannot do it because it is not an Android project and Android Studio balks at it. – Brian Reinhold Jan 05 '16 at 13:53
  • Hi Brian. Yes, the imported projects are Android code and they are Eclipse library projects. As far as I know, Android Studio is only used for developing Android code. If your existing projects are not for Android, why would you want to import them into Android Studio? If you want the code from some other project, to be used for an Android, I would make a new Android project and them copy the code files into the appropriate place in the file tree of the new Android project. Be sure to fix the package names in every file before restarting Studio, who will automatically find the new files. – plevintampabay Jan 05 '16 at 14:57
  • Very useful post. I was using "pure" Android Studio projects not Eclipse imported ones and also got the `Error:Configuration with name 'default' not found` error. I resolved this by simply leaving off the final part of the path as AS projects are one level higher than Eclipse ones. e.g: `project(':projLib').projectDir = new File('../projLib')` – Kuffs Sep 16 '16 at 20:45
  • You saved my day man. Thank you. – Anfet Nov 03 '16 at 06:40
  • Unfortunately, the link is dead. – ASP Mar 22 '18 at 12:35
-4

File --> New --> Import Module

kandi
  • 1,098
  • 2
  • 12
  • 24
  • This copies the module/library into the project, so there is no "sharing" per the actual question. – Kit10 Jul 18 '18 at 15:46