How to resolve Duplicate class error with relocation e/ou shadow?

I'm writing a react-native package. This package has two libraries added manually. As you can see from the error, both implement the package com.realsil.sdk.*

I can't contact the library developer to request the change and I can't delete the files as both are important for the libraries to work correctly.

Searching, I saw that it is possible to rename the duplicated class using a relocation technique. However, this is not working well. The documentation seems to be out of date and I don't know any other way to fix this.

https://github.com/johnrengelman/shadow

Is it possible to solve this problem? The "shadowJar" task is not being found for me. What do the changes to the gradle build look like after this?

this is my build.gradle:

def DEFAULT_COMPILE_SDK_VERSION = 31
def DEFAULT_BUILD_TOOLS_VERSION = '28.0.3'
def DEFAULT_MIN_SDK_VERSION = 23
def DEFAULT_TARGET_SDK_VERSION = 31

def safeExtGet(prop, fallback) {
    rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def safeExtHasProp(prop){
    rootProject.ext.has(prop)
}

apply plugin: 'com.android.library'
apply plugin: 'maven-publish'

buildscript {
  
    if (project == rootProject) {
        repositories {
            google()
            jcenter()
            maven {
                url = uri("https://plugins.gradle.org/m2/")
            }
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.5.4'
            classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1"
            classpath "com.github.johnrengelman:shadow:8.1.0"
        }
    }
}

android {
    compileSdkVersion safeExtGet('compileSdkVersion', DEFAULT_COMPILE_SDK_VERSION)
    buildToolsVersion safeExtGet('buildToolsVersion', DEFAULT_BUILD_TOOLS_VERSION)
    defaultConfig {
        minSdkVersion safeExtGet('minSdkVersion', DEFAULT_MIN_SDK_VERSION)
        targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
        versionCode 1
        versionName "1.0"
        multiDexEnabled true

        ndk {
            abiFilters "arm64-v8a", "armeabi-v7a", "x86", "x86_64"
        }
    }
    lintOptions {
        abortOnError false
    }
    sourceSets {
        main {
            assets.srcDirs = ['assets']
        }
    }
    compileOptions {
        coreLibraryDesugaringEnabled true
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

repositories {
    // ref: https://www.baeldung.com/maven-local-repository
    mavenLocal()
    maven {
        // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
        url "$rootDir/../node_modules/react-native/android"
    }
    maven {
        // Android JSC is installed from npm
        url "$rootDir/../node_modules/jsc-android/dist"
    }
    google()
    jcenter()
}

dependencies {
    //noinspection GradleDynamicVersion
    implementation 'com.facebook.react:react-native:+'  // From node_modules

    implementation 'org.greenrobot:greendao:3.2.2'
    implementation 'com.alibaba:fastjson:1.2.35'
    implementation 'com.squareup.okhttp3:okhttp:3.4.1'
    implementation 'com.squareup.okio:okio:1.9.0'
    implementation 'com.google.code.gson:gson:2.8.0'
    implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
    implementation 'com.tencent.bugly:crashreport:latest.release'

    //KCT -- SDK--need
    implementation fileTree(dir: 'libs/kctblesdk', include: ['*.aar', '*.jar'], exclude: [])

    //IDO -- SDK--need
    implementation fileTree(dir: 'libs/idoblesdk', include: ['*.jar'], exclude: [])

    //SMA -- SDK--need
    debugImplementation fileTree(dir: 'libs/smablesdk/debug', include: ['*.aar', '*.jar'], exclude: [])
    releaseImplementation fileTree(dir: 'libs/smablesdk/release', include: ['*.aar', '*.jar'], exclude: [])

    //CRP -- SDK--need
    implementation fileTree(dir: 'libs/crpblesdk', include: ['*.aar', '*.jar'], exclude: [])

    //Esta dependência só deve ser adicionada em apps que usam a sdk SMA
    if(safeExtHasProp('iotLibsVersion')){
        implementation fileTree(dir: 'libs/smablesdk/iot_libs', include: ['*.aar', '*.jar'], exclude: [])
        implementation group: 'com.adups.iot', name: 'trace', version: '1.0.4'
        implementation group: 'com.adups.iot', name: 'mqtt_libs', version: '1.1.3'
        implementation group: 'com.adups.iot', name: 'iot_download_libs', version: '1.1.7'
        implementation group: 'com.adups.iot', name: 'http_libs', version: '1.0.8'
    }

    //SMA, IDO and KCT -- SDK -- need
    implementation 'no.nordicsemi.android:dfu:1.8.1'

    //For IDODataStorage
    def room_version = "2.4.1"
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    // RxJava support for Room - IDODataStorage
    implementation "androidx.room:room-rxjava2:$room_version"
    // Guava support for Room, including Optional and ListenableFuture - IDODataStorage
    implementation "androidx.room:room-guava:$room_version"
    // Test helpers - IDODataStorage
    testImplementation "androidx.room:room-testing:$room_version"

    //For IDOWorkManager
    def work_version = "2.7.0"
    implementation "androidx.work:work-runtime:$work_version"

    //Location Manager
    implementation 'com.google.android.gms:play-services-location:19.0.1'
    implementation 'com.google.android.gms:play-services-maps:18.0.2'
    coreLibraryDesugaring "com.android.tools:desugar_jdk_libs:1.1.1"

}

def configureReactNativePom(def pom) {
    def packageJson = new groovy.json.JsonSlurper().parseText(file('../package.json').text)

    pom.project {
        name packageJson.title
        artifactId packageJson.name
        version = packageJson.version
        group = "com.reactlibrary"
        description packageJson.description
        url packageJson.repository.baseUrl

        licenses {
            license {
                name packageJson.license
                url packageJson.repository.baseUrl + '/blob/master/' + packageJson.licenseFilename
                distribution 'repo'
            }
        }

        developers {
            developer {
                id packageJson.author.username
                name packageJson.author.name
            }
        }
    }
}

configurations {
    customConfig.extendsFrom implementation
}

afterEvaluate { project ->
    // some Gradle build hooks ref:
    // https://www.oreilly.com/library/view/gradle-beyond-the/9781449373801/ch03.html
    task androidJavadoc(type: Javadoc) {
        source = android.sourceSets.main.java.srcDirs
        classpath += files(android.bootClasspath)
        classpath += files(project.getConfigurations().getByName('customConfig').asList())
        include '**/*.java'
    }

    task androidJavadocJar(type: Jar, dependsOn: androidJavadoc) {
        classifier = 'javadoc'
        from androidJavadoc.destinationDir
    }

    task androidSourcesJar(type: Jar) {
        classifier = 'sources'
        from android.sourceSets.main.java.srcDirs
        include '**/*.java'
    }

    android.libraryVariants.all { variant ->
        def name = variant.name.capitalize()
        def javaCompileTask = variant.javaCompileProvider.get()

        task "jar${name}"(type: Jar, dependsOn: javaCompileTask) {
            from javaCompileTask.destinationDir
        }
    }

    artifacts {
        archives androidSourcesJar
        archives androidJavadocJar
    }

    publishing {
        repositories {
            maven {
                url = uri("${rootProject.projectDir}/maven-repo")
            }
        }
    }
}



This is de error:

Duplicate class com.realsil.sdk.core.base.BaseService found in modules jetified-IDoBLELib-VeryFit-2.67.66 (IDoBLELib-VeryFit-2.67.66.jar) and jetified-crpblelib-1.8.0-local-0926-runtime (crpblelib-1.8.0-local-0926.aar)
Duplicate class com.realsil.sdk.core.base.DaemonService found in modules jetified-IDoBLELib-VeryFit-2.67.66 (IDoBLELib-VeryFit-2.67.66.jar) and jetified-crpblelib-1.8.0-local-0926-runtime (crpblelib-1.8.0-local-0926.aar)
Duplicate class com.realsil.sdk.core.bluetooth.BluetoothProfileManager found in modules jetified-IDoBLELib-VeryFit-2.67.66 (IDoBLELib-VeryFit-2.67.66.jar) and jetified-crpblelib-1.8.0-local-0926-runtime (crpblelib-1.8.0-local-0926.aar)
Duplicate class com.realsil.sdk.core.bluetooth.BluetoothProfileManager$ProfileBroadcastReceiver found in modules jetified-IDoBLELib-VeryFit-2.67.66 (IDoBLELib-VeryFit-2.67.66.jar) and jetified-crpblelib-1.8.0-local-0926-runtime (crpblelib-1.8.0-local-0926.aar)
Duplicate class com.realsil.sdk.core.bluetooth.RtkBluetoothManager found in modules jetified-IDoBLELib-VeryFit-2.67.66 (IDoBLELib-VeryFit-2.67.66.jar) and jetified-crpblelib-1.8.0-local-0926-runtime (crpblelib-1.8.0-local-0926.aar)
Duplicate class com.realsil.sdk.core.bluetooth.RtkBluetoothManager$BluetoothBroadcastReceiver found in modules jetified-IDoBLELib-VeryFit-2.67.66 (IDoBLELib-VeryFit-2.67.66.jar) and jetified-crpblelib-1.8.0-local-0926-runtime (crpblelib-1.8.0-local-0926.aar)
Duplicate class com.realsil.sdk.dfu.DfuService found in modules jetified-IDoBLELib-VeryFit-2.67.66 (IDoBLELib-VeryFit-2.67.66.jar) and jetified-crpblelib-1.8.0-local-0926-runtime (crpblelib-1.8.0-local-0926.aar)
Duplicate class com.realsil.sdk.dfu.DfuService$a found in modules jetified-IDoBLELib-VeryFit-2.67.66 (IDoBLELib-VeryFit-2.67.66.jar) and jetified-crpblelib-1.8.0-local-0926-runtime (crpblelib-1.8.0-local-0926.aar)
Duplicate class com.realsil.sdk.dfu.DfuService$b found in modules jetified-IDoBLELib-VeryFit-2.67.66 (IDoBLELib-VeryFit-2.67.66.jar) and jetified-crpblelib-1.8.0-local-0926-runtime (crpblelib-1.8.0-local-0926.aar)
Duplicate class com.realsil.sdk.dfu.DfuService$c found in modules jetified-IDoBLELib-VeryFit-2.67.66 (IDoBLELib-VeryFit-2.67.66.jar) and jetified-crpblelib-1.8.0-local-0926-runtime (crpblelib-1.8.0-local-0926.aar)
Duplicate class com.realsil.sdk.dfu.DfuService$d found in modules jetified-IDoBLELib-VeryFit-2.67.66 (IDoBLELib-VeryFit-2.67.66.jar) and jetified-crpblelib-1.8.0-local-0926-runtime (crpblelib-1.8.0-local-0926.aar)
Duplicate class com.realsil.sdk.dfu.utils.AesJni found in modules jetified-IDoBLELib-VeryFit-2.67.66 (IDoBLELib-VeryFit-2.67.66.jar) and jetified-crpblelib-1.8.0-local-0926-runtime (crpblelib-1.8.0-local-0926.aar)

Upvotes: 1

Views: 205

Answers (0)

Related Questions