Android 온디바이스 AI 실전 개요
1편에서 개요와 트렌드, 2편에서 iOS Core ML·Vision을 다뤘습니다. 3편에서는 **Android(AOS)**에서 ML Kit, TensorFlow Lite, Gemini Nano를 어떻게 쓰는지 Kotlin 기준으로 실전 코드와 함께 정리합니다.
Android 온디바이스 AI 스택 한눈에
앱 레벨
├── Kotlin / Java
├── ML Kit → 즉시 사용 API (텍스트, 얼굴, 바코드, 번역 등)
├── TensorFlow Lite → 커스텀 모델 추론 (NNAPI/GPU 가속)
├── AICore (Android 15+) → Gemini Nano 등 시스템 AI
└── AndroidX CameraX → 카메라 + ML 파이프라인
도구
├── Android Studio → ML Model Binding, TFLite 메타데이터
├── Google AI Studio → Gemini 모델, AICore 배포
└── TensorFlow Lite Converter → Keras/TF 모델 → .tflite 변환
1. ML Kit으로 즉시 사용하기
ML Kit은 Google이 제공하는 온디바이스 API로, 별도 서버 없이 텍스트 인식, 얼굴 검출, 바코드 스캔, 번역 등을 바로 쓸 수 있습니다. iOS도 지원하므로 크로스 플랫폼 시 유리합니다.
1.1 Gradle 의존성
// app/build.gradle.kts
dependencies {
implementation("com.google.mlkit:text-recognition:16.0.0")
implementation("com.google.mlkit:face-detection:16.1.6")
implementation("com.google.mlkit:barcode-scanning:17.2.0")
implementation("com.google.mlkit:translate:17.0.2")
}
1.2 텍스트 인식 (Text Recognition)
import com.google.mlkit.vision.common.InputImage
import com.google.mlkit.vision.text.TextRecognition
import com.google.mlkit.vision.text.latin.TextRecognizerOptions
val recognizer = TextRecognition.getClient(TextRecognizerOptions.DEFAULT_OPTIONS)
val image = InputImage.fromBitmap(bitmap)
recognizer.process(image)
.addOnSuccessListener { result ->
val fullText = result.text
for (block in result.textBlocks) {
for (line in block.lines) {
println(line.text)
}
}
}
.addOnFailureListener { e -> Log.e("MLKit", "텍스트 인식 실패", e) }
1.3 얼굴 검출 (Face Detection)
import com.google.mlkit.vision.face.FaceDetection
import com.google.mlkit.vision.face.FaceDetectorOptions
val options = FaceDetectorOptions.Builder()
.setPerformanceMode(FaceDetectorOptions.PERFORMANCE_MODE_FAST)
.setLandmarkMode(FaceDetectorOptions.LANDMARK_MODE_NONE)
.build()
val detector = FaceDetection.getClient(options)
val image = InputImage.fromBitmap(bitmap)
detector.process(image)
.addOnSuccessListener { faces ->
for (face in faces) {
val bounds = face.boundingBox
val smileProb = face.smilingProbability
// 뷰에 바운딩 박스 그리기 등
}
}
1.4 바코드 스캔
import com.google.mlkit.vision.barcode.BarcodeScanning
import com.google.mlkit.vision.barcode.BarcodeScannerOptions
val options = BarcodeScannerOptions.Builder()
.setBarcodeFormats(Barcode.FORMAT_QR_CODE, Barcode.FORMAT_EAN_13)
.build()
val scanner = BarcodeScanning.getClient(options)
scanner.process(InputImage.fromBitmap(bitmap))
.addOnSuccessListener { barcodes ->
for (barcode in barcodes) {
barcode.rawValue?.let { value -> println("바코드: $value") }
}
}
- ML Kit은 모델이 필요 시 자동 다운로드되며, 설정으로 항상 기기 내 로드만 하도록 할 수 있습니다 (완전 온디바이스).
2. TensorFlow Lite로 커스텀 모델 쓰기
자체 학습한 이미지 분류·객체 검출 모델을 앱에 넣으려면 TensorFlow Lite가 표준입니다. NNAPI·GPU 덕분에 기기별로 가속도 받을 수 있습니다.
2.1 Gradle 의존성
implementation("org.tensorflow:tensorflow-lite:2.14.0")
implementation("org.tensorflow:tensorflow-lite-support:0.4.4")
implementation("org.tensorflow:tensorflow-lite-gpu:2.14.0") // 선택: GPU 가속
2.2 .tflite 모델 배치
app/src/main/assets/아래에model.tflite(및 필요 시 라벨 파일) 넣기.- Android Studio ML Model Binding을 쓰면 자동으로 wrapper 클래스가 생성됩니다.
2.3 Kotlin에서 추론 (이미지 분류 예시)
import org.tensorflow.lite.Interpreter
import java.nio.ByteBuffer
import java.nio.ByteOrder
val model = loadModelFile("model.tflite")
val interpreter = Interpreter(model)
val inputSize = 224 * 224 * 3
val inputBuffer = ByteBuffer.allocateDirect(4 * inputSize).order(ByteOrder.nativeOrder())
// bitmap을 224x224로 리사이즈 후 inputBuffer에 채우기 (정규화 등 포함)
val output = Array(1) { FloatArray(numClasses) }
interpreter.run(inputBuffer, output)
val probs = output[0]
val maxIndex = probs.indices.maxByOrNull { probs[it] } ?: 0
println("예측 클래스: $maxIndex, 확률: ${probs[maxIndex]}")
2.4 GPU 위임 (선택)
import org.tensorflow.lite.gpu.GpuDelegate
val gpuDelegate = GpuDelegate()
val options = Interpreter.Options().apply { addDelegate(gpuDelegate) }
val interpreter = Interpreter(model, options)
// 사용 후 gpuDelegate.close()
- 저사양 기기에서는 GPU 위임이 오히려 느릴 수 있으니, NNAPI 또는 CPU와 성능을 비교해 보는 것이 좋습니다.
3. Gemini Nano (AICore)로 온디바이스 LLM
Android 15+와 AICore가 탑재된 기기에서는 Gemini Nano를 앱에서 호출할 수 있습니다. 채팅, 요약, 스마트 회신 등에 활용 가능하고, 완전 온디바이스로 동작합니다.
3.1 지원 여부 확인
import androidx.aicore.aicore.AICoreManager
val aicoreManager = AICoreManager.from(context)
if (!aicoreManager.isGeminiNanoAvailable()) {
// Gemini Nano 미지원 → 클라우드 API 또는 다른 UX 폴백
return
}
3.2 Gemini Nano 사용 (AICore API)
- AICore SDK는 Google에서 제공하는 라이브러리를 추가한 뒤, GenerativeModel에 온디바이스 설정을 넘겨 사용합니다.
- 정확한 API는 Android 15 AICore 문서와 Google AI Studio 가이드를 참고하는 것이 좋습니다. 일반적인 패턴은 다음과 비슷합니다.
// 개념적 예시 (실제 패키지/클래스명은 AICore 문서 참고)
val model = GenerativeModel(
modelName = "gemini-nano",
device = Device.LOCAL // 온디바이스
)
val response = model.generateContent("요약해줘: ...")
- 주의: 지원 기기만 제한적으로 있으므로,
isGeminiNanoAvailable()체크 후 미지원 시에는 Gemini API(클라우드) 또는 간단한 규칙 기반 로직으로 폴백하는 설계가 필요합니다.
4. 실전 팁: 성능과 호환성
| 항목 | 권장 |
|---|---|
| ML Kit | 공통 기능은 ML Kit으로 먼저 구현 → 개발 속도와 유지보수에 유리 |
| TFLite | 입력 크기(예: 224×224)에 맞춰 리사이즈·정규화 후 추론 |
| 백그라운드 | 추론은 Executor/코루틴으로 메인 스레드 밖에서 실행 |
| 모델 다운로드 | ML Kit은 첫 실행 시 다운로드 가능 → 필요 시 “모델 다운로드 중” UX |
| Gemini Nano | AICore 가용 여부 확인 후 사용, 미지원 기기는 API 또는 비AI 폴백 |
5. iOS와 비교 정리
| 기능 | iOS | Android |
|---|---|---|
| 즉시 사용 비전/NLP | Vision, Natural Language | ML Kit |
| 커스텀 모델 | Core ML (.mlmodel) | TensorFlow Lite (.tflite) |
| 모델 학습/변환 | Create ML, coremltools | TF/Keras → TFLite Converter |
| 온디바이스 LLM | Apple Intelligence 연동 (API 확대 중) | Gemini Nano (AICore) |
| 하드웨어 가속 | Neural Engine, Metal | NNAPI, GPU Delegate |
- 크로스 플랫폼을 노린다면 ML Kit을 iOS·Android 공통으로 쓰고, 플랫폼별로만 Core ML / TFLite를 추가하는 구성이 많이 쓰입니다.
6. 시리즈 마무리
- 1편: 온디바이스 AI 개념, Apple/Google 전략.
- 2편: iOS Core ML, Vision, Create ML, App Intents.
- 3편: Android ML Kit, TensorFlow Lite, Gemini Nano(AICore).
세 편을 따라오시면 iOS와 Android 모두에서 온디바이스 AI를 설계하고 구현하는 흐름을 잡을 수 있습니다. 다음 단계로는 실제 앱에 한 가지 기능만 붙여 보기(예: ML Kit 텍스트 인식 + TFLite/Core ML 커스텀 분류)를 추천합니다.
BlueFox Dev - iOS·Android 온디바이스 AI 완벽 가이드 3편