Skip to main content

Complete Example

Here’s a complete example to get you started with on-device text generation:
MainActivity.kt

Step-by-Step Guide

1. Initialize the SDK

Initialize RunAnywhere in your Activity.onCreate(). The order of initialization is critical:
AndroidPlatformContext.initialize(this) must be called before RunAnywhere.initialize(). CppBridgeModelPaths.setBaseDirectory() tells the C++ bridge where to find model files.

Environment Options

2. Register Backend Modules

Register backends after initialization but before registering models:
LlamaCPP.register() is wrapped in try/catch because VLM native registration may fail if the .so doesn’t include nativeRegisterVlm. LLM text generation still works because it is registered before VLM internally.

3. Register Models

Register models with their framework, modality, and download URL:

4. Download and Load Models

Download with progress tracking using Kotlin Flows:

5. Generate Text

6. Stream Responses

For real-time token streaming:

What’s Next?

LLM Chat

Simple one-liner for quick responses

Streaming

Real-time token streaming with Flows

Speech-to-Text

Transcribe audio to text

Text-to-Speech

Convert text to spoken audio