Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): enable Android release R8 optimization across regions (#6124)

boboe57a79793b95
diff --git a/apps/mobile/app.json b/apps/mobile/app.json
index a53b0a4c85..b7fc3702e7 100644
--- a/apps/mobile/app.json
+++ b/apps/mobile/app.json
@@ -65,12 +65,17 @@
[
"expo-build-properties",
{
+ "android": {
+ "enableMinifyInReleaseBuilds": true,
+ "enableShrinkResourcesInReleaseBuilds": true
+ },
"ios": {
"buildReactNativeFromSource": true,
"enableSceneSupport": true
}
}
],
+ "./plugins/with-android-release-optimization",
[
"expo-splash-screen",
{
diff --git a/apps/mobile/modules/cindy-remote-credentials/android/build.gradle b/apps/mobile/modules/cindy-remote-credentials/android/build.gradle
index 044926b0cb..906f42f6fd 100644
--- a/apps/mobile/modules/cindy-remote-credentials/android/build.gradle
+++ b/apps/mobile/modules/cindy-remote-credentials/android/build.gradle
@@ -10,6 +10,7 @@ android {
minSdkVersion 24
versionCode 1
versionName '0.1.0'
+ consumerProguardFiles 'consumer-rules.pro'
}
sourceSets.main.assets.srcDirs += '../../../../../packages/remote-credentials-native/Sources/CindyRemoteCredentials/Resources'
}
diff --git a/apps/mobile/modules/cindy-remote-credentials/android/consumer-rules.pro b/apps/mobile/modules/cindy-remote-credentials/android/consumer-rules.pro
new file mode 100644
index 0000000000..81d9930c64
--- /dev/null
+++ b/apps/mobile/modules/cindy-remote-credentials/android/consumer-rules.pro
@@ -0,0 +1,6 @@
+# Nimbus JOSE JWT 10.9.1 optionally uses Tink for XC20P content encryption.
+# CredentialCrypto.kt creates and accepts only ECDH-ES + A256GCM (JCA-backed),
+# so this optional backend is not shipped. Revisit this rule and the dependency
+# if the credential protocol ever enables XC20P; do not suppress other classes.
+# https://connect2id.com/products/nimbus-jose-jwt/examples/jwe-with-xc20p
+-dontwarn com.google.crypto.tink.subtle.XChaCha20Poly1305
diff --git a/apps/mobile/modules/xdt-tapdb/android/build.gradle b/apps/mobile/modules/xdt-tapdb/android/build.gradle
index 3ffde2b309..9a3c7af3ff 100644
--- a/apps/mobile/modules/xdt-tapdb/android/build.gradle
+++ b/apps/mobile/modules/xdt-tapdb/android/build.gradle
@@ -15,6 +15,7 @@ android {
defaultConfig {
minSdkVersion 23
+ consumerProguardFiles 'consumer-rules.pro'
}
}
diff --git a/apps/mobile/modules/xdt-tapdb/android/consumer-rules.pro b/apps/mobile/modules/xdt-tapdb/android/consumer-rules.pro
new file mode 100644
index 0000000000..2ff4859344
--- /dev/null
+++ b/apps/mobile/modules/xdt-tapdb/android/consumer-rules.pro
@@ -0,0 +1,6 @@
+# TapSDK 4.x is already obfuscated and uses service loading / reflection.
+# Keep the vendor namespaces as required by its integration guide. Put the rules
+# on the wrapper so they reach every consuming CN/Global release build.
+# https://developer.taptap.io/docs/sdk/access/quickstart/#android-code-obfuscation
+-keep class com.taptap.** { *; }
+-keep class com.tapsdk.** { *; }
diff --git a/apps/mobile/plugins/with-android-release-optimization.js b/apps/mobile/plugins/with-android-release-optimization.js
new file mode 100644
index 0000000000..7269ebd683
--- /dev/null
+++ b/apps/mobile/plugins/with-android-release-optimization.js
@@ -0,0 +1,27 @@
+const { withAppBuildGradle } = require('@expo/config-plugins');
+
+// expo-build-properties enables R8, but Expo's Android template still selects
+// proguard-android.txt, which contains -dontoptimize. Apply the optimizing defaults
+// during prebuild so every region and build entry point gets the same rules,
+// including clean prebuilds that replace the generated Android project.
+function enableDefaultProguardOptimizations(source) {
+ const defaultFile = /getDefaultProguardFile\(\s*(['"])proguard-android(?:-optimize)?\.txt\1\s*\)/g;
+ if (!defaultFile.test(source)) {
+ throw new Error('with-android-release-optimization: expected an Android default ProGuard file; review the Expo template before building');
+ }
+ return source.replace(defaultFile, (_match, quote) =>
+ `getDefaultProguardFile(${quote}proguard-android-optimize.txt${quote})`);
+}
+
+function withAndroidReleaseOptimization(config) {
+ return withAppBuildGradle(config, (androidConfig) => {
+ if (androidConfig.modResults.language !== 'groovy') {
+ throw new Error('with-android-release-optimization: expected a Groovy app build.gradle');
+ }
+ androidConfig.modResults.contents = enableDefaultProguardOptimizations(androidConfig.modResults.contents);
+ return androidConfig;
+ });
+}
+
+module.exports = withAndroidReleaseOptimization;
+module.exports.enableDefaultProguardOptimizations = enableDefaultProguardOptimizations;
diff --git a/apps/mobile/src/__tests__/androidReleaseOptimizationPlugin.test.ts b/apps/mobile/src/__tests__/androidReleaseOptimizationPlugin.test.ts
new file mode 100644
index 0000000000..0875c04f7b
--- /dev/null
+++ b/apps/mobile/src/__tests__/androidReleaseOptimizationPlugin.test.ts
@@ -0,0 +1,67 @@
+import { execFileSync } from 'node:child_process';
+import { createRequire } from 'node:module';
+import { dirname, resolve } from 'node:path';
+
+import { describe, expect, it } from 'vitest';
+
+const require = createRequire(import.meta.url);
+const { enableDefaultProguardOptimizations } = require('../../plugins/with-android-release-optimization.js') as {
+ enableDefaultProguardOptimizations: (source: string) => string;
+};
+
+describe('Android release R8 configuration', () => {
+ it.each(['cn', 'global', 'dev'])('enables code and resource shrinking for %s', (region) => {
+ // Resolve the real dynamic Expo config without inheriting build-machine
+ // identity, credentials, or private region configuration from the test runner.
+ const output = execFileSync(process.execPath, ['-e', `
+ if (process.env.EXPO_PUBLIC_CINDY_AUTH_REGION === 'dev') {
+ const fs = require('node:fs');
+ const path = require('node:path');
+ const readFileSync = fs.readFileSync;
+ fs.readFileSync = function(file, ...args) {
+ if (String(file) === path.resolve('../../config/endpoint.dev.json')) {
+ return JSON.stringify({ schemaVersion: 1, cdnBaseUrl: 'https://example.invalid/cindy' });
+ }
+ return readFileSync.call(this, file, ...args);
+ };
+ }
+ const config = require('./app.config.js')();
+ console.log(JSON.stringify({
+ properties: config.plugins.find((item) => Array.isArray(item) && item[0] === 'expo-build-properties')[1],
+ optimizerRegistered: config.plugins.includes('./plugins/with-android-release-optimization'),
+ }));
+ `], {
+ cwd: resolve(__dirname, '../..'),
+ env: { PATH: process.env.PATH, NODE_ENV: 'test', EXPO_PUBLIC_CINDY_AUTH_REGION: region },
+ encoding: 'utf8',
+ });
+ const config = JSON.parse(output);
+ expect(config.properties.android).toEqual({
+ enableMinifyInReleaseBuilds: true,
+ enableShrinkResourcesInReleaseBuilds: true,
+ });
+ expect(config.optimizerRegistered).toBe(true);
+ });
+
+ it('replaces the non-optimizing defaults in the installed Expo template and survives repeated prebuilds', () => {
+ const templatePath = resolve(dirname(require.resolve('expo/package.json')), 'template.tgz');
+ const template = execFileSync('tar', ['-xOf', templatePath, 'package/android/app/build.gradle'], { encoding: 'utf8' });
+ const optimized = enableDefaultProguardOptimizations(template);
+ expect(optimized).toContain('getDefaultProguardFile("proguard-android-optimize.txt")');
+ expect(optimized).not.toContain('getDefaultProguardFile("proguard-android.txt")');
+ expect(optimized).toContain('minifyEnabled enableMinifyInReleaseBuilds');
+ expect(optimized).toContain('"proguard-rules.pro"');
+ expect(enableDefaultProguardOptimizations(optimized)).toBe(optimized);
+ });
+
+ it('keeps application and SDK rules while replacing a single-quoted default', () => {
+ const source = "proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro', 'sdk-rules.pro'";
+ expect(enableDefaultProguardOptimizations(source)).toBe(
+ "proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro', 'sdk-rules.pro'",
+ );
+ });
+
+ it('stops prebuild when the default rule-file contract changes', () => {
+ expect(() => enableDefaultProguardOptimizations("proguardFiles 'custom.pro'")).toThrow(/Expo template/);
+ });
+});
diff --git a/docs/dev-rules/mobile-development.md b/docs/dev-rules/mobile-development.md
index 0e67809543..10343b4eb9 100644
--- a/docs/dev-rules/mobile-development.md
+++ b/docs/dev-rules/mobile-development.md
@@ -205,6 +205,31 @@ pnpm --filter mobile test:smoke
与 `IncomingShareBridge.tsx`。回归:`src/__tests__/incomingShare*.test.ts` 和
`nativeIntent.test.ts`。上述 TS 层修复不修改原生配置或模块,不需要为此改变 runtime fingerprint。
+## Android release 代码优化
+
+所有地区共用 `app.json` 的 `expo-build-properties.android` 配置,在 release 构建中开启
+R8 代码混淆/裁剪与资源裁剪。`plugins/with-android-release-optimization.js` 在每次
+prebuild 时将 Expo 模板默认的 `proguard-android.txt` 换成
+`proguard-android-optimize.txt`,避免默认规则中的 `-dontoptimize` 禁用代码优化。
+不要手改生成的 `android/` 工程;clean prebuild 会覆盖它。应用和依赖 SDK 的必要 keep
+规则继续生效,遇到反射/JNI 兼容问题时只补充实际需要保留的入口,不整体关闭 R8。
+
+这些原生配置参与 runtime fingerprint,首次启用须按下文冷更边界处理,并使用 release
+原生包回归启动、登录、通知和原生模块调用。
+
+SDK 规则随依赖一起进入应用的最终 R8 配置,不在应用侧重复保留全部依赖:
+
+| Android SDK / 桥接 | 保留规则来源与边界 |
+| --- | --- |
+| React Native、Reanimated / Worklets | 使用依赖自带的 consumer rules,保留 JNI / TurboModule / Fabric 入口。 |
+| Expo 原生模块 | 使用 `expo-modules-core` 的 consumer rules,保留模块构造与 `definition()`、Record、视图等动态入口。自定义模块的函数名通过 `ModuleDefinition` 字符串注册。 |
+| 微信 OpenSDK 6.8.38 | Maven AAR 自带 `com.tencent.mm.opensdk` / `com.tencent.wxop` 规则;微信回调 Activity 由 config plugin 写入 manifest,由 Android 构建规则保留。 |
+| TapSDK 4.10.5 | `modules/xdt-tapdb/android/consumer-rules.pro` 按[官方接入要求](https://developer.taptap.io/docs/sdk/access/quickstart/#android-code-obfuscation)保留 `com.taptap` / `com.tapsdk`,覆盖服务发现及反射调用。 |
+| Nimbus JOSE JWT 10.9.1 | 远控凭证模块直接调用 JOSE API,协议 JSON 使用显式键名的 `JSONObject` / Map,不依赖业务类字段名反射序列化。`cindy-remote-credentials/android/consumer-rules.pro` 仅忽略未启用的 XC20P 算法所需的可选 Tink 类;当前协议创建和接收均限制为 `ECDH-ES + A256GCM`。若扩展算法,须重新核对可选依赖。 |
+
+升级 SDK 时核对其 AAR/JAR 的 consumer rules、应用的最终 `configuration.txt` 和 release
+运行结果。共享的 Expo 配置也可能改变 iOS 指纹,冷更范围以指纹比对为准。
+
## 原生配置与 runtime fingerprint(冷更边界)
### 伙伴通信通知