栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Gradle-下载依赖项,锁定版本并手动更新依赖项

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Gradle-下载依赖项,锁定版本并手动更新依赖项

我的解决方案适用于使用

java
android
插件进行Gradle配置。

java
插件定义
compile
testCompile
配置。
compile
用于编译项目生产源所需的依赖项。
testCompile
用于编译项目测试源所需的依赖项。

让我们在中定义自己的配置

build.gradle

configurations {    download    testDownload}

接下来让我们创建目录:

  • libs/compile/downloaded
    download
    将存储依赖项的位置;
  • libs/testCompile/downloaded
    testDownload
    将存储依赖项的位置。

接下来,我们定义几个任务。

download
配置中删除依赖项:

task cleanDownloadedDependencies(type: Delete) {    delete fileTree('libs/compile/downloaded')}

testDownload
配置中删除依赖项:

task cleanDownloadedTestDependencies(type: Delete) {    delete fileTree('libs/testCompile/downloaded')}

download
配置下载依赖项:

task downloadDependencies(type: Copy) {    from configurations.download    into "libs/compile/downloaded/"}

testDownload
配置下载依赖项:

task downloadTestDependencies(type: Copy) {    from configurations.testDownload    into "libs/testCompile/downloaded/"}

执行上述所有任务以更新依赖关系:

task updateDependencies {    dependsOn cleanDownloadedDependencies, cleanDownloadedTestDependencies, downloadDependencies, downloadTestDependencies}

接下来,我们定义依赖项:

dependencies {    download( 'com.google.pre.gson:gson:+', 'joda-time:joda-time:+',    )    testDownload( 'junit:junit:+'    )

然后,我们告诉哪里

compile
testCompile
配置应使用用于编译的依赖项。

    compile fileTree(dir: 'libs/compile', include: '***.jar')}

现在,您可以下载或更新已经下载的依赖项:

./gradlew updateDependencies

如果您使用的是

android
插件,则还可以为
androidTestDownload
在Android设备上编译和运行测试所需的依赖项添加配置。还可以将某些依赖项作为
aar
工件提供。

这是使用

android
插件配置Gradle的示例:

...repositories {    ...    flatDir {        dirs 'libs/compile', 'libs/compile/downloaded',     'libs/testCompile', 'libs/testCompileDownloaded',     'libs/androidTestCompile', 'libs/androidTestCompile/downloaded'    }}configurations {    download    testDownload    androidTestDownload}android {    ...}dependencies {    download( 'com.android.support:support-v4:+', 'com.android.support:appcompat-v7:+', 'com.google.android.gms:play-services-location:+', 'com.facebook.android:facebook-android-sdk:+', 'com.vk:androidsdk:+', 'com.crashlytics.sdk.android:crashlytics:+', 'oauth.signpost:signpost-core:+', 'oauth.signpost:signpost-commonshttp4:+', 'org.twitter4j:twitter4j-core:+', 'commons-io:commons-io:+', 'com.google.pre.gson:gson:+', 'org.jdeferred:jdeferred-android-aar:+'    )    compile fileTree(dir: 'libs/compile', include: '***.jar')    androidTestCompile fileTree(dir: 'libs/androidTestCompile', include: '***.aar')        .each { File file ->    dependencies.add("compile", [name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }, ext: 'aar'])}fileTree(dir: 'libs/testCompile', include: '***.aar')        .each { File file ->    dependencies.add("androidTestCompile", [name: file.name.lastIndexOf('.').with { it != -1 ? file.name[0..<it] : file.name }, ext: 'aar'])}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/413955.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号