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

Android Gradle declare a Variant Build Flavor dependency using API and Exclude in KTS

我正在尝试构建我的应用程序的风格,其中包含非常重的依赖性,并且只会在某些构建中用于测试和离线开发(依赖性是 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 进行格式化并排除依赖符号,因为在将这些与构建风格结合起来时我找不到很多示例。

经过大量的尝试,我得到了:

// 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”来实际约束变体。