From 03d7adf91b91b9a5a07a24acedea259e5366758a Mon Sep 17 00:00:00 2001 From: Janic Duplessis Date: Fri, 2 Aug 2019 14:28:06 -0400 Subject: [PATCH] Enable module annotation processor in OSS --- .circleci/Dockerfiles/Dockerfile.android | 1 + .gitignore | 3 +- RNTester/android/app/{_BUCK => BUCK} | 0 RNTester/android/app/build.gradle | 1 + ReactAndroid/build.gradle | 3 + .../facebook/react/CoreModulesPackage.java | 75 ++----------------- .../com/facebook/react/DebugCorePackage.java | 51 +------------ .../main/java/com/facebook/react/bridge/BUCK | 1 + .../facebook/react/bridge/NativeModule.java | 3 +- .../facebook/react/module/annotations/BUCK | 9 +-- .../java/com/facebook/react/module/model/BUCK | 7 +- .../com/facebook/react/module/processing/BUCK | 19 +---- .../third-party/java/infer-annotations/BUCK | 2 +- .../src/main/third-party/java/javapoet/BUCK | 2 +- android/annotations-compiler/build.gradle | 25 +++++++ .../com/facebook/react/module/processing/BUCK | 20 +++++ .../processing/ReactModuleSpecProcessor.java | 0 .../javax.annotation.processing.Processor | 1 + android/annotations/build.gradle | 17 +++++ .../facebook/react/module/annotations/BUCK | 17 +++++ .../react/module/annotations/ReactModule.java | 2 - .../module/annotations/ReactModuleList.java | 5 +- .../java/com/facebook/react/module/model/BUCK | 13 ++++ .../react/module/model/BaseNativeModule.java | 15 ++++ .../react/module/model/ReactModuleInfo.java | 0 .../module/model/ReactModuleInfoProvider.java | 0 settings.gradle.kts | 6 ++ template/android/app/build.gradle | 1 + template/android/settings.gradle | 7 ++ 29 files changed, 155 insertions(+), 151 deletions(-) rename RNTester/android/app/{_BUCK => BUCK} (100%) create mode 100644 android/annotations-compiler/build.gradle create mode 100644 android/annotations-compiler/src/main/java/com/facebook/react/module/processing/BUCK rename {ReactAndroid => android/annotations-compiler}/src/main/java/com/facebook/react/module/processing/ReactModuleSpecProcessor.java (100%) create mode 100644 android/annotations-compiler/src/main/resources/META-INF/services/javax.annotation.processing.Processor create mode 100644 android/annotations/build.gradle create mode 100644 android/annotations/src/main/java/com/facebook/react/module/annotations/BUCK rename {ReactAndroid => android/annotations}/src/main/java/com/facebook/react/module/annotations/ReactModule.java (94%) rename {ReactAndroid => android/annotations}/src/main/java/com/facebook/react/module/annotations/ReactModuleList.java (87%) create mode 100644 android/annotations/src/main/java/com/facebook/react/module/model/BUCK create mode 100644 android/annotations/src/main/java/com/facebook/react/module/model/BaseNativeModule.java rename {ReactAndroid => android/annotations}/src/main/java/com/facebook/react/module/model/ReactModuleInfo.java (100%) rename {ReactAndroid => android/annotations}/src/main/java/com/facebook/react/module/model/ReactModuleInfoProvider.java (100%) diff --git a/.circleci/Dockerfiles/Dockerfile.android b/.circleci/Dockerfiles/Dockerfile.android index 431f802e3566dc..693800500c015c 100644 --- a/.circleci/Dockerfiles/Dockerfile.android +++ b/.circleci/Dockerfiles/Dockerfile.android @@ -27,6 +27,7 @@ ADD .buckconfig /app/.buckconfig ADD .buckjavaargs /app/.buckjavaargs ADD tools /app/tools ADD ReactAndroid /app/ReactAndroid +ADD android /app/android ADD ReactCommon /app/ReactCommon ADD React /app/React ADD keystores /app/keystores diff --git a/.gitignore b/.gitignore index 43810959f7eca0..9851496f4fa6cf 100644 --- a/.gitignore +++ b/.gitignore @@ -31,6 +31,8 @@ project.xcworkspace /ReactAndroid/gradle/ /ReactAndroid/gradlew /ReactAndroid/gradlew.bat +/android/annotations/build/ +/android/annotations-compiler/build/ # Buck .buckd @@ -51,7 +53,6 @@ buck-out .gradle local.properties *.iml -/android/ # Node node_modules diff --git a/RNTester/android/app/_BUCK b/RNTester/android/app/BUCK similarity index 100% rename from RNTester/android/app/_BUCK rename to RNTester/android/app/BUCK diff --git a/RNTester/android/app/build.gradle b/RNTester/android/app/build.gradle index 0a36475ade519b..def084bcaa5380 100644 --- a/RNTester/android/app/build.gradle +++ b/RNTester/android/app/build.gradle @@ -168,6 +168,7 @@ dependencies { // Build React Native from source implementation project(':ReactAndroid') + annotationProcessor project(":react-native-annotations-compiler") implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" diff --git a/ReactAndroid/build.gradle b/ReactAndroid/build.gradle index 23d9fd5912bc9a..bca3c521df9648 100644 --- a/ReactAndroid/build.gradle +++ b/ReactAndroid/build.gradle @@ -437,6 +437,9 @@ android { } dependencies { + 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.14.1") api("javax.inject:javax.inject:1") diff --git a/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java b/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java index 754d86ec08d5c4..3b11f9c473651e 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.core.DefaultHardwareBackBtnHandler; import com.facebook.react.modules.core.DeviceEventManagerModule; @@ -30,21 +24,22 @@ 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.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, @@ -76,63 +71,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 - }; - - 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 24116e4f99db4e..bfaa384d7a7c8d 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/DebugCorePackage.java @@ -10,19 +10,11 @@ import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.devsupport.JSCHeapCapture; -import com.facebook.react.devsupport.JSDevSupport; -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 = { @@ -44,45 +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, JSDevSupport.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/BUCK b/ReactAndroid/src/main/java/com/facebook/react/bridge/BUCK index fcf60e30696589..e541579f55865e 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/bridge/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/bridge/BUCK @@ -85,6 +85,7 @@ rn_android_library( react_native_target("java/com/facebook/debug/tags:tags"), ], exported_deps = [ + react_native_target("java/com/facebook/react/module/model:model"), react_native_dep("java/com/facebook/proguard/annotations:annotations"), react_native_dep("third-party/java/jsr-330:jsr-330"), ], 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 16939d03209621..8859919efaf13a 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.model.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/ReactAndroid/src/main/java/com/facebook/react/module/annotations/BUCK b/ReactAndroid/src/main/java/com/facebook/react/module/annotations/BUCK index b213c9d3133a0f..c50cd38c15560a 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/module/annotations/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/module/annotations/BUCK @@ -1,16 +1,13 @@ -load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library") +load("//tools/build_defs/oss:rn_defs.bzl", "rn_android_library") rn_android_library( name = "annotations", - srcs = glob(["**/*.java"]), labels = ["supermodule:xplat/default/public.react_native.infra"], required_for_source_only_abi = True, visibility = [ "PUBLIC", ], - deps = [ - react_native_dep("third-party/java/infer-annotations:infer-annotations"), - react_native_dep("third-party/java/jsr-305:jsr-305"), - react_native_target("java/com/facebook/react/bridge:interfaces"), + exported_deps = [ + "//android/annotations/src/main/java/com/facebook/react/module/annotations:annotations", ], ) diff --git a/ReactAndroid/src/main/java/com/facebook/react/module/model/BUCK b/ReactAndroid/src/main/java/com/facebook/react/module/model/BUCK index 85f23a35691b69..705cf3590d7c6c 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/module/model/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/module/model/BUCK @@ -1,13 +1,12 @@ -load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "rn_android_library") +load("//tools/build_defs/oss:rn_defs.bzl", "rn_android_library") rn_android_library( name = "model", - srcs = glob(["**/*.java"]), labels = ["supermodule:xplat/default/public.react_native.infra"], visibility = [ "PUBLIC", ], - deps = [ - react_native_dep("third-party/java/jsr-305:jsr-305"), + exported_deps = [ + "//android/annotations/src/main/java/com/facebook/react/module/model:model", ], ) diff --git a/ReactAndroid/src/main/java/com/facebook/react/module/processing/BUCK b/ReactAndroid/src/main/java/com/facebook/react/module/processing/BUCK index cc44f729d1893a..34816f74af55fc 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/module/processing/BUCK +++ b/ReactAndroid/src/main/java/com/facebook/react/module/processing/BUCK @@ -1,4 +1,4 @@ -load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_java_annotation_processor", "rn_java_library") +load("//tools/build_defs/oss:rn_defs.bzl", "rn_java_annotation_processor") rn_java_annotation_processor( name = "processing", @@ -8,22 +8,7 @@ rn_java_annotation_processor( "PUBLIC", ], deps = [ - ":processing-lib", + "//android/annotations-compiler/src/main/java/com/facebook/react/module/processing:processing", ], ) -rn_java_library( - name = "processing-lib", - srcs = glob(["*.java"]), - labels = ["supermodule:xplat/default/public.react_native.infra"], - source = "8", - target = "8", - deps = [ - react_native_dep("third-party/java/infer-annotations:infer-annotations"), - react_native_dep("third-party/java/javapoet:javapoet"), - react_native_dep("third-party/java/jsr-305:jsr-305"), - react_native_target("java/com/facebook/react/module/annotations:annotations"), - react_native_target("java/com/facebook/react/module/model:model"), - react_native_target("java/com/facebook/react/turbomodule/core/interfaces:interfaces"), - ], -) diff --git a/ReactAndroid/src/main/third-party/java/infer-annotations/BUCK b/ReactAndroid/src/main/third-party/java/infer-annotations/BUCK index 9cf53d29a79beb..7809a3794faf16 100644 --- a/ReactAndroid/src/main/third-party/java/infer-annotations/BUCK +++ b/ReactAndroid/src/main/third-party/java/infer-annotations/BUCK @@ -4,7 +4,7 @@ load("//tools/build_defs/oss:rn_defs.bzl", "rn_prebuilt_jar") rn_prebuilt_jar( name = "infer-annotations", binary_jar = ":infer-annotations.jar", - visibility = ["//ReactAndroid/..."], + visibility = ["//ReactAndroid/...", "//android/..."], ) fb_native.remote_file( diff --git a/ReactAndroid/src/main/third-party/java/javapoet/BUCK b/ReactAndroid/src/main/third-party/java/javapoet/BUCK index 3a50a70a433e81..d1f6b4acf6aa62 100644 --- a/ReactAndroid/src/main/third-party/java/javapoet/BUCK +++ b/ReactAndroid/src/main/third-party/java/javapoet/BUCK @@ -4,7 +4,7 @@ load("//tools/build_defs/oss:rn_defs.bzl", "rn_prebuilt_jar") rn_prebuilt_jar( name = "javapoet", binary_jar = ":jsr305-binary.jar", - visibility = ["//ReactAndroid/..."], + visibility = ["//ReactAndroid/...", "//android/..."], ) fb_native.remote_file( 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/android/annotations-compiler/src/main/java/com/facebook/react/module/processing/BUCK b/android/annotations-compiler/src/main/java/com/facebook/react/module/processing/BUCK new file mode 100644 index 00000000000000..cb664b1d458bce --- /dev/null +++ b/android/annotations-compiler/src/main/java/com/facebook/react/module/processing/BUCK @@ -0,0 +1,20 @@ +load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_java_annotation_processor", "rn_java_library") + +rn_java_library( + name = "processing", + srcs = glob(["*.java"]), + labels = ["supermodule:xplat/default/public.react_native.infra"], + source = "8", + target = "8", + visibility = [ + "PUBLIC", + ], + deps = [ + react_native_dep("third-party/java/infer-annotations:infer-annotations"), + react_native_dep("third-party/java/javapoet:javapoet"), + react_native_dep("third-party/java/jsr-305:jsr-305"), + react_native_target("java/com/facebook/react/module/annotations:annotations"), + react_native_target("java/com/facebook/react/module/model:model"), + react_native_target("java/com/facebook/react/turbomodule/core/interfaces:interfaces"), + ], +) 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/android/annotations/src/main/java/com/facebook/react/module/annotations/BUCK b/android/annotations/src/main/java/com/facebook/react/module/annotations/BUCK new file mode 100644 index 00000000000000..cef979ce29dd7b --- /dev/null +++ b/android/annotations/src/main/java/com/facebook/react/module/annotations/BUCK @@ -0,0 +1,17 @@ +load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_android_library") + +rn_android_library( + name = "annotations", + srcs = glob(["**/*.java"]), + labels = ["supermodule:xplat/default/public.react_native.infra"], + required_for_source_only_abi = True, + visibility = [ + "PUBLIC", + ], + deps = [ + "//android/annotations/src/main/java/com/facebook/react/module/model:model", + react_native_dep("third-party/java/infer-annotations:infer-annotations"), + react_native_dep("third-party/java/jsr-305:jsr-305"), + react_native_target("java/com/facebook/react/bridge:interfaces"), + ], +) 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 94% 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 index cbcd4aa6002d60..12f0a6471324a8 100644 --- 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 @@ -8,7 +8,6 @@ package com.facebook.react.module.annotations; import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Retention; import java.lang.annotation.Target; @@ -17,7 +16,6 @@ * Annotation for use on {@link com.facebook.react.bridge.BaseJavaModule}s to describe properties * for that module. */ -@Retention(RUNTIME) @Target(TYPE) public @interface ReactModule { /** Name used to {@code require()} this module from JavaScript. */ 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 87% 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..1548f494bd1555 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 @@ -7,10 +7,11 @@ package com.facebook.react.module.annotations; +import com.facebook.react.module.model.BaseNativeModule; + 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 +28,5 @@ * * @return List of Native modules in the package. */ - Class[] nativeModules(); + Class[] nativeModules(); } diff --git a/android/annotations/src/main/java/com/facebook/react/module/model/BUCK b/android/annotations/src/main/java/com/facebook/react/module/model/BUCK new file mode 100644 index 00000000000000..85f23a35691b69 --- /dev/null +++ b/android/annotations/src/main/java/com/facebook/react/module/model/BUCK @@ -0,0 +1,13 @@ +load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "rn_android_library") + +rn_android_library( + name = "model", + srcs = glob(["**/*.java"]), + labels = ["supermodule:xplat/default/public.react_native.infra"], + visibility = [ + "PUBLIC", + ], + deps = [ + react_native_dep("third-party/java/jsr-305:jsr-305"), + ], +) diff --git a/android/annotations/src/main/java/com/facebook/react/module/model/BaseNativeModule.java b/android/annotations/src/main/java/com/facebook/react/module/model/BaseNativeModule.java new file mode 100644 index 00000000000000..8c5a69669fd665 --- /dev/null +++ b/android/annotations/src/main/java/com/facebook/react/module/model/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.model; + +/** + * 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/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 0c80447ca88928..eff2fde78b7e6e 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -9,3 +9,9 @@ include( ":ReactAndroid", ":RNTester:android:app" ) + +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") diff --git a/template/android/app/build.gradle b/template/android/app/build.gradle index 56a8363293d710..66cb0ce897fd11 100644 --- a/template/android/app/build.gradle +++ b/template/android/app/build.gradle @@ -184,6 +184,7 @@ dependencies { implementation fileTree(dir: "libs", include: ["*.jar"]) //noinspection GradleDynamicVersion implementation "com.facebook.react:react-native:+" // From node_modules + annotationProcessor project(":react-native-annotations-compiler") implementation "androidx.swiperefreshlayout:swiperefreshlayout:1.0.0" diff --git a/template/android/settings.gradle b/template/android/settings.gradle index e50c29d6298864..3c162c4b3951b1 100644 --- a/template/android/settings.gradle +++ b/template/android/settings.gradle @@ -1,3 +1,10 @@ rootProject.name = 'HelloWorld' + +include ':react-native-annotations' +project(':react-native-annotations').projectDir = new File(rootProject.projectDir, '../node_modules/react-native/android/annotations') +include ':react-native-annotations-compiler' +project(':react-native-annotations-compiler').projectDir = new File(rootProject.projectDir, '../node_modules/react-native/android/annotations-compiler') + apply from: file("../node_modules/@react-native-community/cli-platform-android/native_modules.gradle"); applyNativeModulesSettingsGradle(settings) + include ':app'