Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Android Duplicate file error when generating apk

When I am trying to generate android apk by using ./gradlew installRelease, I get this error in console:

~/React-Native/mockingbird/android/app/build/intermediates/res/merged/release/drawable-mdpi-v4/src_resources_img_loading.gif: error: Duplicate file. ~/React-Native/mockingbird/android/app/build/intermediates/res/merged/release/drawable-mdpi/src_resources_img_loading.gif: Original is here. The version qualifier may be implied. 

I tried Build->Clean Project via Android Studio and ran ./gradlew installRelease again; it didn't work either.

Also, I tried deleting the build folder, but it doesn't help either.

like image 225
Shongsu Avatar asked Nov 02 '17 21:11

Shongsu


People also ask

How do you solve duplicate resources in React Native?

However, in case you still have the “duplicate resources” error, the way to fix it is simple. Go to android/app/src/main/res and delete all the directories starting with drawables. This is similar to what we did for earlier versions of React Native.

How do I resolve duplicate resources in android?

Remove the files you might have on: android/app/src/main/res/drawable-mdpi/ android/app/src/main/res/drawable-xhdpi/ android/app/src/main/res/drawable-xxhdpi/ Run Build again, This fixed the issue for me.

What is duplicate resource error in Android Studio?

It seems like you have the same image resource in two files OR you added an additional, non-required file to the res folder. Rename or remove the other. You can check the required structure of the res folder and subfolders by downloading a fresh copy of WebViewGold from CodeCanyon.


1 Answers

Give some tips for you, hope it's work.

Update with "react": "16.7.0", "react-native": "0.57.8"

Custom node_modules/react-native/react.gradle to solve the Duplicate file error perfectly. Add following code into currentBundleTask's creation block (after doFirst block)

doLast {     def moveFunc = { resSuffix ->         File originalDir = file("${resourcesDir}/drawable-${resSuffix}");         if (originalDir.exists()) {             File destDir = file("$buildDir/../src/main/res/drawable-${resSuffix}");             ant.move(file: originalDir, tofile: destDir);         }     }     moveFunc.curry("ldpi").call()     moveFunc.curry("mdpi").call()     moveFunc.curry("hdpi").call()     moveFunc.curry("xhdpi").call()     moveFunc.curry("xxhdpi").call()     moveFunc.curry("xxxhdpi").call() } 

You can create script to do it automatically.

  1. Create android-react-gradle-fix file
  2. Create script android-release-gradle-fix.js file
  3. Update package.json file:

    "scripts": { "postinstall": "node ./android-release-gradle-fix.js" },

That's it! Run npm install to get awesome.

Note: If you run npm install on ci like jenkins, you may get error: postinstall: cannot run in wd %s %s (wd=%s) node => just use npm install --unsafe-perm instead

like image 88
Nhan Cao Avatar answered Sep 28 '22 13:09

Nhan Cao