diff --git a/.gitignore b/.gitignore index 747bb762be951a..492e74de3c00da 100644 --- a/.gitignore +++ b/.gitignore @@ -36,6 +36,8 @@ project.xcworkspace /ReactAndroid/gradlew.bat /template/android/app/build/ /template/android/build/ +/android/annotations/build/ +/android/annotations-compiler/build/ # Buck .buckd @@ -58,7 +60,6 @@ buck-out .gradle local.properties *.iml -/android/ # Node node_modules diff --git a/ReactAndroid/build.gradle b/ReactAndroid/build.gradle index 7d2e903790e3fb..b1981c33d9188b 100644 --- a/ReactAndroid/build.gradle +++ b/ReactAndroid/build.gradle @@ -503,6 +503,10 @@ android { dependencies { api("com.facebook.infer.annotation:infer-annotation:0.18.0") + api(project(':react-native-annotations')) + annotationProcessor(project(':react-native-annotations-compiler')) + + api("com.facebook.infer.annotation:infer-annotation:0.11.2") api("com.facebook.yoga:proguard-annotations:1.19.0") api("javax.inject:javax.inject:1") api("androidx.appcompat:appcompat:1.0.2") diff --git a/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java b/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java index 59c5c7759c52c6..403e1951cd318a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java @@ -7,19 +7,13 @@ package com.facebook.react; -import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_END; -import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_START; -import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_CORE_REACT_PACKAGE_END; -import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_CORE_REACT_PACKAGE_START; - import androidx.annotation.Nullable; + import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactMarker; import com.facebook.react.devsupport.LogBoxModule; -import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.module.annotations.ReactModuleList; -import com.facebook.react.module.model.ReactModuleInfo; import com.facebook.react.module.model.ReactModuleInfoProvider; import com.facebook.react.modules.bundleloader.NativeDevSplitBundleLoaderModule; import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; @@ -31,22 +25,23 @@ import com.facebook.react.modules.debug.SourceCodeModule; import com.facebook.react.modules.deviceinfo.DeviceInfoModule; import com.facebook.react.modules.systeminfo.AndroidInfoModule; -import com.facebook.react.turbomodule.core.interfaces.TurboModule; import com.facebook.react.uimanager.UIImplementationProvider; import com.facebook.react.uimanager.UIManagerModule; import com.facebook.react.uimanager.ViewManager; import com.facebook.react.uimanager.ViewManagerResolver; import com.facebook.systrace.Systrace; -import java.util.HashMap; + import java.util.List; -import java.util.Map; + +import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_END; +import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_MODULE_START; +import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_CORE_REACT_PACKAGE_END; +import static com.facebook.react.bridge.ReactMarkerConstants.PROCESS_CORE_REACT_PACKAGE_START; /** * This is the basic module to support React Native. The debug modules are now in DebugCorePackage. */ @ReactModuleList( - // WARNING: If you modify this list, ensure that the list below in method - // getReactModuleInfoByInitialization is also updated nativeModules = { AndroidInfoModule.class, DeviceEventManagerModule.class, @@ -79,64 +74,9 @@ public CoreModulesPackage( mMinTimeLeftInFrameForNonBatchedOperationMs = minTimeLeftInFrameForNonBatchedOperationMs; } - /** - * This method is overridden, since OSS does not run the annotation processor to generate {@link - * CoreModulesPackage$$ReactModuleInfoProvider} class. Here we check if it exists. If it does not - * exist, we generate one manually in {@link - * CoreModulesPackage#getReactModuleInfoByInitialization()} and return that instead. - */ @Override public ReactModuleInfoProvider getReactModuleInfoProvider() { - try { - Class reactModuleInfoProviderClass = - Class.forName("com.facebook.react.CoreModulesPackage$$ReactModuleInfoProvider"); - return (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance(); - } catch (ClassNotFoundException e) { - // In OSS case, the annotation processor does not run. We fall back on creating this byhand - Class[] moduleList = - new Class[] { - AndroidInfoModule.class, - DeviceEventManagerModule.class, - DeviceInfoModule.class, - DevSettingsModule.class, - ExceptionsManagerModule.class, - LogBoxModule.class, - HeadlessJsTaskSupportModule.class, - SourceCodeModule.class, - TimingModule.class, - UIManagerModule.class, - NativeDevSplitBundleLoaderModule.class, - }; - - final Map reactModuleInfoMap = new HashMap<>(); - for (Class moduleClass : moduleList) { - ReactModule reactModule = moduleClass.getAnnotation(ReactModule.class); - - reactModuleInfoMap.put( - reactModule.name(), - new ReactModuleInfo( - reactModule.name(), - moduleClass.getName(), - reactModule.canOverrideExistingModule(), - reactModule.needsEagerInit(), - reactModule.hasConstants(), - reactModule.isCxxModule(), - TurboModule.class.isAssignableFrom(moduleClass))); - } - - return new ReactModuleInfoProvider() { - @Override - public Map getReactModuleInfos() { - return reactModuleInfoMap; - } - }; - } catch (InstantiationException e) { - throw new RuntimeException( - "No ReactModuleInfoProvider for CoreModulesPackage$$ReactModuleInfoProvider", e); - } catch (IllegalAccessException e) { - throw new RuntimeException( - "No ReactModuleInfoProvider for CoreModulesPackage$$ReactModuleInfoProvider", e); - } + return new CoreModulesPackage$$ReactModuleInfoProvider(); } @Override diff --git a/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.java b/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.java index 740182fbc0f7f1..eefd1a9cdc3184 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.java @@ -10,18 +10,11 @@ import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.devsupport.JSCHeapCapture; -import com.facebook.react.module.annotations.ReactModule; import com.facebook.react.module.annotations.ReactModuleList; -import com.facebook.react.module.model.ReactModuleInfo; import com.facebook.react.module.model.ReactModuleInfoProvider; -import com.facebook.react.turbomodule.core.interfaces.TurboModule; -import java.util.HashMap; -import java.util.Map; /** - * Package defining core framework modules (e.g. UIManager). It should be used for modules that - * require special integration with other framework parts (e.g. with the list of packages to load - * view managers from). + * Package defining core debug only modules. */ @ReactModuleList( nativeModules = { @@ -43,42 +36,6 @@ public NativeModule getModule(String name, ReactApplicationContext reactContext) @Override public ReactModuleInfoProvider getReactModuleInfoProvider() { - try { - Class reactModuleInfoProviderClass = - Class.forName("com.facebook.react.DebugCorePackage$$ReactModuleInfoProvider"); - return (ReactModuleInfoProvider) reactModuleInfoProviderClass.newInstance(); - } catch (ClassNotFoundException e) { - // In OSS case, the annotation processor does not run. We fall back on creating this by hand - Class[] moduleList = new Class[] {JSCHeapCapture.class}; - - final Map reactModuleInfoMap = new HashMap<>(); - for (Class moduleClass : moduleList) { - ReactModule reactModule = moduleClass.getAnnotation(ReactModule.class); - - reactModuleInfoMap.put( - reactModule.name(), - new ReactModuleInfo( - reactModule.name(), - moduleClass.getName(), - reactModule.canOverrideExistingModule(), - reactModule.needsEagerInit(), - reactModule.hasConstants(), - reactModule.isCxxModule(), - TurboModule.class.isAssignableFrom(moduleClass))); - } - - return new ReactModuleInfoProvider() { - @Override - public Map getReactModuleInfos() { - return reactModuleInfoMap; - } - }; - } catch (InstantiationException e) { - throw new RuntimeException( - "No ReactModuleInfoProvider for DebugCorePackage$$ReactModuleInfoProvider", e); - } catch (IllegalAccessException e) { - throw new RuntimeException( - "No ReactModuleInfoProvider for DebugCorePackage$$ReactModuleInfoProvider", e); - } + return new com.facebook.react.DebugCorePackage$$ReactModuleInfoProvider(); } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModule.java b/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModule.java index 11f7117f2e9789..a2e79d846c258e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/NativeModule.java @@ -9,6 +9,7 @@ import androidx.annotation.NonNull; import com.facebook.proguard.annotations.DoNotStrip; +import com.facebook.react.module.annotations.BaseNativeModule; /** * A native module whose API can be provided to JS catalyst instances. {@link NativeModule}s whose @@ -18,7 +19,7 @@ * themselves using {@link CxxModuleWrapper}. */ @DoNotStrip -public interface NativeModule { +public interface NativeModule extends BaseNativeModule { interface NativeMethod { void invoke(JSInstance jsInstance, ReadableArray parameters); diff --git a/android/annotations-compiler/build.gradle b/android/annotations-compiler/build.gradle new file mode 100644 index 00000000000000..8ca6867c604a65 --- /dev/null +++ b/android/annotations-compiler/build.gradle @@ -0,0 +1,25 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +apply plugin: 'java' + +sourceSets.configureEach { sourceSet -> + tasks.named(sourceSet.compileJavaTaskName).configure { + options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/annotationProcessor/java/${sourceSet.name}") + } +} + +java { + sourceCompatibility(JavaVersion.VERSION_1_8) + targetCompatibility(JavaVersion.VERSION_1_8) +} + +dependencies { + implementation('com.squareup:javapoet:1.8.0') + implementation('com.facebook.infer.annotation:infer-annotation:0.11.2') + implementation(project(':react-native-annotations')) +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/module/processing/BUCK b/android/annotations-compiler/src/main/java/com/facebook/react/module/processing/BUCK similarity index 100% rename from ReactAndroid/src/main/java/com/facebook/react/module/processing/BUCK rename to android/annotations-compiler/src/main/java/com/facebook/react/module/processing/BUCK diff --git a/ReactAndroid/src/main/java/com/facebook/react/module/processing/ReactModuleSpecProcessor.java b/android/annotations-compiler/src/main/java/com/facebook/react/module/processing/ReactModuleSpecProcessor.java similarity index 100% rename from ReactAndroid/src/main/java/com/facebook/react/module/processing/ReactModuleSpecProcessor.java rename to android/annotations-compiler/src/main/java/com/facebook/react/module/processing/ReactModuleSpecProcessor.java diff --git a/android/annotations-compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor b/android/annotations-compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor new file mode 100644 index 00000000000000..31846ced5a51a8 --- /dev/null +++ b/android/annotations-compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor @@ -0,0 +1 @@ +com.facebook.react.module.processing.ReactModuleSpecProcessor diff --git a/android/annotations/build.gradle b/android/annotations/build.gradle new file mode 100644 index 00000000000000..e4bae3f4f9e34b --- /dev/null +++ b/android/annotations/build.gradle @@ -0,0 +1,17 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +apply plugin: 'java' + +dependencies { + +} + +java { + sourceCompatibility(JavaVersion.VERSION_1_8) + targetCompatibility(JavaVersion.VERSION_1_8) +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/module/annotations/BUCK b/android/annotations/src/main/java/com/facebook/react/module/annotations/BUCK similarity index 100% rename from ReactAndroid/src/main/java/com/facebook/react/module/annotations/BUCK rename to android/annotations/src/main/java/com/facebook/react/module/annotations/BUCK diff --git a/android/annotations/src/main/java/com/facebook/react/module/annotations/BaseNativeModule.java b/android/annotations/src/main/java/com/facebook/react/module/annotations/BaseNativeModule.java new file mode 100644 index 00000000000000..fda3bb0eb7d2e8 --- /dev/null +++ b/android/annotations/src/main/java/com/facebook/react/module/annotations/BaseNativeModule.java @@ -0,0 +1,15 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.module.annotations; + +/** + * Used to make sure classes passed to the ReactModule annotation are actually an instance of + * NativeModule without needing a dependency on NativeModule that is in the ReactAndroid target. + */ +public interface BaseNativeModule { +} diff --git a/ReactAndroid/src/main/java/com/facebook/react/module/annotations/ReactModule.java b/android/annotations/src/main/java/com/facebook/react/module/annotations/ReactModule.java similarity index 100% rename from ReactAndroid/src/main/java/com/facebook/react/module/annotations/ReactModule.java rename to android/annotations/src/main/java/com/facebook/react/module/annotations/ReactModule.java diff --git a/ReactAndroid/src/main/java/com/facebook/react/module/annotations/ReactModuleList.java b/android/annotations/src/main/java/com/facebook/react/module/annotations/ReactModuleList.java similarity index 89% rename from ReactAndroid/src/main/java/com/facebook/react/module/annotations/ReactModuleList.java rename to android/annotations/src/main/java/com/facebook/react/module/annotations/ReactModuleList.java index efad29c134445e..016f71b0549c70 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/module/annotations/ReactModuleList.java +++ b/android/annotations/src/main/java/com/facebook/react/module/annotations/ReactModuleList.java @@ -10,7 +10,6 @@ import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.SOURCE; -import com.facebook.react.bridge.NativeModule; import java.lang.annotation.Retention; import java.lang.annotation.Target; @@ -27,5 +26,5 @@ * * @return List of Native modules in the package. */ - Class[] nativeModules(); + Class[] nativeModules(); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/module/model/BUCK b/android/annotations/src/main/java/com/facebook/react/module/model/BUCK similarity index 100% rename from ReactAndroid/src/main/java/com/facebook/react/module/model/BUCK rename to android/annotations/src/main/java/com/facebook/react/module/model/BUCK diff --git a/ReactAndroid/src/main/java/com/facebook/react/module/model/ReactModuleInfo.java b/android/annotations/src/main/java/com/facebook/react/module/model/ReactModuleInfo.java similarity index 100% rename from ReactAndroid/src/main/java/com/facebook/react/module/model/ReactModuleInfo.java rename to android/annotations/src/main/java/com/facebook/react/module/model/ReactModuleInfo.java diff --git a/ReactAndroid/src/main/java/com/facebook/react/module/model/ReactModuleInfoProvider.java b/android/annotations/src/main/java/com/facebook/react/module/model/ReactModuleInfoProvider.java similarity index 100% rename from ReactAndroid/src/main/java/com/facebook/react/module/model/ReactModuleInfoProvider.java rename to android/annotations/src/main/java/com/facebook/react/module/model/ReactModuleInfoProvider.java diff --git a/settings.gradle.kts b/settings.gradle.kts index f804e186f975f7..b75006e1c3bfb1 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -29,3 +29,9 @@ if (File("template/node_modules/").exists()) { name = "template-android" } } + +include(":react-native-annotations") +project(":react-native-annotations").projectDir = File(rootProject.projectDir, "android/annotations") + +include(":react-native-annotations-compiler") +project(":react-native-annotations-compiler").projectDir = File(rootProject.projectDir, "android/annotations-compiler")