@react-native-async-storage/async-storage Fails to Compile on Android after Upgrading to RN 0.73.8? Here’s the Solution!
Image by Fannee - hkhazo.biz.id

@react-native-async-storage/async-storage Fails to Compile on Android after Upgrading to RN 0.73.8? Here’s the Solution!

Posted on

If you’re reading this, chances are you’re one of the unlucky ones who’ve stumbled upon the frustrating issue of @react-native-async-storage/async-storage failing to compile on Android after upgrading to React Native 0.73.8. Don’t worry, we’ve got you covered! In this article, we’ll take a deep dive into the problem, explore the possible causes, and provide a step-by-step guide to get you back on track.

What’s the Issue?

@react-native-async-storage/async-storage is a popular library used for storing data locally in React Native applications. After upgrading to React Native 0.73.8, many developers have reported issues with the library failing to compile on Android. The error message typically looks something like this:

BUILD FAILDED

* What went wrong:
Execution failed for task ':app:mergeDexDebug'.
> com.android.build.api.transform.TransformException: Error while generating the main dex list:
  Error while merging dex archives:
    Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.
    Program type already present: androidx.window.R$attr

What’s Causing the Issue?

There are a few possible reasons why @react-native-async-storage/async-storage might be failing to compile on Android after upgrading to React Native 0.73.8. Here are some potential culprits:

  • Dependency conflicts: React Native 0.73.8 introduces some changes to the AndroidX library, which might conflict with the dependencies required by @react-native-async-storage/async-storage.
  • Duplicate classes: The error message above hints at duplicate classes being present in the dex archives. This could be due to multiple libraries including the same classes or versions.
  • AndroidX compatibility: @react-native-async-storage/async-storage might not be fully compatible with the latest AndroidX versions used in React Native 0.73.8.

Solution: Step-by-Step Guide

Fear not, dear developer! We’ve got a comprehensive solution to get you back up and running. Follow these steps carefully, and you’ll be storing data like a pro in no time:

  1. Update @react-native-async-storage/async-storage: Make sure you’re running the latest version of @react-native-async-storage/async-storage. Run the following command in your terminal:
npm install @react-native-async-storage/async-storage@^1.15.0
  1. Check your AndroidX versions: Ensure that your project is using the correct AndroidX versions compatible with React Native 0.73.8. In your android/build.gradle file, update the following lines:
android {
  ...
  defaultConfig {
    ...
    minSdkVersion 21
    targetSdkVersion 29
    ...
  }
  ...
  compileSdkVersion 29
  ...
  androidXVersion = '1.7.0-alpha01'
}
  1. Exlude duplicate classes: To resolve the duplicate classes issue, we need to exclude the androidx.window package from the @react-native-async-storage/async-storage library. In your android/app/build.gradle file, add the following lines:
android {
  ...
  defaultConfig {
    ...
    packagingOptions {
      exclude 'androidx/window/R$attr.class'
    }
  }
}
  1. Configure multiDex: Since we’re dealing with a large number of dependencies, we need to enable multiDex to avoid the 65K method limit. In your android/app/build.gradle file, add the following lines:
android {
  ...
  defaultConfig {
    ...
    multiDexEnabled true
  }
  ...
  dependencies {
    implementation 'com.android.support:multidex:2.0.1'
  }
}

Additional Tips and Tricks

To ensure a smooth experience, keep the following tips in mind:

  • Make sure you’ve cleaned and rebuilt your project: After making changes to your Gradle files, it’s essential to clean and rebuild your project to reflect the changes. Run the following commands:
cd android
./gradlew clean
./gradlew build
  • Check for other dependency conflicts: If you’re still facing issues, try checking for other dependency conflicts by running the following command:
npx react-native run-android --verbose
  • Report issues and contribute to the community: If you’ve encountered an issue that’s not mentioned here, please report it to the @react-native-async-storage/async-storage community. Your contributions will help make the library better for everyone!

Conclusion

Upgrading to React Native 0.73.8 shouldn’t mean sacrificing the functionality of your app. By following this guide, you should be able to overcome the @react-native-async-storage/async-storage compilation issues on Android. Remember to stay vigilant, and don’t hesitate to reach out to the community if you encounter any further problems.

Happy coding, and may your data be stored effortlessly!

Keyword Description
@react-native-async-storage/async-storage A popular library for storing data locally in React Native applications.
React Native 0.73.8 The latest version of React Native, which introduces changes to the AndroidX library.
AndroidX A set of Android libraries that provide a more consistent, backward-compatible, and extensible platform for Android app development.

Frequently Asked Question

Got stuck with “@react-native-async-storage/async-storage” issues after upgrading to RN 0.73.8 on Android? Don’t worry, we’ve got your back!

Q1: What is the main cause of the “@react-native-async-storage/async-storage” compilation failure on Android after upgrading to RN 0.73.8?

The main culprit behind this error is the outdated “react-native-async-storage” version. When you upgrade to RN 0.73.8, the “react-native-async-storage” version needs to be updated to a compatible version, specifically 1.17.3 or higher, to avoid compilation issues on Android.

Q2: How do I update “react-native-async-storage” to a compatible version to resolve the compilation issue on Android?

To update “react-native-async-storage”, run the command `npm install @react-native-async-storage/[email protected]` or `yarn add @react-native-async-storage/[email protected]` in your project’s root directory. Then, make sure to clean and rebuild your project by running `npx react-native run-android` or `npx react-native start` followed by `npx react-native run-android`.

Q3: What if I’m still facing issues after updating “react-native-async-storage” to a compatible version?

If updating “react-native-async-storage” doesn’t resolve the issue, try cleaning the project by running `cd android && ./gradlew clean && cd ..`. Then, reinstall the node modules using `npm install` or `yarn install`, and finally, rebuild the project using `npx react-native run-android`. If the issue persists, try resetting the metro cache using `npx react-native start –reset-cache`.

Q4: Are there any specific Android configurations that might affect the compilation of “@react-native-async-storage/async-storage” after upgrading to RN 0.73.8?

Yes, ensure that your `android/build.gradle` file has the correct configuration. Specifically, check that the `android/defaultConfig` section has the correct `targetSdkVersion` and `compileSdkVersion` values, which should be 31 or higher for RN 0.73.8. Also, make sure that the `android/app/build.gradle` file has the correct `reactNativeVersion` and `targetSdkVersion` values.

Q5: Can I use the “@react-native-async-storage/async-storage” version 1.17.3 with React Native 0.73.8 on iOS?

Yes, the “@react-native-async-storage/async-storage” version 1.17.3 is compatible with React Native 0.73.8 on both Android and iOS. You can use the same version of “react-native-async-storage” for your iOS project, and it should compile and work as expected.

Leave a Reply

Your email address will not be published. Required fields are marked *