From 95731527a258f4e6f801016bfd5c2d257d5ffde9 Mon Sep 17 00:00:00 2001 From: injun choi Date: Tue, 21 Jul 2026 11:32:08 +0900 Subject: [PATCH] fix(android): forward ExecuTorch consumer ProGuard rules --- .../android/build.gradle | 1 + .../android/consumer-proguard-rules.pro | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 packages/react-native-executorch/android/consumer-proguard-rules.pro diff --git a/packages/react-native-executorch/android/build.gradle b/packages/react-native-executorch/android/build.gradle index 5b1cfd2973..be4a2f5373 100644 --- a/packages/react-native-executorch/android/build.gradle +++ b/packages/react-native-executorch/android/build.gradle @@ -115,6 +115,7 @@ android { defaultConfig { minSdkVersion getExtOrIntegerDefault("minSdkVersion") targetSdkVersion getExtOrIntegerDefault("targetSdkVersion") + consumerProguardFiles "consumer-proguard-rules.pro" externalNativeBuild { cmake { abiFilters (*reactNativeArchitectures()) diff --git a/packages/react-native-executorch/android/consumer-proguard-rules.pro b/packages/react-native-executorch/android/consumer-proguard-rules.pro new file mode 100644 index 0000000000..de12e0bdec --- /dev/null +++ b/packages/react-native-executorch/android/consumer-proguard-rules.pro @@ -0,0 +1,51 @@ +# Ported from ExecuTorch's official Android consumer rules: +# https://github.com/pytorch/executorch/blob/904c667007aed15e9a3bb8278aa271b479450c85/extension/android/executorch_android/consumer-proguard-rules.pro + +# ExecuTorch Android AAR — Consumer ProGuard/R8 Rules +# +# These rules are automatically applied to any app that depends on the +# ExecuTorch AAR. They prevent R8/ProGuard from stripping classes and +# methods that are called from native (JNI) code. + +# Keep ExecuTorch classes and members annotated with @DoNotStrip. +# Scoped to org.pytorch.executorch to avoid affecting unrelated libraries. +-keep @com.facebook.jni.annotations.DoNotStrip class org.pytorch.executorch.** { *; } +-keepclassmembers class org.pytorch.executorch.** { + @com.facebook.jni.annotations.DoNotStrip *; +} + +# Keep all native methods across ExecuTorch packages. +# Use -keepclasseswithmembers (not -keepclasseswithmembernames) to prevent +# both shrinking and obfuscation of JNI entry points. +-keepclasseswithmembers class org.pytorch.executorch.** { + native ; +} + +# Keep HybridData fields (accessed by fbjni via reflection). +-keepclassmembers class org.pytorch.executorch.** { + com.facebook.jni.HybridData *; +} + +# Keep ExecutorchRuntimeException and its factory/subclasses. +# These are instantiated from JNI via jni_helper.cpp. +-keep class org.pytorch.executorch.ExecutorchRuntimeException { *; } +-keep class org.pytorch.executorch.ExecutorchRuntimeException$* { *; } + +# Keep EValue (fields and type codes accessed from native code). +-keep class org.pytorch.executorch.EValue { *; } + +# Keep Tensor and its inner classes. The Tensor class has methods and fields +# accessed from JNI (dtypeJniCode, shape, getRawDataBuffer, nativeNewTensor). +-keep class org.pytorch.executorch.Tensor { *; } +-keep class org.pytorch.executorch.Tensor$* { *; } + +# Keep LlmCallback interface methods (invoked from native code). +-keep interface org.pytorch.executorch.extension.llm.LlmCallback { *; } + +# Keep AsrCallback interface methods (invoked from native code). +-keep interface org.pytorch.executorch.extension.asr.AsrCallback { *; } + +# --- react-native-executorch rules (not from upstream) --- + +# RNE's own JNI entry point, resolved by literal descriptor from C++. +-keep class com.swmansion.rnexecutorch.ETInstaller { *; }