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



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

我已经尝试通过以下代码从本地反应中删除冲突

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

我的错误如下

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)

我在互联网上尝试了几种方法,但似乎都没有帮助

天然反应:0.67.1

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

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
}
}
}
}
}

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"
}
}
}
}
}
}

另一种选择是运行unzip Jar任务,然后将生成的aar放入本地maven repo中,这样就可以正常替换依赖关系。祝你好运

最新更新