关于Google Drive api:Lint错误:`httpclient`定义了与Android现在提供的类冲突的类

Lint error: `httpclient` defines classes that conflict with classes now provided by Android

我正在尝试通过遵循示例应用程序将我的应用程序从不赞成使用的Android Drive API更新为Drive REST API。

当尝试构建签名的发行版APK时,出现以下问题:

httpclient defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don't have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar.

这是我的gradle依赖项:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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'
    }
}

我猜测是由于以下原因引起的:

1
    implementation 'com.google.http-client:google-http-client-gson:1.26.0'

我尝试了此处和此处发布的解决方案,但是它们不起作用。

喜欢此评论说:

I am not able to get release apk if exclude httpclient

还有什么其他解决方案?


尝试将org.apache.httpcomponents模块也从google-http-client-gson中排除。
然后,如果您的项目需要Apache HttpClient类,请使用一些package器来提供它们,例如即使以最新的SDK为目标,该软件也能很好地发挥作用。

我在app\\build.gradle中使用以下块,并且工作正常(我只是使用google-http-client-gson模块的旧版本):

1
2
3
4
5
6
7
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,例如

1
import org.apache.http.HttpResponse

成为

1
import cz.msebera.android.httpclient.HttpResponse