gradle学习之依赖项配置

2018-08-26  本文已影响0人  我就是杨过

依赖项类型

apply plugin: 'com.android.application'

android { ... }

dependencies {
    // Dependency on a local library module
    implementation project(":mylibrary")

    // Dependency on local binaries
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    // Dependency on a remote binary
    implementation 'com.example.android:app-magic:12.3'
}

本地库模块依赖项

本地二进制依赖项

远程二进制依赖项

依赖项配置

依赖项配置有很多种 列举如下 TODO 查看每个具体的用法 官网看的不是太懂
不同编译环境的依赖项配置
dependencies {
    freeImplementation 'com.google.firebase:firebase-ads:9.8.0'
}
不同的编译环境和生成类型
configurations {
    // Initializes a placeholder for the freeDebugRuntimeOnly dependency
    // configuration.
    freeDebugRuntimeOnly {}
}

dependencies {
    freeDebugRuntimeOnly fileTree(dir: 'libs', include: ['*.jar'])
}
为本地测试和检测测试添加implementation 代码如下:
dependencies {
    // Adds a remote binary dependency only for local tests.
    testImplementation 'junit:junit:4.12'

    // Adds a remote binary dependency only for the instrumented test APK.
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
在依赖中添加注释处理器到编译路径中,会出现错误,解决如下:
dependencies {
    // Adds libraries defining annotations to only the compile classpath.
    compileOnly 'com.google.dagger:dagger:version-number'
    // Adds the annotation processor dependency to the annotation processor classpath.
    annotationProcessor 'com.google.dagger:dagger-compiler:version-number'
}

排除可传递依赖项

dependencies {
    implementation('some-library') {
        exclude group: 'com.example.imgtools', module: 'native'
    }
}

使用变量控制依赖项管理

远程存储库

allprojects {
    repositories {
        jcenter()
        mavenCentral()
        mavenLocal()
    }
}
// 指定特定的Maven和常春藤存储库
allprojects {
    repositories {
        maven {
            url "https://repo.example.com/maven2"
        }
        maven {
            url "file://local/repo/"
        }
        ivy {
            url "https://repo.example.com/ivy"
        }
    }
}

依赖顺序

查看依赖关系树

解决重复的类错误

出现这种情况的原因:

上一篇 下一篇

猜你喜欢

热点阅读