1 背景

事情是这样的,最近在利用组件化的方式开发一个多模块工程,看着模块越来越多,于是想把某些同一层级的模块放到同一个目录下,如下:
在这里插入图片描述

可以直接将相应模块拖动到目标目录下,也可以如下所示,在模块上右键,选择move directory移动路径
在这里插入图片描述

2 问题

本以为只是移动一个路径而已,其他的Android Studio都会帮我们处理好,无需额外操作,结果移动之后发现项目编译失败,报错信息如下:

Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'.
   > Could not resolve project :limu_music.
     Required by:
         project :app
      > No matching configuration of project :limu_music was found. The consumer was configured to find an API of a component, preferably optimized for Android, as well as attribute 'com.android.build.api.attributes.BuildTypeAttr' with value 'debug', attribute 'org.jetbrains.kotlin.platform.type' with value 'androidJvm' but:
          - None of the consumable configurations have attributes.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

关键信息在:No matching configuration of project :limu_music was found.

于是去网上搜了一圈,但没找到什么有效的解决方案,对gradle还是太陌生了,虽然平时天天和它打交道,但是一直没有好好的去学习相关的一些知识。
在这里插入图片描述

3 解决方法

就在随便翻一翻桌边那本gradle书籍时,还真有了一些发现,看到了setting.gradle的相关配置。
在这里插入图片描述
可以看到,我们在setting.gradle中定义了很多子项目,但是并没有配置具体的项目路径,此时gradle会默认这些子项目的路径是setting.gradle文件的同级目录,所以才能找到我们相关子项目。

但是上面我移动了项目路径,导致同级目录找不到相关项目。从上面的报错信息也可以看到,No matching configuration of project :limu_music was found.,大概就是模块的配置文件没找到,这才导致了标题中的问题出现。

解决这个报错只需要我们手动指定相应项目的路径即可,提供一下几个方法:

(1)在include时指定路径
setting.gradle文件中在include模块时带上目录,这里的business就是这个模块的父目录,:就相当于\
在这里插入图片描述
另外,在壳工程/其它模块中引入该模块时也需如此,带上模块路径

implementation project(':business:limu_music')

(2)配置项目是指定具体的路径
这种方法在依赖相关模块时和之前一样,只需要指定模块名称

implementation project(':limu_music')

只需要在setting.gradle中配置模块目录:
在这里插入图片描述
图中四种方式都是可以的,任选一种即可,此时重新编译就没什么问题了。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐