OAuth in Android application

2020-03-30 08:24发布

I need to use OAuth protocol in my Android application. I decided to use android-oauth-client. In my build.gradle file I have:

dependencies {        
    compile 'com.android.support:appcompat-v7:21.0.0'
    compile 'com.wu-man:android-oauth-client:0.0.3'    
}

Sync project with gradle files works fine, but when I try to build my application, there is a problem:

Error:Execution failed for task ':app:dexDebug'. com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.7.0_51\bin\java.exe'' finished with non-zero exit value 2

I found that I should try to add multiDexEnabled true in defaultConfig section in build.gradle. The first problem is solved, but there is another one:

Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'. java.util.zip.ZipException: duplicate entry: android/support/v4/accessibilityservice/AccessibilityServiceInfoCompat$AccessibilityServiceInfoIcsImpl.class

I have no idea how can I fix it and how can I build my project. Can you help me, please? Thanks in advance.

1条回答
【Aperson】
2楼-- · 2020-03-30 08:49

The support-v4 transitive dependency is being added twice. You can verify it using either dependencies task or dependency insight report:

MacBook-Pro:demo alex$ ./gradlew app:dependencies --configuration compile

or:

MacBook-Pro:demo alex$ ./gradlew app:dependencyInsight --configuration compile --dependency support-v4

Where demo is the root directory of your project. The output for compile configuration will be:

compile - Classpath for compiling the main sources.
+--- com.android.support:appcompat-v7:21.0.0
|    \--- com.android.support:support-v4:21.0.0
|         \--- com.android.support:support-annotations:21.0.0
\--- com.wu-man:android-oauth-client:0.0.3
     +--- com.google.oauth-client:google-oauth-client-java6:1.15.0-rc
     |    \--- com.google.oauth-client:google-oauth-client:1.15.0-rc
     |         +--- com.google.http-client:google-http-client:1.15.0-rc
     |         |    \--- com.google.code.findbugs:jsr305:1.3.9
     |         \--- com.google.code.findbugs:jsr305:1.3.9
     +--- com.google.http-client:google-http-client-jackson:1.15.0-rc
     |    +--- com.google.http-client:google-http-client:1.15.0-rc (*)
     |    \--- org.codehaus.jackson:jackson-core-asl:1.9.11
     +--- com.google.android:support-v4:r7
     \--- com.google.api-client:google-api-client-android:1.15.0-rc
          +--- com.google.api-client:google-api-client:1.15.0-rc
          |    \--- com.google.oauth-client:google-oauth-client:1.15.0-rc (*)
          \--- com.google.http-client:google-http-client-android:1.15.0-rc
               \--- com.google.http-client:google-http-client:1.15.0-rc (*)

And the output of dependency insight report will be:

:app:dependencyInsight
com.android.support:support-v4:21.0.0
\--- com.android.support:appcompat-v7:21.0.0
     \--- compile

com.google.android:support-v4:r7
\--- com.wu-man:android-oauth-client:0.0.3
     \--- compile


Note that both appcompat-v7 and android-oauth-client depend on support-v4. You can easily solve it by excluding the android-oauth-client's dependency:

dependencies {
    compile 'com.android.support:appcompat-v7:21.0.0'
    compile ('com.wu-man:android-oauth-client:0.0.3') {
        exclude group: 'com.google.android', module: 'support-v4'
    }
}
查看更多
登录 后发表回答