简体   繁体   中英

Duplicated classes from two libraries in android project

I have a two my own android libraries libA and libB, which use protobuf. When i add one of them in my android app project all is fine, but if i add both i have an error when i build app project:

Duplicate class com.google.api.Advice found in modules classes.jar (libA) and classes.jar (libB)
Duplicate class com.google.api.Advice$1 found in modules classes.jar (libA) and classes.jar (libB)
Duplicate class com.google.api.Advice$Builder found in modules classes.jar (libA) and classes.jar (libB)
Duplicate class com.google.api.AdviceOrBuilder found in modules classes.jar (libA) and classes.jar (libB)
Duplicate class com.google.api.AnnotationsProto found in modules classes.jar (libA) and classes.jar (libB)
Duplicate class com.google.api.AuthProto found in modules classes.jar (libA) and classes.jar (libB)
Duplicate class com.google.api.AuthProvider found in modules classes.jar (libA) and classes.jar (libB)
Duplicate class com.google.api.AuthProvider$1 found in modules classes.jar (libA) and classes.jar (libB)
Duplicate class com.google.api.AuthProvider$Builder found in modules classes.jar (libA) and classes.jar (libB)
Duplicate class com.google.api.AuthProviderOrBuilder found in modules classes.jar (libA) and classes.jar (libB)
Duplicate class com.google.api.AuthRequirement found in modules classes.jar (libA) and classes.jar (libB)
.................

I use gradle in libraries projects and app project. I add libraries in app project as:

dependencies {
    implementation "com.libA"
    implementation "com.libB"
}

In both libraries i have almost the same config:

apply plugin: 'com.android.library'
apply plugin: 'com.google.protobuf'
apply from: rootProject.file("gradle/versions.gradle")

android {
    compileSdkVersion versions.sdk.compile

    defaultConfig {
        minSdkVersion versions.min
        targetSdkVersion versions.compile
        versionCode versions.code
        versionName versions.name
    }

    sourceSets {
        main {
            jni.srcDirs = []
            jniLibs.srcDirs = ['src/main/libs']
        }
    }
}

protobuf {
    protoc {
        artifact = "com.google.protobuf:protoc:3.6.1"
    }
    plugins {
        javalite {
            artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
        }
        grpc {
            artifact = "io.grpc:protoc-gen-grpc-java:1.25.0"
        }
    }
    generateProtoTasks {
        all().each { task ->
            task.plugins {
                task.builtins {
                    remove java
                }
                javalite {}
                grpc {
                    // Options added to --grpc_out
                    option 'lite'
                }
            }
        }
    }
}

dependencies {
    protobuf "com.google.protobuf:protobuf-lite:3.0.1"
    protobuf "com.google.api.grpc:proto-google-common-protos:1.12.0"

    implementation "io.grpc:grpc-okhttp:1.25.0"
    implementation "io.grpc:grpc-protobuf-lite:1.25.0"
    implementation "io.grpc:grpc-stub:1.25.0"
    
    //other dependencies 
}

In result i have same classes in both dependencies (screenshort one of them), this throws an duplicate error: libraries

Exclude group/module does not help. How can i resolve this problem?

I found a solution. I have created a third library with generator only and add it as a dependency to my libA and libB. This is not quite the correct architectural solution, but so far it works. I will work on the best solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM