Warm tip: This article is reproduced from serverfault.com, please click

其他-Android Gradle 使用 API 声明 Variant Build Flavor 依赖项并在 KTS 中排除

(其他 - Android Gradle declare a Variant Build Flavor dependency using API and Exclude in KTS)

发布于 2021-01-14 20:49:22

我正在尝试构建我的应用程序的风格,其中包含非常重的依赖项,并且只会在某些构建中用于测试和离线开发(依赖项是 Android 的 Wiremock)。但是,我似乎找不到任何也使用 api() 和 exclude 的风味变体依赖项声明。

在我决定将依赖项移动到构建变体之前,我可以像这样声明依赖项:

        dependencies {
            //WireMock - Do not put in release builds bc of large size
            api("com.github.tomakehurst:wiremock:2.18.0") {
                exclude("org.apache.httpcomponents", "httpclient")
                exclude("org.ow2.asm", "asm")
                exclude("org.json", "json")
            }
            api("org.apache.httpcomponents:httpclient-android:4.3.5.1")
        }

我很想将这种依赖限制在我的构建风格上,我简单地称之为“模拟”,例如:

    dependencies: {
        "mockImplementation"(
               api("com.github.tomakehurst:wiremock:2.18.0") {
                    exclude("org.apache.httpcomponents", "httpclient")
                    exclude("org.ow2.asm", "asm")
                    exclude("org.json", "json")
            }
            api("org.apache.httpcomponents:httpclient-android:4.3.5.1")
        })
    }

这显然是非常错误的,但我不确定如何使用 api 进行格式化并排除依赖符号,因为在将这些与构建风格相结合时我找不到很多示例。

Questioner
astralbody888
Viewed
0
astralbody888 2021-01-28 03:39:28

经过大量玩耍后,我最终得到了:

// WireMock - Do not put in release builds bc of large size, restrict to mock flavors
"mockImplementation"(mockApi("com.github.tomakehurst:wiremock:2.18.0") {
    // Using Android Version Instead
    exclude("org.apache.httpcomponents", "httpclient")
    //Was getting a classpath conflict for org.objectweb.asm.AnnotationVisitor which is a part of 'net.minidev:asm'
    exclude("org.ow2.asm", "asm")
    //Was getting this warning, so decided to ignore this version included by WireMock.
    //Warning:Dependency org.json:json:20090211 is ignored as it may be conflicting with the internal version provided by Android.
    //In case of problem, please repackage with jar to change the class packages
    exclude("org.json", "json")
})
"mockImplementation"(mockApi("org.apache.httpcomponents:httpclient-android:4.3.5.1") {})

请注意,“mockApi”是必要的,而不仅仅是使用“api”来实际约束变体。