Reaction-native link causes duplication of imports in Android.gradle settings

For some reason, when I call react-native link , it adds duplicate to android/settings.gradle , which in turn adds duplicate import to android/app/src/main/com/<projectName>/MainApplication.java .

It is not always so. He started doing this when I added a specific package manually, because something wasn’t working, but I don’t remember which one.

My guess is that react-native link assumes that the packages listed in some order look like the reverse alphabetical, but since the npm package name does not necessarily match the name of the include project in settings.gradle , which does not match the import name in MainApplication.java , I find it difficult to determine how to reorder them in settings.gradle to stop this behavior. Or it could be quite another.

package.json

 { // ... "dependencies": { "events": "^1.1.1", "flux": "^3.1.2", "react": "~15.4.1", "react-native": "0.42.0", "react-native-datepicker": "^1.4.4", "react-native-fs": "^2.1.0-rc.1", "react-native-navigation": "^1.0.30", "react-native-push-notification": "^2.2.1", "react-native-sound": "^0.9.1", "react-native-vector-icons": "^4.0.0", "redux": "^3.6.0" }, "devDependencies": { "babel-jest": "19.0.0", "babel-preset-react-native": "1.9.1", "jest": "19.0.2", "react-test-renderer": "~15.4.1" }, "jest": { "preset": "react-native" } } 

Here are the files that should be ...

Android / settings.gradle

 rootProject.name = // <projectName> include ':react-native-vector-icons' project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android') include ':react-native-sound' project(':react-native-sound').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sound/android') include ':react-native-push-notification' project(':react-native-push-notification').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-push-notification/android') include ':react-native-navigation' project(':react-native-navigation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-navigation/android/app') include ':react-native-fs' project(':react-native-fs').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fs/android') include ':app' 

Android / app / src / core / com / [Projectname] /MainApplication.java

 package // com.<projectName>; import android.app.Application; import com.facebook.react.ReactApplication; import com.oblador.vectoricons.VectorIconsPackage; import com.zmxv.RNSound.RNSoundPackage; import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage; import com.reactnativenavigation.RnnPackage; import com.rnfs.RNFSPackage; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.react.shell.MainReactPackage; import com.facebook.soloader.SoLoader; import java.util.Arrays; import java.util.List; public class MainApplication extends Application implements ReactApplication { private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { @Override public boolean getUseDeveloperSupport() { return BuildConfig.DEBUG; } @Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new VectorIconsPackage(), new RNSoundPackage(), new ReactNativePushNotificationPackage(), new RnnPackage(), new RNFSPackage(), ); } }; @Override public ReactNativeHost getReactNativeHost() { return mReactNativeHost; } @Override public void onCreate() { super.onCreate(); SoLoader.init(this, /* native exopackage */ false); } } 

After starting react-native link files change to:

Android / settings.gradle

 rootProject.name = // <projectName> include ':react-native-vector-icons' project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android') include ':react-native-sound' project(':react-native-sound').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sound/android') include ':react-native-push-notification' project(':react-native-push-notification').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-push-notification/android') include ':react-native-navigation' project(':react-native-navigation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-navigation/android/app') include ':react-native-fs' project(':react-native-fs').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fs/android') include ':react-native-vector-icons' project(':react-native-vector-icons').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-vector-icons/android') include ':react-native-sound' project(':react-native-sound').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-sound/android') include ':react-native-push-notification' project(':react-native-push-notification').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-push-notification/android') include ':react-native-navigation' project(':react-native-navigation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-navigation/android/app') include ':react-native-fs' project(':react-native-fs').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fs/android') include ':app' 

Android / app / src / core / com / [Projectname] /MainApplication.java

 // ... import com.facebook.react.ReactApplication; import com.oblador.vectoricons.VectorIconsPackage; import com.zmxv.RNSound.RNSoundPackage; import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage; import com.reactnativenavigation.RnnPackage; import com.rnfs.RNFSPackage; import com.oblador.vectoricons.VectorIconsPackage; import com.zmxv.RNSound.RNSoundPackage; import com.dieam.reactnativepushnotification.ReactNativePushNotificationPackage; import com.reactnativenavigation.RnnPackage; import com.rnfs.RNFSPackage; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.react.shell.MainReactPackage; import com.facebook.soloader.SoLoader; import java.util.Arrays; import java.util.List; public class MainApplication extends Application implements ReactApplication { private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) { @Override public boolean getUseDeveloperSupport() { return BuildConfig.DEBUG; } @Override protected List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new VectorIconsPackage(), new RNSoundPackage(), new ReactNativePushNotificationPackage(), new RnnPackage(), new RNFSPackage(), new VectorIconsPackage(), new RNSoundPackage(), new ReactNativePushNotificationPackage(), new RnnPackage(), new RNFSPackage(), ); } }; // ... 

If I run react-native-link again, it will add a third copy of the packages.

Does anyone have an idea on how react-native link works?

+5
source share
2 answers

This is a known bug in React Native. Currently PR is expecting this error: https://github.com/facebook/react-native/pull/18131

Update: bug fixed!

To solve this problem, you just have to manually comb your dependencies when you run react-native-link . I find it useful to have a clean git status before starting, and then move on to the modified files later with git diff .

+1
source

A WARNING. Before performing the steps below, please backup your project.

If I encountered such a problem, I would remove all the dependency from the .graddle settings, except include ':app' , from getpackages, except new MainReactPackage() , and from the /build.graddle application I would remove all the dependencies, except:

 dependencies { compile fileTree(dir: "libs", include: ["*.jar"]) compile "com.android.support:appcompat-v7:23.0.1" compile "com.facebook.react:react-native:+" } 

After deleting all the above code, I would follow the link with feedback.

Greetings (coffee) :)

0
source

Source: https://habr.com/ru/post/1265927/


All Articles