Search code examples
androidreact-nativeapkgstreamerdex

How can I fix "Type org.freedesktop.gstreamer.GStreamer is defined multiple times" error when building an Android APK?


I have been working on a project for a couple of months integrating gstreamer to a basic react-native Android app. The starting point was react-native-gstreamer but with a number of modifications to the gstreamer pipeline etc.

My debug builds (created with yarn android or ./gradlew assembleDebug) have been working reliably, on emulated and real devices, but I can't get a release build / signed APK ./gradlew assembleRelease to work. The build scan showing the error (created with ./gradlew assembleRelease --scan, doesn't appear to contain any sensitive information?) is at scans.gradle.com. The error I am stuck on is

C:\Temp\MyApp\app\intermediates\project_dex_archive\release\out\org\freedesktop\gstreamer\GStreamer.dex: D8: Type org.freedesktop.gstreamer.GStreamer is defined multiple times: C:\Temp\MyApp\app\intermediates\project_dex_archive\release\out\org\freedesktop\gstreamer\GStreamer.dex, C:\Temp\MyApp\react-native-gstreamer.transforms\b22258b54d8d354026f3fb18b0251db4\transformed\classes\classes.dex com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:
Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.

I'm new to Android development (experienced C++/Python mainly), so I could be missing something obvious, this seems to be a common issue (see below) but the link in the error doesn't help me and the solutions I have found have not worked.

The most likely cause that I can't pin down is some sort of dependency clash, similar to this issue also, but I don't have lots of dependencies, it seems specifically related to gstreamer, and I don't know where to dig further to find the clash.

./gradlew app:dependencies doesn't show any obviously bad dependencies. react-native-gstreamer has a dependency on com.facebook.react:react-native:+ -> 0.63.4 (*) and doesn't appear in any other context in the dependencies (i.e. the above dependency appears a few times in the output, but react-native-gstreamer isn't a dependency of other packages and doesn't have other dependencies and there are no other occurrences of gstreamer)

Similar errors elsewhere on stackoverflow and solutions which I have unsuccessfully tested:

  • Delete gradle folder - fault still occurs
  • Enable multiDex in build.gradle - fault still occurs (note minSdkVersion is set to 21, so some messages suggest this should not be necessary, but tried anyway)
  • Similar to this but I am not aware of any dependency
  • ./gradlew clean and ./gradlew assembleRelease - fault still occurs
  • Various solutions
    • look at the path given in AS, it should point to \app\build\intermediates\project_dex_archive\debug\out. Go to this path, delete all the *.jar files, leaving the most recent one - fault still occurs (removed file is recreated on next build)
    • Delete the build directory (C:/Temp/MyApp above) - fault still occurs
  • Multidex changes - it looks like this is now outdated for minSdkVersion >= 21, no upvotes and one comment that it didn't fix the original poster's problem, not tested.

Solution

  • The error message mentions two dex sources which contain the org.freedesktop.gstreamer.GStreamer class:

    • C:\Temp\MyApp\app\intermediates\project_dex_archive\release\out\org\freedesktop\gstreamer\GStreamer.dex
    • C:\Temp\MyApp\react-native-gstreamer.transforms\b22258b54d8d354026f3fb18b0251db4\transformed\classes\classes.dex

    First source's project_dex_archive folder generally contains output artifacts which were built from project source files. Is it possible that your app module contains GStreamer.java source file?
    If yes - then it might be the root cause of the conflict, since you're most likely have similar artifact packaged in a third party dependency (the second dex source: react-native-gstreamer).