42

Suppose that I had 3 modules: mod1, mod2 and mod3.

In normal case, the file system hierarchy should be:

[android_root_dir]
build.gradle # list this file just to make it clear.
----mod1
----mod2
----mod3

But what I want is:

[android_root_dir]
build.gradle # list this file just to make it clear.
----modules
    ----mod1
    ----mod2
    ----mod3

how to do it in Android Studio 1.1.0?

PS: I find this article but it does not seem to work, or it works for earlier versions of AS, not 1.1.0: How can I move a module inside a subdirectory?

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
McArthor Lee
  • 459
  • 1
  • 5
  • 8

4 Answers4

67

You can do it:

root
  build.gradle
  settings.gradle
  modules
    mod1
      build.gradle
    mod2
      build.gradle
    mod3
      build.gradle

In your settings.gradle

include ':modules:mod1' , ':modules:mod2', ':modules:mod3'
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 7
    thx for your feedback. this method works...somewhat... AS will generate a fake and null module with the name of "modules", and generate "modules/modules.iml". if i delete this module in AS project structure, AS will auto-generate it again. any ideas? – McArthor Lee Feb 23 '15 at 03:05
  • i give it up for hours of research on this topic... what's important is the project itself... i use this means now: name modules with name prefix of "module", such as moduleMain, moduleFuncA, moduleFuncB, etc... – McArthor Lee Feb 23 '15 at 03:52
  • @McArthorLee If you do not want to see the null module, you can choose "Load/Unload modules ..." from the menu and unload the null module. – ivan.panasiuk May 17 '18 at 08:05
12

Android Module inside a subdirectory

settings.gradle file

include ':module1', ':module2', ':module3'

project(':module1').projectDir = new File('modules/module1')
project(':module2').projectDir = new File('modules/module2')
project(':module3').projectDir = new File('modules/module3')

[Example]

yoAlex5
  • 29,217
  • 8
  • 193
  • 205
10

Though include ':modules:mod1' , ':modules:mod2', ':modules:mod3' kind of gives you the solution, the AS annoyingly generates an empty module for modules. To address this gotcha, please refer to the settings.gradle on the gradle github repo. It basically includes every subproject as a top level one and then sets its project directory to be inside the subdirectory. This trick should perfectly address your need.

include 'mod1'
include 'mod2'
...
include 'modX'
rootProject.name = 'gradle'
rootProject.children.each {project ->
    String projectDirName = "modules/$project.name"
    project.projectDir = new File(settingsDir, projectDirName)
    assert project.projectDir.isDirectory()
    assert project.buildFile.isFile()
}
Farley
  • 1,160
  • 1
  • 10
  • 17
  • 1
    Same result can be achieved by adding new 'build.gradle' in the intermediate directory 'modules', with content: `jar.enabled = false; uploadArchives.enabled = false` – Dimitar II Oct 15 '17 at 20:54
3

My project struct

Coding
   app
   push-dir
     coding-push
     push-xiaomi
   setting.gradle

setting.gradle file

// need repeat declarative, i think it's bug
include ':app'
include ':coding-push'
include ':push-dir:coding-push'
include ":push-xiaomi"
include ":push-dir:push-xiaomi"

I used repeat declarative in settings.gradle to solve. (Android Studio 3.0), I think it's AS's bug. Sometimes need restart AS.

codingnow
  • 94
  • 4