2

So I recently migrated to Android Studio from Eclipse. For the most part, it's better, but I haven't found a good way to maintain a shared codebase between multiple projects.

What I want to do is be able to share some code between several of my applications. Each application is in its own project. From what I've seen, most people add it as a library module in the application's project. The problem with that is the module is accessible from only one project. The other projects within which my other applications reside can't access the library.

It seems to me like there should be a mechanism for creating another library project and then allow each of the application projects to access that code. This worked in Eclipse, where I would create another project in my workspace, mark it as a library, and then have the other projects reference it. I would be able to change the code in the library and then all of the projects referencing it would automatically build with the updated code.

Is this something I could do in Android Studio?

Cybran
  • 83
  • 1
  • 1
  • 8

1 Answers1

1

Yes this is possible:

Create the project you would like to have as a shared library - we'll refer to it as sharedProject.

Now in the project that you want to use this library open settings.gradle and paste the following:

include '..:sharedProject:app'

Open your build.gradle and paste the following under the dependencies element:

compile project(':..:sharedProject:app')

You can use this technique for as many projects as you'd like to refer to your common codebase in sharedProject. Note that this assumes your project and your sharedProject directories are in a common workspace directory (which is almost always the case).

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
  • Unfortunately, this did not work for me. It is saying that the project with that path cannot be found in the current project. I did change the path you gave to reflect my project names. – Cybran Jun 12 '14 at 05:01
  • Project with path '..:sharedProject:lib' could not be found in project ':currentProject' – Cybran Jun 12 '14 at 05:05
  • The sharedProject's `build.gradle` is at `sharedProject/lib/build.gradle`? – Martin Konecny Jun 12 '14 at 05:08
  • Interesting - it is definitely working here. First make sure your project and your library have a common parent directory. Next try removing the `:app` suffix. Also make sure you're on latest version of Gradle (or at least version 0.5.6). Best of luck! – Martin Konecny Jun 12 '14 at 05:30