androidreactjsreact-nativeautolink

Do you need to follow manual linking steps for Android with React Native?


When I'm adding third party components to React Native apps, such as this one, it often says that because of auto-linking, you do not have to follow the manual linking steps. For Android, I notice that none of the code added in the manual linking steps, such as adding code to MainApplication.java, none of it gets added automatically.

Can anyone explain what's going on here? Do I need to do the manual linking steps for Android despite auto-linking?


Solution

  • for android, it doesn't need linking. since react-native 0.6, the auto-link doesn't directly add int MainApplication.java.

    in android settings.gradle, you can see this code:

    apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); 
    

    in the native_modules.gradle it will add the package. the code is complex. I read some days, only know a little. but we can get from the name and comment.

    def generatedFileName = "PackageList.java"
    def generatedFilePackage = "com.facebook.react"
    ...
      /**
       * Include the react native modules android projects and specify their project directory
       */
      void addReactNativeModuleProjects(DefaultSettings defaultSettings) 
    
      //here are add the package list
      /**
       * Adds the react native modules as dependencies to the users `app` project
       */
      void addReactNativeModuleDependencies(Project appProject)
    
      /**
       * Code-gen a java file with all the detected ReactNativePackage instances automatically added
       *
       * @param outputDir
       * @param generatedFileName
       * @param generatedFileContentsTemplate
       */
      void generatePackagesFile(File outputDir, String generatedFileName, String 
      generatedFileContentsTemplate)
    

    in the end, we only should know we don't need the link.