33
* What went wrong:
Execution failed for task ':app:mergeDebugNativeLibs'.
> A failure occurred while executing com.android.build.gradle.internal.tasks.Workers$ActionFacade
   > More than one file was found with OS independent path 'lib/armeabi-v7a/libfbjni.so'. If you are using jniLibs and CMake IMPORTED targets, see https://developer.android.com/studio/preview/features#automatic_packaging_of_prebuilt_dependencies_used_by_cmake

In app/build.gradle file

packagingOptions {
  pickFirst 'lib/x86/libc++_shared.so'
  pickFirst 'lib/x86_64/libc++_shared.so'
  pickFirst 'lib/arm64-v8a/libc++_shared.so'
  pickFirst 'lib/armeabi-v7a/libc++_shared.so'
}

still giving error....

Tried

In app/build.gradle file

    packagingOptions {
      pickFirst 'lib/x86/libfbjni.so'
      pickFirst 'lib/x86_64/libfbjni.so'
      pickFirst 'lib/arm64-v8a/libfbjni.so'
      pickFirst 'lib/armeabi-v7a/libfbjni.so'
   }

but it giving following error

More than one file was found with OS independent path 'lib/armeabi-v7a/libc++_shared.so'.
pL3b
  • 1,155
  • 1
  • 3
  • 18
Vaishnavi Maske
  • 421
  • 1
  • 3
  • 11
  • 2
    [Try this](https://github.com/facebook/react-native/issues/35210). It is a general problem – BK52 Nov 05 '22 at 12:19

4 Answers4

70

==OLD SOLUTION==

The fix for current react-native

We're suggesting all users of React Native to apply this fix to your top level build.gradle file as follows:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    // ...
}


allprojects {
    repositories {
+       exclusiveContent {
+           // We get React Native's Android binaries exclusively through npm,
+           // from a local Maven repo inside node_modules/react-native/.
+           // (The use of exclusiveContent prevents looking elsewhere like Maven Central
+           // and potentially getting a wrong version.)
+           filter {
+               includeGroup "com.facebook.react"
+           }
+           forRepository {
+               maven {
+                   // NOTE: if you are in a monorepo, you may have "$rootDir/../../../node_modules/react-native/android"
+                   url "$rootDir/../node_modules/react-native/android"
+               }
+           }
+       }
        // ...
    }
}

What this fix will do is apply an exclusiveContent resolution rule that will force the resolution of React Native Android library, to use the one inside node_modules.

Once you update your app to React Native v0.71.0, this fix won't be needed anymore.

==NEW SOLUTION==

We have prepared releases for all the main versions of react-native with an hotfix:

0.70.5: https://github.com/facebook/react-native/releases/tag/v0.70.5

️ 0.69.7: https://github.com/facebook/react-native/releases/tag/v0.69.7

0.68.5: https://github.com/facebook/react-native/releases/tag/v0.68.5

️ 0.67.5: https://github.com/facebook/react-native/releases/tag/v0.67.5

️ 0.66.5: https://github.com/facebook/react-native/releases/tag/v0.66.5

️ 0.65.3: https://github.com/facebook/react-native/releases/tag/v0.65.3

️ 0.64.4: https://github.com/facebook/react-native/releases/tag/v0.64.4

️ 0.63.5: https://github.com/facebook/react-native/releases/tag/v0.63.5

By updating to these patch versions, your Android build should start working again.

To do so, in your package.json change react-native's version to the relevant new patch (ex. if you are on 0.64.3, change to 0.64.4) and run yarn install. No other changes should be necessary, but you might want to clean your android artifacts with a cd android && ./gradlew clean before trying to re-run your Android app.

== Fix for older react-native (< 0.63) ==

The fix above only works on gradle 6.2 and higher. Older react-native used older gradle.

You may determine your gradle version by looking in your /android/gradle/wrapper/gradle-wrapper.properties file.

If you are on an older version of react-native (for example 0.63 or earlier) that uses gradle version 6.1 or below, you must use a different workaround as gradle 6.1 does not support exclusiveContent.

Add this in the allprojects area of your android/buld.gradle file.

def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())

allprojects {
    configurations.all {
        resolutionStrategy {
            // Remove this override in 0.65+, as a proper fix is included in react-native itself.
            force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
        }
    }

Instead of using exclusiveContent, the hotfix must force the version of React Native. The recommended hotfix shells out to node to read your current version of react-native. If you hard code the version of react-native, when you upgrade your project in the future, your build will fail if you forget to remove this hotfix.

Note that this fix is fragile as the location of your package.json could be different if you are in a monorepo, and node might not be available if you use a node package manager like nvm.

Source: https://github.com/facebook/react-native/issues/35210

RamaProg
  • 1,106
  • 7
  • 15
  • Thank you so much, seems to work. Can you explain, why it happens now, because we did not change our setup and on day (I think yesterday or day before) it suddenly failed. – Aitch Nov 10 '22 at 09:14
  • If you are on an older version of react native like I am ( 0.60.6 ), hopefully this will provide clues https://github.com/facebook/react-native/issues/35210 – Ed of the Mountain Nov 10 '22 at 21:07
  • @aitch it's explained in the source. "On November 4th 2022 we published the React Native version 0.71.0-rc0, the first release candidate for the 71 release series, on several public repositories. Specifically, this version was also published on Maven Central as we're updating our artifacts distribution strategy (technical details are explained below). This event resulted in build failures for Android on several users as they ended up downloading the wrong React Native version (0.71.0-rc0 instead of the version they were using in their project, say 0.68.0)." – RamaProg Nov 10 '22 at 21:39
  • 1
    Updated from 0.67.3 to 0.67.5. Still got the error – Sher Mi Nov 14 '22 at 08:32
  • delete node_modules and install again – RamaProg Nov 14 '22 at 09:45
10

Under allProjects-build.gradle-repositories add below code. It worked for an old project that I have been working on.

 exclusiveContent {
       filter {
           includeGroup "com.facebook.react"
       }
       forRepository {
           maven {
               // NOTE: if you are in a monorepo, you may have "$rootDir/../../../node_modules/react-native/android"
               url "$rootDir/../node_modules/react-native/android"
           }
       }
   }
Jimmy James
  • 101
  • 3
5

Goto to yourProject/app/build.gradle inside android{}

If you look at the error message very well, you will realise that the library is not among the pickFirst list. i.e lib/armeabi-v7a/libc++_shared

But remember, you will have to include all the react architectures options in the pickFirst list. i.e armeabi-v7a, arm64-v8a, x86, x86_64.

Do this:

android { 
        packagingOptions { 
            pickFirst 'lib/armeabi-v7a/libc++_shared.so' 
            pickFirst 'lib/arm64-v8a/libc++_shared.so' 
            pickFirst 'lib/x86/libc++_shared.so' 
            pickFirst 'lib/x86_64/libc++_shared.so' 
        } 
    }

Or You can just do this: This is also good if you don't know the name of the library

android { 
        packagingOptions { 
            pickFirst '**/*.so' 
        } 
    }
hkniyi
  • 263
  • 3
  • 7
  • putting that 'android' block in global scope of build.gradle is not working, where to put it pls? – Dee Jul 12 '23 at 18:41
  • tks, this works, that android block is another build.gradle file in `android/app/` not the one in `android/` – Dee Jul 12 '23 at 18:47
3

See below article if you are on older react-native

Fix for older react-native (< 0.63)

The fix above only works on gradle 6.2 and higher. Older react-native used older gradle.

You may determine your gradle version by looking in your /android/gradle/wrapper/gradle-wrapper.properties file.

If you are on an older version of react-native (for example 0.63 or earlier) that uses gradle version 6.1 or below, you must use a different workaround as gradle 6.1 does not support exclusiveContent.

Add this in the allprojects area of your android/buld.gradle file.

def REACT_NATIVE_VERSION = new File(['node', '--print',"JSON.parse(require('fs').readFileSync(require.resolve('react-native/package.json'), 'utf-8')).version"].execute(null, rootDir).text.trim())

allprojects {
    configurations.all {
        resolutionStrategy {
            // Remove this override in 0.65+, as a proper fix is included in react-native itself.
            force "com.facebook.react:react-native:" + REACT_NATIVE_VERSION
        }
    }
Ed of the Mountain
  • 5,219
  • 4
  • 46
  • 54
  • Solved for me ... thanks! AndroidRuntime: java.lang.NoSuchMethodError: No interface method putMap(Ljava/lang/String;Lcom/facebook/react/bridge/ReadableMap;)V in class Lcom/facebook/react/bridge/WritableMap; or its super classes (declaration of 'com.facebook.react.bridge.WritableMap' – jpmc Jun 11 '23 at 05:20