简体   繁体   English

Gradle多模块依赖问题

[英]Gradle multi-module dependecy issue

Information 信息

So I have this project structure that looks like this: 所以我有这个项目结构,如下所示:

Project root: 项目根目录:
| | Resources-module (uses the packagename: com.derk.application.resources) 资源模块(使用软件包名称:com.derk.application.resources)
-+ src -+ src
---+ main ---主要
-----+ res ----- +资源
| | Core-module (uses the packagename: com.derk.application) 核心模块(使用程序包名称:com.derk.application)
-+ src -+ src
---+ main ---主要
-----+ src ----- + src
| | Brand-module (uses whatever packagename of the customer domain) 品牌模块(使用客户域的任何软件包名称)

Resources-module contains the res files. 资源模块包含res文件。
Core contains a git-module that contains code for a code-base we keep updated. Core包含一个git模块,其中包含我们不断更新的代码库的代码。
Brand contains build.gradle where I setup the packagename of our customer and bind the main/res directory to the resource-module main/res, and then the main/src to the core-module main/src folder. 品牌包含build.gradle,在其中我设置客户的软件包名称,并将main / res目录绑定到资源模块main / res,然后将main / src绑定到核心模块main / src文件夹。

Like this: 像这样:

    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"]
    }
}

I do not wish to alter the Core-module sources under any circumstances without it being pushed up to the master repository, meaing I only make global changes/fixes for all projects that uses core-module. 我不希望在任何情况下都不会更改核心模块源,而不会将其推到主存储库中,这意味着我只对使用核心模块的所有项目进行全局更改/修复。 This is why i tried this structure out. 这就是为什么我尝试了这种结构。

The sourcefiles in the core-module will loads the imports 核心模块中的源文件将加载导入
import com.derk.application.resources.R; 导入com.derk.application.resources.R;
import com.derk.application.resources.BuildConfig; 导入com.derk.application.resources.BuildConfig;

to handle the resource generated content from gradle/idea 处理来自gradle / idea的资源生成的内容

Since Brand-module is due to having packagename changes, I have to use some sort of middlemodule that holds the R and BuildConfig for easy deployment, so that the core-sources indeed never have local modifications. 由于Brand-module是由于更改了程序包名称而导致的,因此我必须使用某种包含R和BuildConfig的中间模块来简化部署,因此核心源实际上永远都不会进行本地修改。

When i try to refresh gradle for the brand-module, i do not get any issues, and android studio seems to find the R.java and BuildConfig.java just fine in the com.derk.application.resources when I check out the linkage in Android Studio 当我尝试刷新brand-module的gradle时,我没有任何问题,当我检查链接时,android studio似乎在com.derk.application.resources中很好地找到了R.java和BuildConfig.java。在Android Studio中

HOWEVER 然而

When I try to run Brand-module, i get: 当我尝试运行品牌模块时,我得到:

"Execution failed for task ':core-module:compileReleaseJava'."

and it now instead shows me: 现在它向我显示:

 Error:(20, 39) error: package com.derk.application.resources does not exist

even thought I have added 甚至以为我添加了

 dependencies {
    compile project(':resources-module')
 }

to the build.gradle of core-module. 到核心模块的build.gradle。

So the question is: 所以问题是:

How do I setup gradle to handle this kind of cross-module dependency? 如何设置gradle来处理这种跨模块依赖性?

Keep in mind, I do not wish to alter the packagename for the core-module imports for each new project I setup, because we get local changes made to a gitmodule that is used for several projects. 请记住,我不希望为我设置的每个新项目更改核心模块导入的软件包名称,因为我们会对用于多个项目的gitmodule进行本地更改。

/.ps /.ps

Currently i can without problem run module resources-module and have the app running, with the static packagename i've chosen for it. 目前,我可以毫无问题地运行模块资源模块并运行应用程序,并为其选择了静态包名称。 But that is also the problem, I want to keep it static, and hence that is why i introduced the third module. 但这也是问题,我想使其保持静态,因此这就是为什么我引入了第三个模块的原因。

You do that in the wrong way. 您以错误的方式进行操作。 What you are trying to achive is implementing a normal library module (which you use for every customer). 您要实现的目标是实现一个普通的库模块(每个客户都使用该模块)。 Then you can create a new Module from the type application which uses the shared module. 然后,您可以从使用共享模块的类型应用程序中创建一个新模块。 In that case you don't need to mess around with the path of the shared module. 在这种情况下,您无需弄乱共享模块的路径。

For the case that your shared module is in another directory than the project root you can use this settings.gradle: 如果您的共享模块位于项目根目录以外的其他目录中,则可以使用以下settings.gradle:

include ':CustomerX', ':SharedModule'
project(':SharedModule').projectDir = new File('../../some/where/else')

If not you can omit the last line. 如果没有,您可以省略最后一行。

When you keep a old directory structure you should try using this build.gradle: 当您保留旧的目录结构时,应尝试使用此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'
}

You customers build.gradle should look eg like this: 您的客户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')
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM