'Duplicate R.class after renaming package name for my Android Studio Flutter app

Tried renaming my Android Studio flutter app package, and it is tunring out as a mess. How can I solve this issue?

Build is failing with the error message:

Execution failed for task ':app:multiDexListDebug'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > Error while merging dex archives: 
     Type com.xxxxx.R is defined multiple times: 
....\AndroidStudioProjects\xxxx\build\app\intermediates\compile_and_runtime_not_namespaced_r_class_jar\debug\R.jar: D8: Type com.xxxx.R is defined multiple times: ....\AndroidStudioProjects\xxxx\build\app\intermediates\compile_and_runtime_not_namespaced_r_class_jar\debug\R.jar:com/xxxx/R.class, ....\AndroidStudioProjects\xxxx\build\app\intermediates\javac\debug\classes\com\xxxx\R.class

     Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.


Solution 1:[1]

Try to run flutter clean in the terminal first. Or remove /build directory by yourself manually.

Solution 2:[2]

  1. On terminal type flutter clean
  2. Restart VSCode
  3. Run flutter get package

Solution 3:[3]

this happens because the library you are using uses the same dependences.

SOLUTION 1

  1. in VSCode Search (Ctrl+Shift + P)
  2. in your error com.xxxxx.R. search for keywords from that error.
  3. If there are the same dependences, it means that there are duplicate dependences.
  4. Solution: Delete one of the package you are using.

SOLUTION 2 (Recommended)

  1. in VSCode Search (Ctrl+Shift + P)
  2. in your error com.xxxxx.R. search for keywords from that error.
  3. Find a library that uses the same dependences, then update to the latest package version

4.This solution will keep the library you are using without deleting it

Solution 4:[4]

Try flutter clean && flutter get package

And try to use a package https://pub.dev/packages/change_app_package_name

Steps:

dev_dependencies: 
  change_app_package_name: ^1.0.0

Update dependencies

flutter pub get

Run this command to change the package name.

flutter pub run change_app_package_name:main com.new.package.name

(like flutter pub run change_app_package_name:main com.example.app)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 saturov
Solution 2 Farid Istiqlal
Solution 3 Farid Istiqlal
Solution 4 Zakaria Hossain