Resolving the libc++_shared.so Conflict in NativeScript projects

background_app_fix

Android development can be a challenging endeavor, especially when dealing with native libraries and dependencies. A common issue that developers often encounter is a conflict arising from multiple instances of the same native library being included in the project. A typical example of this is the libc++_shared.so conflict. In this article, we will explore what this error is, why it occurs, and how to resolve it effectively.

The error message typically looks something like this:

Execution failed for task ':app:mergeDebugNativeLibs'.
2 files found with path 'lib/arm64-v8a/libc++_shared.so' from inputs:
 - /Users/minhnhut/.gradle/caches/transforms-3/cd760d28106a1b4d4e96396bcec235da/transformed/jetified-nativescript-optimized-with-inspector/jni/arm64-v8a/libc++_shared.so
 - /Users/minhnhut/.gradle/caches/transforms-3/5f9aea1bacbd967e47cda037c65bd0e3/transformed/jetified-PdfiumAndroid-1.0.1/jni/arm64-v8a/libc++_shared.so
If you are using jniLibs and CMake IMPORTED targets, see
https://developer.android.com/r/tools/jniLibs-vs-imported-targets

TL;DR

For the error message mentioned above, it was caused by the @nativescript/pdf plugin attempting to require the libc++_shared.so library again, which was already present. To resolve this, ensure that both mentioned instances of the libc++_shared.so library are the same version. Then, simply add the following instruction to App_Resources/Android/app.gradle:

packagingOptions {
    pickFirst 'lib/*/libc++_shared.so'
}

Understanding the Error

The error message you’re seeing indicates a conflict in your Android project, specifically related to the inclusion of the libc++_shared.so library from two different sources. This typically happens when you have multiple dependencies in your project that include the same native library.

Here’s a breakdown of the error message:

  1. Multiple Instances of the Same Library: The file libc++_shared.so is being included from two different paths in your project. This is causing a conflict because Android doesn’t know which version of the library to use.
  2. Sources of the Conflict: In the example above, the conflict arises from two dependencies:
    • nativescript-optimized-with-inspector
    • PdfiumAndroid-1.0.1
  3. JNI Libraries and CMake: The error message suggests that if you are using JNI libraries and CMake IMPORTED targets, you should refer to a specific Android developer guide. This guide can help in resolving conflicts between JNI libraries and CMake IMPORTED targets.

In the above example, an error message occurs after I install the @nativescript/pdf plugin, which introduces PdfiumAndroid-1.0.1. Unfortunately, this native dependency requires the libc++_shared.so library, which apparently is already required somewhere by other plugins.

Resolving the Conflict

To resolve this issue, you have a few options:

  • Exclude the Library from One of the Dependencies: If one of your dependencies doesn’t strictly require libc++_shared.so, you can exclude this library from being packaged with it. This can usually be done through Gradle configuration.
  • Ensure Consistent Version Across Dependencies: If both dependencies require the library, ensure they are using the same version. This might involve updating one of the dependencies to align the versions of libc++_shared.so.
  • Manual Resolution: Manually resolve the conflict by ensuring that only one instance of libc++_shared.so is included in your project. This could involve custom gradle scripting or manual manipulation of your project’s build files.
  • Consult the Provided Guide: As suggested in the error message, consult the Android developer guide on JNI Libraries and CMake IMPORTED targets for detailed instructions on handling such conflicts.

Conclusion

It’s important to carefully consider the dependencies of your project and how they interact, especially when dealing with native libraries. Mismanagement of these libraries can lead to runtime issues, even if the build process completes successfully.

Leave a Reply