首页 > 解决方案 > 从 Android 应用程序中的其他模块引用公共模块

问题描述

我正在创建一个包含两个模块的 Android 应用程序。

一个模块是普通平板/手机模块,另一个是 WearOS 模块。

但是还有第三个模块叫做“common”。通用模块编译无误。它有java类、资源等等。它有

import static com.myapp.common.R.*;

在需要资源的主要 ​​java 类中(IDE 建议在资源参考说明下添加红线时添加该资源)。

这三个包名称类似于:

com.myapp.common

com.myapp.wear_module

com.myapp.phone_tablet_module

应用程序 build.gradle 文件包括

implementation project(':app:common')

磨损模块 build.gradle 文件包括

 implementation project(':app:common')

手机/平板电脑模块包括

 implementation project(':app:common')

但是我看到其他模块中没有引用公共模块的资源和java文件。此外,我看到一个奇怪的抱怨,例如:

ERROR: Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve project :app:common

IDE 建议添加实现行,但它已经存在。如果我接受,它会添加另一个。

问题是什么。如何将通用模块导入其他模块?

标签: javaandroidgradlemoduledependencies

解决方案


尝试这个implementation project(':common')

然后在settings.gradle确保像这样包含公共模块

include ':app', ':common', ':wear_module', ':phone_tablet_module' // as this is the modules names
rootProject.name='MyApp'

推荐阅读