61

I'm creating projects with dependencies in Android Studio. I know how to link projects by adding modules.

But I realized that 'importing modules' create a copy of the libProject inside the project.

Is there a way to prevent that ? Like an 'external module' ?

Since i'm in charge of both project, I want to be able to push changes to the libProject Repo, without having to copy paste files between folders.

Thanks

Daniels Šatcs
  • 526
  • 6
  • 18
Philippe David
  • 8,814
  • 3
  • 24
  • 35
  • Possible duplicate of [How to reference without copying a library project on Android Studio?](https://stackoverflow.com/questions/24494105/how-to-reference-without-copying-a-library-project-on-android-studio) – TT-- Jan 29 '19 at 03:08

4 Answers4

122

Yes, you can do it. The module needs to have a Gradle build file set up for it. If it's got that, then in the project you're linking to it, add this to the settings.gradle file at the project root:

include ':libraryName'
project(':libraryName').projectDir=new File('/path/to/library')

where the path you specify in the second line is the path to the directory containing the library's build.gradle file. The path can be relative or absolute.

Scott Barta
  • 79,344
  • 24
  • 180
  • 163
  • Thanks works perfectly! Is there a reason the settings.gradle is read-only ? I used Sublime to edit the file . – Philippe David Jul 09 '14 at 17:14
  • I don't know why it would be read-only. Must be something specific about your project or source control. – Scott Barta Jul 09 '14 at 17:18
  • I am having a problem with this http://stackoverflow.com/questions/24898060/gradle-dependencies-in-folder-above-project – CQM Jul 29 '14 at 22:18
  • @ScottBarta this works like a charm, thanks for saving me hours of digging around! – andreimarinescu Jan 13 '15 at 17:26
  • 4
    I have added the lines, but the main app does not compile because it does not find the package defined in the library. What should I look for? – Antonio Sesto Feb 15 '15 at 18:17
  • Add the new project as a module dependency now – bickster Feb 24 '15 at 20:00
  • 1
    This does not work any more as of Android studio 1.2.1. Does any one have workaround for this new version? – peceps May 13 '15 at 15:10
  • 4
    @peceps still working in android studio 1.4, don't forget to add the library to build.gradle dependencies as `compile project(path: ':libraryName')`. – nicolausYes Oct 17 '15 at 20:14
  • I created a project "ScreenSaver" and a module inside it named "screensaver" Path for the project is "C:\Users\sadip_000\StudioProjects\mp\" and module is "C:\Users\sadip_000\StudioProjects\mp\ScreenSaver\" But the gradle says project with path not found what might be the issue? – A_rmas Mar 17 '16 at 03:43
  • 1
    Why do they have to make it so hard? – lordscales91 Apr 13 '18 at 11:04
  • I was wrong because didn't attention to second part of accepted answer where it says **where the path you specify in the second line is the path to the directory containing the library's build.gradle file. The path can be relative or absolute.** – Vahid Sep 24 '19 at 10:14
  • I thought this might not work anymore (v4.1.2) - I discovered I had to **Sync Project With Gradle Files** after starting up AS to get it to reload properly. – spartygw Mar 12 '21 at 15:04
20

The solution:

include ':libraryName'
project(':libraryName').projectDir=new File('/path/to/library')

was not working for me. After couple of wasted hours I figured out the issue. There are two build.gradle files, one for project and one for library name. If the library is in the folder '\MyLib' then there will be a build.gradle in '\MyLib' and another at '\MyLib\app'. You have to point to the '\MyLib\app' and not '\Mylib'.

Hopefully this saves some time for others.

phoenix
  • 7,988
  • 6
  • 39
  • 45
cbelwal
  • 379
  • 2
  • 7
2

If you have, like myself, have multiple modules (I only realised today that copies were included, I thought that the project included links to the source.)

You can have multiple modules/projects along the lines of :-

include ':app', ':sqlwords', ':dbindex', ':dbcolumn', ':dbtable', ':dbdatabase', ':displayhelp', ':pickdate'
project(':sqlwords').projectDir= new File('d:/Android_Applications/Modules/sqlwords')
project(':dbcolumn').projectDir= new File('d:/Android_Applications/Modules/dbcolumn')
project(':dbtable').projectDir= new File('d:/Android_Applications/Modules/dbtable')
project(':dbindex').projectDir= new File('d:/Android_Applications/Modules/dbindex')
project(':dbdatabase').projectDir= new File('d:/Android_Applications/Modules/dbdatabase')
project(':displayhelp').projectDir= new File('d:/Android_Applications/Modules/displayhelp')
project(':pickdate').projectDir= new File('d:/Android_Applications/PickDateShowCase/pickdate')
MikeT
  • 51,415
  • 16
  • 49
  • 68
1

You can also use android { sourceSets{ main.java.srcDirs += '../../../library/src' }} in your app build.gradle . Not sure about supporting all android resources, for pure java library works well.

pvllnspk
  • 5,667
  • 12
  • 59
  • 97