Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio, how to setup shared library projects [closed]

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.

like image 522
plevintampabay Avatar asked Aug 17 '15 04:08

plevintampabay


1 Answers

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).

like image 200
plevintampabay Avatar answered Sep 26 '22 08:09

plevintampabay