gpt4 book ai didi

模块的 Android 设置库依赖项

转载 作者:行者123 更新时间:2023-12-03 05:13:12 25 4
gpt4 key购买 nike

背景:

我有一个 Android项目,我想将特定功能重构为一个 module .我的应用程序结构如下所示:

MyApp
|--app
|----build.gradle
|--myNewModule
|----build.gradle
|build.gradle (MyApp Project)
|settings.gradle

任务:

在项目中,我有一些仅在 myNewModule 中需要的依赖项和一些整个项目需要的依赖项,例如 okHttpClient .

建议:

目前,我的 gradle 文件如下所示:

build.gradle (MyApp 项目)

定义变量以在整个项目中设置相同的库版本。
...
ext {

butterknifeVersion = "7.0.1"
daggerVersion = "2.6"
moshiVersion = "1.1.0"
okhttpVersion = "3.4.1"
...
}
...

build.gradle (MyNewModule)
定义此模块所需的依赖项
...
dependencies {

compile ("com.squareup.retrofit2:converter-simplexml:$retrofitVersion") {
exclude group: 'stax', module: 'stax-api'
exclude group: 'stax', module: 'stax'
exclude group: 'xpp3', module: 'xpp3'
}
compile "com.squareup.okhttp3:okhttp-urlconnection:$project.okHttpVersion"
compile "com.squareup.okhttp3:okhttp:$project.okHttpVersion"
compile "com.squareup.okhttp3:logging-interceptor:$project.okHttpVersion"
....
}
...

build.gradle (应用程序)
定义其他依赖,可以与 MyNewModule 中的依赖重叠吗?
...
dependencies {
compile "com.jakewharton:butterknife:$project.butterknifeVersion"
compile "com.jakewharton.timber:timber:$project.timberVersion"
compile "com.jakewharton.threetenabp:threetenabp:$project.threetenabpVersion"

compile "com.squareup.okhttp3:okhttp-urlconnection:$project.okHttpVersion"
compile "com.squareup.okhttp3:okhttp:$project.okHttpVersion"
compile "com.squareup.okhttp3:logging-interceptor:$project.okHttpVersion"
...
}
...

问题:
  • 如果我对不同模块中的同一个库有依赖项,Android 会将其识别为一个依赖项,还是会使 apk 的 dexcount 加倍?
  • 这是定义依赖关系的好方法吗?在我的情况下,myNewModule 将包含依赖项,这些依赖项仅在其中需要,但两个模块可能包含相同的依赖项,因为两者都需要它们(例如,将 okHttp 客户端添加到两个模块,因为两个模块都需要它们)
  • 或者,我是否可以将公共(public)依赖项直接放入项目中的 build.gradle 文件中,就像使用模块 build.gradle 文件一样?

  • build.gradle (MyApp 项目)

    在这里添加整个项目的所有依赖项?
    ...
    dependencies {
    compile "com.jakewharton:butterknife:$project.butterknifeVersion"
    compile "com.jakewharton.timber:timber:$project.timberVersion"
    compile "com.jakewharton.threetenabp:threetenabp:$project.threetenabpVersion"

    compile "com.squareup.okhttp3:okhttp-urlconnection:$project.okHttpVersion"
    compile "com.squareup.okhttp3:okhttp:$project.okHttpVersion"
    compile "com.squareup.okhttp3:logging-interceptor:$project.okHttpVersion"
    ...
    }
    ...

    我只想遵循良好的设计模式并且不增加项目的 dexcount,因为它使用了许多库并且接近于 multi dex,这是我想避免的。

    编辑 : 我已经改变了我的第二个问题的措辞,因为它具有误导性。
    我只是想在两个模块中添加一些相同的依赖项,因为我可能会从 myNewModule 中创建一个单独的库,然后它仍然需要例如一个 okHttpClient 并且不能再依赖主项目中的依赖项。

    最佳答案

    If I have a dependency for the same library in different modules, will Android recognize it as one dependency or will it double the dexcount for the apk?



    Gradle 会自动处理它。
    如果您在库和使用该库的模块中添加相同的依赖关系,gradle 将添加它们 仅一次 .

    Would this be a good way to define dependencies? In my case, the myNewModule will contain dependencies, which are only needed in it, but also dependencies, that are needed in the app module.



    在我看来不是。
    您的库应该包含 仅需要的依赖项 .
    没有理由也包括将由将使用它的模块使用的依赖项。

    Or else, could I have put common dependencies directly into the build.gradle file from the Project?



    这个问题不是很清楚。
    每个模块都应该有其依赖关系,但您可以按照您在问题中描述的相同方式集中它们。

    关于模块的 Android 设置库依赖项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39774382/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com