[Developer] #218 Real Gemma 4 E2B integration via flutter_gemma 0.16.5

Implements the OQ-1 follow-up to #215 v0.2.0: replace the placeholder
GemmaLlmService stub with a real flutter_gemma 0.16.5 backend driving
Gemma 4 E2B (litert-community/gemma-4-E2B-it-litert-lm, 2.41GB).

Highlights:
- GemmaLlmService.load → FlutterGemma.initialize + installModel.fromFile +
  getActiveModel; idempotent + FileSystemException on missing file.
- generateStructured uses Gemma 4 native function calling via
  createChat(tools: [Tool(...)], toolChoice: required). Stream parsed by
  collectFunctionCall — first FCR wins, ParallelFCR first-call wins,
  TextResponse/ThinkingResponse skipped, errors sanitized to prevent
  prompt leakage.
- main.dart wires _LazyLlmService adapter that resolves to GemmaLlmService
  when ModelLifecycle reports ready, MockLlmService otherwise.
- ai_providers.dart pins real model URL + SHA-256 (181938...39a63c).
- F2 hardening: ModelLifecycle.purge wraps each delete + meta remove in
  try/catch so a single OS-level flake cannot block opt-out.
- Android: INTERNET / FOREGROUND_SERVICE / POST_NOTIFICATIONS permissions
  + R8 proguard-rules.pro keeping MediaPipe / LiteRT / TFLite / protobuf
  JNI entry points (release builds otherwise crash on first inference).

Design-First: fn-gemma_llm_service.md updated to v2 — §C
(_appendSchemaInstruction) deprecated after reading flutter_gemma
0.16.5 source (Gemma 4 SDK injects tool declarations via template;
prompt-side append would double-wrap).

Tests:
- 10 new unit tests for collectFunctionCall covering all 8 fn-spec
  cases + 2 ParallelFunctionCallResponse paths.
- All 81 existing tests still pass.
- flutter analyze: 0 issues.

Refs #218

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-06-12 15:18:08 +09:00
parent a1f3c5f85d
commit 9a9eb2abd5
14 changed files with 646 additions and 175 deletions

View File

@@ -30,6 +30,13 @@ android {
// TODO: Add your own signing config for the release build.
// Signing with the debug keys for now, so `flutter run --release` works.
signingConfig = signingConfigs.getByName("debug")
// #218: keep flutter_gemma JNI bindings — see proguard-rules.pro.
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(
getDefaultProguardFile("proguard-android-optimize.txt"),
"proguard-rules.pro"
)
}
}
}

30
app/android/app/proguard-rules.pro vendored Normal file
View File

@@ -0,0 +1,30 @@
# flutter_gemma 0.16.5 keep MediaPipe + LiteRT native bindings (#218)
# Without these the release build (R8 minify) strips JNI entry points
# and the first inference call crashes with NoSuchMethodError.
# MediaPipe LLM (.task path)
-keep class com.google.mediapipe.** { *; }
-dontwarn com.google.mediapipe.**
# LiteRT runtime (.litertlm path used by Gemma 4 E2B)
-keep class com.google.ai.edge.** { *; }
-keep class com.google.ai.litert.** { *; }
-dontwarn com.google.ai.edge.**
-dontwarn com.google.ai.litert.**
# TensorFlow Lite (used by LiteRT under the hood)
-keep class org.tensorflow.lite.** { *; }
-dontwarn org.tensorflow.lite.**
# Protobuf-lite (LiteRT message classes referenced via reflection)
-keep class com.google.protobuf.** { *; }
-dontwarn com.google.protobuf.**
# flutter_gemma plugin's own native bridge
-keep class dev.flutterberlin.flutter_gemma.** { *; }
-dontwarn dev.flutterberlin.flutter_gemma.**
# Generic JNI methods covers any LiteRT/MediaPipe class loaded dynamically
-keepclasseswithmembernames class * {
native <methods>;
}

View File

@@ -1,4 +1,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<!-- #218: flutter_gemma downloads ≈ 2.4GB model checkpoint. -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!-- Foreground service for large downloads (>500MB auto-detect). -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<application
android:label="life_helper"
android:name="${applicationName}"