Android Studio how to set up shared library projects

How to set up a common source code library project?

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

This post is not a duplicate. How do I add a library project in Android Studio? . This article discusses adding a jar library rather than a source code library. The solutions are similar, but not the same. Mine is shorter, and hopefully with the right grammar to make it easier to read.

+5
source share
2 answers

Google's structured file structure places library projects in the application project directory. This is a very poor design when this library can be used by several applications.

I found a great article that provided an alternative way to work with library projects . However, a couple of details were missing.

The project is imported from the Eclipse project. In addition, the files were in the CVS control. Here are the steps.

1) export Eclipse projects from CVS. Not just getting the files, because it will put the CVS directory in each folder, which will cause a mess when importing into Android Studio.

2) for each library project and application project, edit the .classpath file and delete links 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. Say the project you are editing is called projA, and it depends on the 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 the "new file ()" refers to the folder containing the settings.gradle file being edited.

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

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

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

+9
source

File → New → Import Module

-3
source

All Articles