McArthor Lee
McArthor Lee

Reputation: 539

Android Studio - How to make modules inside a subdirectory?

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?

Upvotes: 50

Views: 22524

Answers (6)

Dumitru Hristov
Dumitru Hristov

Reputation: 1499

Let's say you want to create a new module 'myfeature' and place it inside the 'features' folder:

  1. File -> New -> New Module
  2. In the Templates pane choose Android Library
  3. In the Module Name write ':features:myfeature'
  4. modify the rest of the fields to suit your needs
  5. Click on Finish

Now if you didn't have a 'features' folder it will be automatically created and a new module named 'myfeature' will be placed inside it. Otherwise the new module will appear inside the existing 'features' folder.

Upvotes: 9

user128440
user128440

Reputation: 376

In your settings.gradle.kts add include(":directoryname") and sync gradle after which a new "module" in the Android pane will show up where you will be able to add modules. After adding remove the line.

Upvotes: 0

yoAlex5
yoAlex5

Reputation: 34175

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]

Upvotes: 16

codingnow
codingnow

Reputation: 94

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.

Upvotes: 3

Farley
Farley

Reputation: 1190

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()
}

Upvotes: 12

Gabriele Mariotti
Gabriele Mariotti

Reputation: 363429

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'

Upvotes: 76

Related Questions