我的Android Studio项目有一个像这样的目录结构:
project
|-- build.gradle
|-- settings.gradle
|
|-- app
| |-- src
| |-- build.gradle
| +-- settings.gradle
|
+-- submodule
|-- build.gradle
|-- settings.gradle
+-- library
|-- src
|-- build.gradle
+-- settings.gradle
project/submodule
是一个git子模块,直接引用另一个Android Studio项目的git存储库。它的settings.gradle
只是include ':library'
。
以前工作的配置
project/settings.gradle
说include ':submodule/library'
。 project/app/build.gradle
有compile project(':submodule/library')
线。
这一切在Android Studio 3之前都运行良好,但现在Android Studio抱怨它无法通过该名称找到模块:
Error:Unable to find module with Gradle path ':submodule/library' (needed by module 'Accession'.)
(使用gradle在命令行中运行构建似乎工作正常,因此它似乎只是Android Studio不喜欢的东西。)
在线查看似乎指的是:submodule/library
可能只是巧合,因为gradle配置中指定的项目名称被逐字地假定为与其根的相对路径相同。
我有两种可能的解决方案:
可能的解决方案1
在:submodule/library
和:submodule:library
中用build.gradle
替换settings.gradle
。这似乎有效,但我最好的猜测是它包括gradle项目:submodule
及其子项目。更重要的是,我不确定这个:x:y
符号实际上代表什么。
可能的解决方案2
用include ':submodule/library'
替换
include ':library'
project(':library').projectDir = file('submodule/library')
在settings.gradle
并用compile project(':submodule/library')
中的compile project(':library')
取代build.gradle
。
我怀疑这可能是“正确的事情”。
题
有人可以告诉我这三个gradle(原始配置和两个可能的修复)中的每一个究竟发生了什么,哪些(如果有的话)应该工作,哪个是“最佳实践”?
更新(澄清)
我所描述的内容包括我的旧Gradle和Android Gradle插件版本配置(Gradle 3.3,Android Gradle Plugin 2.3.3)以及更新后(Gradle 4.1,Android Gradle Plugin 3.0.0 beta)。
他们在新的gardle版本中改变了很多东西,我的构建脚本也不再适用了。但是这里有一个关于如何迁移构建脚本以使用较新版本的良好文档。
例如,这部分文档应该适合您:
您应该按如下方式配置依赖项:
dependencies {
// This is the old method and no longer works for local
// library modules:
// debugCompile project(path: ':foo', configuration: 'debug')
// releaseCompile project(path: ':foo', configuration: 'release')
// Instead, simply use the following to take advantage of
// variant-aware dependency resolution. You can learn more about
// the 'implementation' configuration in the section about
// new dependency configurations.
implementation project(':foo')
// You can, however, keep using variant-specific configurations when
// targeting external dependencies. The following line adds 'app-magic'
// as a dependency to only the 'debug' version of your module.
debugImplementation 'com.example.android:app-magic:12.3'
}
这里是完整文档的链接,希望它能帮助你喜欢它。
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html