11

I need to write a lot of trivial modules and I don't want to hide the main modules between them. For this reason I want to have a directory tree like this:

Project
|-module1
|-module2
+-directory
    |-module3
    |-module4
    +-module5

The problem: if I move the modules inside a new folder Android Studio doesn't find them.

So, my question is: How can I move modules inside a directory?

2
  • Is this for creating a new project? Or modifying an existing one?
    – Javaru
    Jan 12, 2015 at 16:12
  • Create new modules in a existing project. Jan 12, 2015 at 16:17

4 Answers 4

18

I would recommend closing the project in Android Studio, moving the directories manually in your OS, and updating the project's settings.gradle file to point to the modules in their new location. Using your example, the updated settings.gradle file will look like this:

include ':module1'
include ':module2'
include ':directory:module3'
include ':directory:module4'
include ':directory:module5'

Then re-open the project in Android Studio, and make sure it syncs the project with the new Gradle files (which it should do automatically, but if it doesn't, click the toolbar button for it).

1
  • 4
    Refactor -> Move does the trick. Then make changes to the settings.gradle as mentioned here.
    – suhas_sm
    Jan 28, 2016 at 10:25
5

You can now define the location of modules with

include ':myModule'    

project(':myModule').projectDir = new File('dir/myModule')

Further, you have to remove the .iml files under the module directory to let Android Studio recreate them.

1
  • Doing include ':directory:moduleX' didn't work for me, this is the only solution that worked
    – tenprint
    Aug 30, 2022 at 20:02
0

You can simply add them as a new module.

  1. Open the new module dialog either via:
    • File > New Module; or
    • File > Project Structure, [Modules] and click the add icon enter image description here and select New Module
  2. Select the module type, I assuming Android in your case. Then select the Android module type
  3. Click Next
  4. Either type in or use the browse button enter image description here to open the file chooser and enter the "Content Root" as Project\directory\module3
  5. The "module file location" should update to match the content root, but double check it and set it if necessary
  6. Click Finish
  7. Repeat for the other modules

By default, in the project tool window tree, it will show a flat structure of:

Project
+-Module1
+-Module2    
+-Module3
+-Module4
+-Module5

If you want to add a hierarchy to the project tree structure:

  1. Open the project structure dialog (File > Project Structure)
  2. Select the modules panel on the left.
  3. Select module3 and right click it. Select Move Module to Group > New Top Level Group.
  4. Enter a name and click OK.
  5. for all the other sub modules, select them, right click, and select Move Module to Group > {yourGroupName} > To this group

Now in the project tool window, those modules will be grouped into a hierarchy.

1
  • 2
    This options are not at Android Studio. I suppose that this options are at IntelliJ Idea... I probably need to change to pure IntelliJ. Jan 12, 2015 at 17:00
0

One more variant where you include modules and provide their paths

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.