Lint错误:`httpclient`定义的类与Android现在提供的类冲突

5

我正在尝试通过遵循示例应用程序,将我的应用从已弃用的Android Drive API更新到Drive REST API。

当尝试构建签名发布APK时,我遇到了这个lint问题:

httpclient定义了与现在由Android提供的类冲突的类。解决方案包括查找更新版本或没有相同问题的替代库(例如,对于httpclient使用HttpUrlConnection或okhttp),或者使用jarjar之类的工具重新打包库。

以下是我的gradle依赖项:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.google.android.gms:play-services-auth:16.0.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support:customtabs:27.1.1'
    implementation 'com.google.http-client:google-http-client-gson:1.26.0'
    implementation('com.google.api-client:google-api-client-android:1.26.0') {
        exclude group: 'org.apache.httpcomponents'
    }
    implementation('com.google.apis:google-api-services-drive:v3-rev136-1.25.0') {
        exclude group: 'org.apache.httpcomponents'
    }
}

我猜测问题的原因是:
    implementation 'com.google.http-client:google-http-client-gson:1.26.0'

我尝试了这里这里发布的解决方案,但它们都没有起作用。
就像这条评论所说的那样:

如果排除httpclient,我将无法获得发布apk。

还有什么其他解决方案吗?

我的建议是不要使用Google库,而是自己调用REST端点。 - pinoyyid
1个回答

8
请尝试将google-http-client-gson模块以及org.apache.httpcomponents模块排除在外。如果您的项目需要使用Apache HttpClient类,请使用包装器来提供它们,例如此包装器即使针对最新的SDK也能很好地工作。
我在app\build.gradle中使用以下代码块,它可以正常工作(我只使用旧版本的google-http-client-gson模块):
compile ('com.google.http-client:google-http-client-gson:1.19.0') {
    //Exclude conflicting modules
    exclude module: 'httpclient'
    exclude module: 'commons-logging'
}
//Add HttpClient classes from a different package
compile 'cz.msebera.android:httpclient:4.5.8'

那么您只需要将所有的org.apache.http导入更改为cz.msebera.android.httpclient,例如:

import org.apache.http.HttpResponse

变得

import cz.msebera.android.httpclient.HttpResponse

仅仅从 com.google.api-client:google-api-client 中排除这两个模块对我来说就可以了。我在我的代码中没有显式地使用这些库,因此不需要任何替换。 - Slav

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接