rwadurian/backend/mpc-system/services/service-party-android
hailin 6f38f96b5a fix(android): 修复架构中导致应用崩溃的 P0 级别 bug
## 修复的崩溃风险 (P0 优先级)

### 1. 修复 lateinit var partyId 未初始化访问崩溃 (100% 崩溃风险)

**问题背景**:
- TssRepository.partyId 是 lateinit var,必须在 registerParty() 中初始化
- 多个关键函数(startSessionEventSubscription、ensureSessionEventSubscriptionActive、startMessageRouting)
  直接访问 partyId,如果在初始化前访问会抛出 UninitializedPropertyAccessException

**崩溃场景**:
1. 网络重连时,registerParty() 未完成就触发会话订阅
2. Activity 快速销毁重建,初始化顺序错乱
3. 后台恢复时,Repository 状态不一致

**解决方案**:
- 添加 requirePartyId() 函数进行强制初始化检查
- 在所有直接访问 partyId 的关键位置使用 requirePartyId()
- 提供清晰的错误日志帮助调试

**修改位置**:
- TssRepository.kt:108-135 - 添加 requirePartyId() 和 getPartyIdOrNull()
- TssRepository.kt:281 - startSessionEventSubscription() 使用 requirePartyId()
- TssRepository.kt:390 - ensureSessionEventSubscriptionActive() 使用 requirePartyId()
- TssRepository.kt:1818 - startMessageRouting() 使用 requirePartyId()

**风险等级**:P0 - 立即修复
**影响范围**:核心会话管理流程
**测试验证**:编译通过,无语法错误

---

### 2. 修复 gRPC Channel 关闭导致的内存泄漏和 ANR

**问题背景**:
- GrpcClient.cleanupConnection() 中 channel.awaitTermination() 是阻塞操作
- 在主线程调用会导致 ANR (Application Not Responding)
- 异常处理不完整,channel 可能未完全关闭

**崩溃/性能问题**:
1. Activity.onDestroy() → cleanup() → 主线程阻塞 → ANR → 应用无响应
2. 网络切换快速 disconnect/reconnect → channel 泄漏 → 内存溢出 → OOM 崩溃
3. 异常中断 → channel 未关闭 → 连接池耗尽 → 后续连接失败

**解决方案**:
- 立即清空 channel/stub/asyncStub 引用,防止复用已关闭的连接
- 在后台 IO 线程异步执行 channel 关闭(scope.launch(Dispatchers.IO))
- 优雅关闭(3秒)→ 强制关闭(1秒)→ 完整异常处理
- 所有异常路径都确保 shutdownNow() 被调用

**修改位置**:
- GrpcClient.kt:235-302 - 重写 cleanupConnection() 逻辑
- 异步关闭 channel,避免主线程阻塞
- 增强异常处理,确保资源释放

**风险等级**:P0 - 立即修复
**影响范围**:网络连接管理、应用生命周期
**测试验证**:编译通过,无语法错误

---

## 修复效果

 **防止应用崩溃**:
- 消除 UninitializedPropertyAccessException 风险
- 避免 ANR 导致的系统强制关闭
- 防止 OOM 导致的内存崩溃

 **提升稳定性**:
- 网络重连更加健壮
- Activity 生命周期管理更安全
- 资源清理更加完整

 **改善用户体验**:
- 减少无响应提示
- 降低内存占用
- 提高连接成功率

## 技术债务

待修复的问题(后续 PR):
- P0-3: 实现统一的 Job 管理器
- P1: 竞态条件、OkHttpClient 连接池清理
- P2: 协程全局异常处理

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-01-26 21:02:00 -08:00
..
app fix(android): 修复架构中导致应用崩溃的 P0 级别 bug 2026-01-26 21:02:00 -08:00
contracts feat(blockchain): 切换到dUSDT(绿积分)合约 - KAVA主网 2026-01-02 04:31:11 -08:00
gradle/wrapper feat(android): add Android TSS Party app with full API implementation 2025-12-31 23:27:29 -08:00
tsslib feat(tss): add real-time round progress from msg.Type() parsing 2026-01-01 22:41:51 -08:00
.gitignore feat(android): strengthen gRPC connection reliability 2026-01-01 06:44:42 -08:00
IMPLEMENTATION_PLAN.md feat(android): 实现 2-of-3 钱包服务器备份参与签名功能 2026-01-26 17:32:36 -08:00
README.md feat(android): add Android TSS Party app with full API implementation 2025-12-31 23:27:29 -08:00
build-apk.bat fix(android): simplify build-apk.bat with official gomobile setup 2026-01-01 00:53:04 -08:00
build.gradle.kts feat(android): add Android TSS Party app with full API implementation 2025-12-31 23:27:29 -08:00
gradle.properties feat(android): add Android TSS Party app with full API implementation 2025-12-31 23:27:29 -08:00
gradlew feat(android): add Android TSS Party app with full API implementation 2025-12-31 23:27:29 -08:00
gradlew.bat feat(android): add Android TSS Party app with full API implementation 2025-12-31 23:27:29 -08:00
settings.gradle.kts feat(android): add Android TSS Party app with full API implementation 2025-12-31 23:27:29 -08:00

README.md

TSS Party Android

Android 版本的 TSS (Threshold Signature Scheme) Party 应用,用于多方共管钱包的密钥生成和签名。

项目结构

service-party-android/
├── app/                          # Android 应用模块
│   ├── src/main/
│   │   ├── java/com/durian/tssparty/
│   │   │   ├── data/             # 数据层
│   │   │   │   ├── local/        # 本地存储 (Room, TSS Bridge)
│   │   │   │   ├── remote/       # 远程通信 (gRPC)
│   │   │   │   └── repository/   # 数据仓库
│   │   │   ├── domain/model/     # 领域模型
│   │   │   ├── presentation/     # UI 层
│   │   │   │   ├── screens/      # Compose 屏幕
│   │   │   │   └── viewmodel/    # ViewModels
│   │   │   ├── di/               # Hilt 依赖注入
│   │   │   ├── ui/theme/         # Material Theme
│   │   │   └── util/             # 工具类
│   │   ├── proto/                # gRPC Proto 文件
│   │   └── res/                  # Android 资源
│   └── libs/                     # TSS 原生库 (.aar)
├── tsslib/                       # Go TSS 库源码
│   ├── tsslib.go                 # gomobile 绑定
│   ├── go.mod
│   ├── build.sh                  # Linux/macOS 构建脚本
│   └── build.bat                 # Windows 构建脚本
└── gradle/                       # Gradle Wrapper

技术栈

  • UI: Jetpack Compose + Material 3
  • 架构: MVVM + Repository Pattern
  • 依赖注入: Hilt
  • 数据库: Room
  • 网络: gRPC (protobuf-lite)
  • TSS 核心: Go + gomobile (BnB Chain tss-lib v2)

构建步骤

1. 构建 TSS 原生库 (可选,需要 Go 环境)

# 安装 gomobile
go install golang.org/x/mobile/cmd/gomobile@latest
gomobile init

# 构建 Android AAR
cd tsslib
./build.sh  # Linux/macOS
# 或
build.bat   # Windows

这将在 app/libs/ 生成 tsslib.aar

注意: 当前版本使用 Kotlin stub 实现,无需编译 Go 库即可构建 APK。 实际运行需要真正的 tsslib.aar

2. 构建 APK

# Debug 版本
./gradlew assembleDebug

# Release 版本 (需要签名配置)
./gradlew assembleRelease

APK 输出路径: app/build/outputs/apk/debug/app-debug.apk

功能

  1. 加入 Keygen 会话 - 扫描/输入邀请码,参与多方密钥生成
  2. 查看钱包 - 显示已创建的共管钱包列表
  3. 签名交易 - 使用密钥份额参与多方签名
  4. 设置 - 配置 Message Router 服务器地址

配置

默认服务器配置:

  • Message Router: localhost:50051
  • Kava RPC: https://evm.kava.io

与 Electron 版本的对应关系

Electron 版本 Android 版本
electron/main.ts TssNativeBridge.kt + GrpcClient.kt
electron/preload.ts TssRepository.kt
src/pages/*.tsx presentation/screens/*.kt
tss-party/ (Go 子进程) tsslib/ (gomobile .aar)
sql.js Room Database

许可证

MIT