【问题标题】:Android Studio - How to make modules inside a subdirectory?Android Studio - 如何在子目录中制作模块?
【发布时间】:2015-04-23 21:05:35
【问题描述】:

假设我有 3 个模块:mod1、mod2 和 mod3。

在正常情况下,文件系统层次结构应该是:

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

但我想要的是:

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

如何在 Android Studio 1.1.0 中做到这一点?

PS:我找到了这篇文章,但它似乎不起作用,或者它适用于早期版本的 AS,而不是 1.1.0: How can I move a module inside a subdirectory?

【问题讨论】:

    标签: android-studio module android-gradle-plugin build.gradle subdirectory


    【解决方案1】:

    你可以做到的:

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

    在你的settings.gradle

    include ':modules:mod1' , ':modules:mod2', ':modules:mod3'
    

    【讨论】:

    • 感谢您的反馈。这种方法有效......有点...... AS将生成一个名为“modules”的虚假和空模块,并生成“modules/modules.iml”。如果我在 AS 项目结构中删除此模块,AS 将再次自动生成它。有什么想法吗?
    • 我放弃了对这个主题的数小时研究......重要的是项目本身......我现在使用这个方法:名称前缀为“module”的模块,例如moduleMain , moduleFuncA, moduleFuncB, 等等...
    • @McArthorLee 如果不想看到空模块,可以从菜单中选择“加载/卸载模块...”并卸载空模块。
    【解决方案2】:

    虽然include ':modules:mod1' , ':modules:mod2', ':modules:mod3' 为您提供了解决方案,但AS 烦人地为modules 生成了一个空模块。要解决这个问题,请参阅gradle github repo 上的settings.gradle。它基本上包括每个子项目作为顶级项目,然后将其项目目录设置在子目录内。这个技巧应该可以完美地满足您的需求。

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

    【讨论】:

    • 在中间目录'modules'中添加新的'build.gradle'可以达到同样的效果,内容为:jar.enabled = false; uploadArchives.enabled = false
    【解决方案3】:

    子目录中的模块

    settings.gradle文件

    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]

    【讨论】:

      【解决方案4】:

      我的项目结构

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

      setting.gradle 文件

      // 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"
      

      我在 settings.gradle 中使用了重复声明来解决。 (Android Studio 3.0),我认为这是AS的错误。 有时需要重启AS。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-15
        • 2010-10-29
        • 1970-01-01
        • 2015-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多