Android Studio - Creating Modules without copying files?

AndroidAndroid Studio

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

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.

Solution 3 - Android

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

Solution 4 - Android

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhilippe DavidView Question on Stackoverflow
Solution 1 - AndroidScott BartaView Answer on Stackoverflow
Solution 2 - AndroidcbelwalView Answer on Stackoverflow
Solution 3 - AndroidMikeTView Answer on Stackoverflow
Solution 4 - AndroidpvllnspkView Answer on Stackoverflow