【发布时间】:2026-02-14 21:05:02
【问题描述】:
我遇到了一个 gradle 构建问题,我收到错误“找不到与给定名称匹配的资源 ...”,因为我的核心模块中的布局文件中的一组 @string 引用引用了字符串来自其他模块中的 XML 文件。
这是我的基本布局结构(这是一个旧的 Eclipse 项目,我试图在 Android Studio 中转换为 gradle)。
主项目文件夹
-google-play-services_lib
-MainModule(这是引用其他模块的主模块)
-图书馆模块
-ZXing
我的 build.gradle 目前在主项目级别没有任何内容,我的主要 settings.gradle 仅包含以下项目:
include ':LibraryModule'
include ':google-play-services_lib'
include ':MainModule'
include ':ZXing'
这是我的主模块的 build.gradle,它应该在应用程序打开时运行。
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':LibraryModule')
compile project(':ZXing')
compile project(':google-play-services_lib')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
我收到的所有“未找到资源...”消息都在 MainModule 中,它们引用的字符串在 LibraryModule 中。关于此构建中可能出现的问题有什么想法吗?
编辑:根据请求添加我的 LibraryModule gradle.build 文件。
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':ZXing')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
【问题讨论】:
-
你能把标题写得更具体一点吗? “多模块 Gradle 构建问题”可能意味着任何事情。
-
我添加了我在构建中收到的错误,希望这有助于消除任何混淆。关于问题可能是什么的任何想法?
-
你可以添加你的 librarymodule gradle.build 文件吗?
-
我继续并将其添加到原始帖子中。谢谢!
-
1.为您的 librarymodule 使用 android-library 插件。 2.你的库和主模块都依赖ZXing,有点乱,有什么方法可以整合吗?