Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: autolinking for Android with Gradle (#258)
Provides the Android/Gradle functionality to go with #256 & #254 ## CLI Changes - Updated `react-native config` to include the android `sourceDir` for react native module packages ---- ## Usage These are the one-off changes required to enable this in the React Native init template (or for existing user projects). ### Modify `android/settings.gradle` Include the following line in your Android projects `settings.gradle` file: ```groovy apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings) ``` ### Modify `android/app/build.gradle` Add the following line at the bottom of your Android projects `android/app/build.gradle` file: ```groovy apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesAppBuildGradle(project) ``` ### Modify `MainApplication.java` Import the PackageList class; ```java import com.facebook.react.PackageList; ``` Replace the `protected List<ReactPackage> getPackages()` method with the following: ```java @OverRide protected List<ReactPackage> getPackages() { @SuppressWarnings("UnnecessaryLocalVariable") List<ReactPackage> packages = new PackageList(this).getPackages(); // additional non auto detected packages can still be added here: // packages.add(new SomeReactNativePackage()); return packages; } ``` ### Testing I've tested the following scenarios locally: - User has no React Native global CLI installed - Will warn but continue without issue - User has no React Native packages installed yet - Continues as normal with no warning - User has more than one React Native package installed - Correctly adds multiple packages without issue - User has a package installed that does not support autodetection (config == null) - Prints a warning but continues gracefully - User has custom/non-linked React Native packages - Can be sucessfully manually added via `packages.add(new SomeReactNativePackage());` as documented above in `MainApplication.java` To test this in a new `react-native init` project locally: - Clone the CLI project - Run `yarn` in the CLI project - Run `cd packages/cli && yarn && yarn link` in the CLI project - (a little hacky as it's a mono-repo) - Run `yarn link "@react-native-community/cli"` on your React Native project - Make the required changes as above to your React Native project ---- ### Codegen Output Example An example of the generated `PackageList.java` class: ```java package com.facebook.react; import android.app.Application; import android.content.Context; import android.content.res.Resources; import com.facebook.react.ReactPackage; import com.facebook.react.shell.MainReactPackage; import java.util.Arrays; import java.util.List; import com.test.BuildConfig; // react-native-webview import com.reactnativecommunity.webview.RNCWebViewPackage; public class PackageList { private ReactNativeHost reactNativeHost; public PackageList(ReactNativeHost reactNativeHost) { this.reactNativeHost = reactNativeHost; } private ReactNativeHost getReactNativeHost() { return this.reactNativeHost; } private Resources getResources() { return this.getApplication().getResources(); } private Application getApplication() { return this.reactNativeHost.getApplication(); } private Context getApplicationContext() { return this.getApplication().getApplicationContext(); } public List<ReactPackage> getPackages() { return Arrays.<ReactPackage>asList( new MainReactPackage(), new RNCWebViewPackage() ); } } ```
- Loading branch information