资讯
所以我的项目结构如下所示:
Project root:
| Resources-module (uses the packagename: com.derk.application.resources)
-+ src
---+ main
-----+ res
| Core-module (uses the packagename: com.derk.application)
-+ src
---+ main
-----+ src
| Brand-module (uses whatever packagename of the customer domain)
Resources-module 包含 res 文件。
Core 包含一个 git 模块,其中包含我们不断更新的代码库的代码。
Brand 包含 build.gradle,我在其中设置了客户的包名,并将 main/res 目录绑定(bind)到资源模块 main/res,然后将 main/src 绑定(bind)到 core-module main/src 文件夹。
像这样:
android.sourceSets.main {
manifest.srcFile "src/main/AndroidManifest.xml"
res.srcDirs = ["$rootDir/resources-module/src/main/res"]
java.srcDirs = ["$rootDir/core-module/src/main/java", "src/main/java"]
}
}
我不希望在任何情况下更改核心模块源而不将其推送到主存储库,这意味着我只对使用核心模块的所有项目进行全局更改/修复。这就是我尝试这种结构的原因。
核心模块中的源文件将加载导入
导入 com.derk.application.resources.R;
导入 com.derk.application.resources.BuildConfig;
处理来自 gradle/idea 的资源生成内容
由于 Brand-module 是由于包名发生更改,我必须使用某种包含 R 和 BuildConfig 的中间模块以便于部署,以便核心源确实永远不会进行本地修改。
当我尝试为品牌模块刷新 gradle 时,我没有遇到任何问题,当我查看链接时,android studio 似乎在 com.derk.application.resources 中找到 R.java 和 BuildConfig.java 就好了在 Android Studio 中
然而
当我尝试运行 Brand-module 时,我得到:
"Execution failed for task ':core-module:compileReleaseJava'."
它现在向我展示了:
Error:(20, 39) error: package com.derk.application.resources does not exist
甚至以为我已经添加了
dependencies {
compile project(':resources-module')
}
到 core-module 的 build.gradle。
所以问题是:
如何设置 gradle 来处理这种跨模块依赖?
请记住,我不希望为我设置的每个新项目更改核心模块导入的包名,因为我们对用于多个项目的 gitmodule 进行了本地更改。
/.ps
目前我可以毫无问题地运行模块资源模块并运行应用程序,并使用我为其选择的静态包名。但这也是问题所在,我想让它保持静态,因此这就是我引入第三个模块的原因。
最佳答案
你这样做是错误的。您想要实现的是实现一个普通的库模块(您为每个客户使用)。然后您可以从使用共享模块的类型应用程序创建一个新模块。在这种情况下,您不需要弄乱共享模块的路径。
如果您的共享模块位于项目根目录之外的另一个目录中,您可以使用此 settings.gradle:
include ':CustomerX', ':SharedModule'
project(':SharedModule').projectDir = new File('../../some/where/else')
如果不是,您可以省略最后一行。
当您保留旧的目录结构时,您应该尝试使用此 build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 9
buildToolsVersion '21.0.1'
defaultConfig {
minSdkVersion 9
targetSdkVersion 21
}
sourceSets {
main {
assets.srcDirs = ['assets']
res.srcDirs = ['res']
aidl.srcDirs = ['src']
resources.srcDirs = ['src']
renderscript.srcDirs = ['src']
java.srcDirs = ['src']
manifest.srcFile 'AndroidManifest.xml'
}
}
}
dependencies {
// just as example
compile 'com.android.support:support-v4:21.0.2'
}
您的客户 build.gradle 应该看起来像像这样:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion '21.0.1'
defaultConfig {
minSdkVersion 9
targetSdkVersion 21
signingConfigs {
release {
storeFile file("your.keystore")
storePassword 'pwd1'
keyAlias "alias"
keyPassword 'pwd2'
}
}
buildTypes {
debug {
debuggable true
minifyEnabled false
shrinkResources false
}
release {
debuggable false
jniDebuggable false
signingConfig signingConfigs.release
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dependencies {
// dependencies of the main project
compile 'com.android.support:support-v4:21.0.0'
compile 'com.android.support:appcompat-v7:21.0.0'
compile project(':SharedModule')
}
关于android - Gradle 多模块依赖问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27194507/