Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable module annotation processor with gradle #25922

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .circleci/Dockerfiles/Dockerfile.android
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ ADD ReactAndroid /app/ReactAndroid
ADD ReactCommon /app/ReactCommon
ADD React /app/React
ADD keystores /app/keystores
ADD packages/annotations-compiler /app/packages/annotations-compiler
ADD packages/react-native-codegen /app/packages/react-native-codegen
ADD tools /app/tools

Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ project.xcworkspace
# Gradle
/build/
/packages/react-native-gradle-plugin/build/
/packages/annotations-compiler/annotations/build/
/packages/annotations-compiler/annotations-compiler/build/
/packages/rn-tester/android/app/.cxx/
/packages/rn-tester/android/app/build/
/packages/rn-tester/android/app/gradle/
Expand Down
2 changes: 2 additions & 0 deletions ReactAndroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,8 @@ android {
}

dependencies {
annotationProcessor("com.facebook.react:annotations-compiler")
api("com.facebook.react:annotations")
api("com.facebook.infer.annotation:infer-annotation:0.18.0")
api("com.facebook.yoga:proguard-annotations:1.19.0")
api("javax.inject:javax.inject:1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@
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;
Expand All @@ -31,22 +29,17 @@
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;
janicduplessis marked this conversation as resolved.
Show resolved Hide resolved
import java.util.List;
import java.util.Map;
janicduplessis marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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,
Expand Down Expand Up @@ -79,64 +72,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<? extends NativeModule>[] 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<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>();
for (Class<? extends NativeModule> 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<String, ReactModuleInfo> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,10 @@
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 = {
JSCHeapCapture.class,
Expand All @@ -43,42 +34,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<? extends NativeModule>[] moduleList = new Class[] {JSCHeapCapture.class};

final Map<String, ReactModuleInfo> reactModuleInfoMap = new HashMap<>();
for (Class<? extends NativeModule> 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<String, ReactModuleInfo> 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();
}
}
1 change: 1 addition & 0 deletions ReactAndroid/src/main/java/com/facebook/react/bridge/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,6 @@ rn_android_library(
exported_deps = [
react_native_dep("java/com/facebook/proguard/annotations:annotations"),
react_native_dep("third-party/java/jsr-330:jsr-330"),
react_native_target("java/com/facebook/react/module/annotations:annotations"),
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
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"]),
autoglob = False,
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 = [
"//packages/annotations-compiler/annotations/src/main/java/com/facebook/react/module/annotations:annotations",
],
)
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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"]),
autoglob = False,
labels = ["supermodule:xplat/default/public.react_native.infra"],
visibility = [
"PUBLIC",
],
deps = [
react_native_dep("third-party/java/jsr-305:jsr-305"),
exported_deps = [
"//packages/annotations-compiler/annotations/src/main/java/com/facebook/react/module/model:model",
],
)
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -8,22 +8,6 @@ rn_java_annotation_processor(
"PUBLIC",
],
deps = [
":processing-lib",
],
)

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"),
"//packages/annotations-compiler/annotations-compiler/src/main/java/com/facebook/react/module/processing:processing-lib",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ 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/...",
"//packages/annotations-compiler/...",
],
)

fb_native.remote_file(
Expand Down
5 changes: 4 additions & 1 deletion ReactAndroid/src/main/third-party/java/javapoet/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ 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/...",
"//packages/annotations-compiler/...",
],
)

fb_native.remote_file(
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"Libraries",
"LICENSE",
"local-cli",
"packages/annotations-compiler",
"React-Core.podspec",
"react-native.config.js",
"react.gradle",
Expand Down
32 changes: 32 additions & 0 deletions packages/annotations-compiler/annotations-compiler/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

repositories {
google()
mavenCentral()
}

apply plugin: 'java'

group = "com.facebook.react"

sourceSets.configureEach { sourceSet ->
tasks.named(sourceSet.compileJavaTaskName).configure {
options.annotationProcessorGeneratedSourcesDirectory = file("$buildDir/generated/sources/annotationProcessor/java/${sourceSet.name}")
}
}

java {
sourceCompatibility(JavaVersion.VERSION_11)
targetCompatibility(JavaVersion.VERSION_11)
}

dependencies {
implementation('com.squareup:javapoet:1.13.0')
implementation('com.facebook.infer.annotation:infer-annotation:0.18.0')
implementation('com.facebook.react:annotations')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
load("//tools/build_defs/oss:rn_defs.bzl", "react_native_dep", "react_native_target", "rn_java_library")

rn_java_library(
name = "processing-lib",
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"),
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.facebook.react.module.processing.ReactModuleSpecProcessor
19 changes: 19 additions & 0 deletions packages/annotations-compiler/annotations/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright (c) Meta Platforms, Inc. and 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'

group = "com.facebook.react"

dependencies {

}

java {
sourceCompatibility(JavaVersion.VERSION_11)
targetCompatibility(JavaVersion.VERSION_11)
}
Loading