Android Studio - Creating Modules without copying files?
Asked Answered
S

4

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

Spermic answered 9/7, 2014 at 15:55 Comment(1)
Possible duplicate of How to reference without copying a library project on Android Studio?Rank
Q
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.

Quadripartite answered 9/7, 2014 at 16:40 Comment(12)
Thanks works perfectly! Is there a reason the settings.gradle is read-only ? I used Sublime to edit the file .Spermic
I don't know why it would be read-only. Must be something specific about your project or source control.Quadripartite
I am having a problem with this #24898560Yokum
@ScottBarta this works like a charm, thanks for saving me hours of digging around!Agneta
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?Marlanamarlane
Add the new project as a module dependency nowSibilate
This does not work any more as of Android studio 1.2.1. Does any one have workaround for this new version?Stuppy
@Stuppy still working in android studio 1.4, don't forget to add the library to build.gradle dependencies as compile project(path: ':libraryName').Teammate
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?Gadfly
Why do they have to make it so hard?Bushore
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.Panettone
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.Eloyelreath
L
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.

Ld answered 15/1, 2016 at 13:11 Comment(1)
Thank you so much for posting this! I wasted hours on this.Seismograph
H
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')
Heroine answered 5/7, 2017 at 23:30 Comment(0)
I
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.

Incarnate answered 4/5, 2015 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.