Android Gradle 3.0 更新属性

作者: Mark_Liu | 来源:发表于2018-09-03 17:45 被阅读27次

Dependency configurations

New configuration Deprecated configuration Behavior
implementation compile 编译时仅本模块可以使用
运行时其他模块也可以使用,提高编译速度
api compile 本模块和上游模块都可用
编译和运行时都可用
compileOnly provided 指向编译时的依赖库
库模块必须包含一个运行时条件
用于检测依赖项是否可用
runtimeOnly apk 只向构建输出添加依赖,供运行时使用
annotationProcessor compile 要添加对注释处理器库的依赖关系,
必须使用annotationProcessor配置
将其添加到注释处理器类路径中。

如果要添加特定的配置项,如下


configurations {
    freeDebugRuntimeoly{}
}

dependencies {
    freeDebugRuntimeOnly fileTree(dir: 'libs', include: ['*.jar'])
}

添加测试依赖库

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'
}

annotationProcessor的使用

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'
}

禁止annotation processor错误检测

android {
    ...
    defaultConfig {
        ...
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath false
            }
        }
    }
}

排除不需要的传递依赖项

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

排除不需要的传递测试依赖项

android.testVariants.all { variant ->
    variant.getCompileConfiguration().exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp'
    variant.getRuntimeConfiguration().exclude group: 'com.jakewharton.threetenabp', module: 'threetenabp'
}
参考资料

Add build dependencies

相关文章

网友评论

    本文标题:Android Gradle 3.0 更新属性

    本文链接:https://www.haomeiwen.com/subject/zmsiwftx.html