如何解决react Native和android sdk之间的重复类错误?

5 android react-native

我有一个适用于我的 Android 应用程序的第三方图像识别 sdk 库。不,我想使用本机模块将其集成到我的反应本机项目中。我的sdk代码和react本机代码之间存在一些冲突。

我尝试通过以下代码消除本机反应的冲突

implementation ("com.facebook.react:react-native:+") {
        exclude group: "com.facebook.yoga", module: "proguard-annotations"
    }
Run Code Online (Sandbox Code Playgroud)

我的错误如下所示

Duplicate class com.facebook.proguard.annotations.DoNotStrip found in modules 3rd party sdk (3rdpartysdk.aar) and jetified-proguard-annotations-1.19.0 (com.facebook.yoga:proguard-annotations:1.19.0)

Duplicate class com.facebook.proguard.annotations.KeepGettersAndSetters found in modules 3rd party sdk (3rdpartysdk.aar) and jetified-react-native-0.67.1-runtime (com.facebook.react:react-native:0.67.1)
Run Code Online (Sandbox Code Playgroud)

我已经尝试了互联网上的几种方法,但似乎没有任何帮助

反应本机:0.67.1

Fco*_* P. 0

这非常棘手。在不知道第三方库的情况下,替代方案是:

1.-从根的 gradle 文件中删除冲突的传递依赖项。与您正在做的类似,但针对整个路径,您可以根据需要自定义它:

subprojects {
    afterEvaluate {project ->

        project.configurations.all {
            resolutionStrategy.eachDependency { DependencyResolveDetails details ->
                if (details.requested.group == 'com.facebook.react' && details.requested.name.equals('react-native')){
                    //add exclusion rules here
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

2.-从冲突的库之一中删除冲突的类。鉴于该类位于路径中,它可能会起作用,但这取决于该类的使用方式。这里的另一个替代方案是从所有库中删除该类,然后在所有受影响的库中插入动态依赖项。因此,您必须使用复制任务,如下所示。理论例子:

task unzipJar(type: Copy) {
   from zipTree('$yourLibrary.aar')
   into ("$buildDir/libs/$yourLibrary")
   include "**/*.class"
   exclude "**/Unmodifiable.class"
}

subprojects {
    afterEvaluate {project ->

        project.configurations.all {
            resolutionStrategy.eachDependency { DependencyResolveDetails details ->
                if (details.requested.group == 'com.facebook.react' && details.requested.name.equals('react-native')){
                    files("$buildDir/libs/$yourLibrary") {
                       builtBy "unzipJar"
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

另一种方法是运行 unzipJar 任务,然后将生成的 aar 放入本地 Maven 存储库中,这样您就可以正常替换依赖项。祝你好运。