惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
P
Palo Alto Networks Blog
月光博客
月光博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
aimingoo的专栏
aimingoo的专栏
腾讯CDC
T
Threatpost
D
DataBreaches.Net
Vercel News
Vercel News
F
Fortinet All Blogs
Engineering at Meta
Engineering at Meta
C
Cybersecurity and Infrastructure Security Agency CISA
Forbes - Security
Forbes - Security
U
Unit 42
C
Check Point Blog
Blog — PlanetScale
Blog — PlanetScale
O
OpenAI News
量子位
TaoSecurity Blog
TaoSecurity Blog
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
V
Visual Studio Blog
Recorded Future
Recorded Future
云风的 BLOG
云风的 BLOG
Security Archives - TechRepublic
Security Archives - TechRepublic
The Last Watchdog
The Last Watchdog
S
Security Affairs
Attack and Defense Labs
Attack and Defense Labs
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
Microsoft Security Blog
Microsoft Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
V
V2EX
小众软件
小众软件
S
SegmentFault 最新的问题
www.infosecurity-magazine.com
www.infosecurity-magazine.com
W
WeLiveSecurity
AI
AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 聂微东
I
Intezer
Know Your Adversary
Know Your Adversary
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
博客园_首页
NISL@THU
NISL@THU
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Beyond SQL: How to Build a High-Performance On-Device Vector Search Engine for Android
Programming · 2026-05-10 · via DEV Community

In the traditional world of Android development, we’ve spent decades perfecting the art of the exact match. We write SQL queries like SELECT * FROM users WHERE id = 5 or WHERE name LIKE '%Apple%'. This works perfectly for structured data, but it fails miserably when we try to interact with the messy, nuanced world of human language.

Imagine a user searching their notes app for "the feeling of a rainy afternoon in Kyoto." A traditional database would look for those exact words. If the user’s note actually said, "The petrichor filled the air as I walked through the Gion district under a gray sky," the search would return zero results.

The gap between what a user means and what a computer sees is the final frontier of mobile UX. To bridge this gap, we have to move away from discrete symbols—strings and integers—and into the world of continuous high-dimensional space. We need to build a Vector Search Repository.
(This article is based on the ebook On-Device GenAI with Android Kotlin)

The Theoretical Foundation: Translating Meaning into Geometry

At its core, a Vector Search Repository is not a traditional database; it is a geometric engine. To understand how it works, we must first master the concept of Embeddings.

1. The Concept of Embeddings

An embedding is a numerical representation of data—be it text, images, or audio—as a dense vector of floating-point numbers. When we "embed" a piece of text, we are essentially plotting it as a point in a space that might have 512, 768, or even thousands of dimensions.

If we represent the word "Apple" in a 3D space, it might look like [0.12, -0.59, 0.88]. In a production-grade model like Gemini Nano, these vectors are far more complex. Each dimension represents a latent feature of the data—features the model learned during training, such as "fruit-ness," "technology-ness," or "sentiment."

The Geometry of Meaning:
In this high-dimensional space, semantic similarity is equivalent to geometric proximity. If two pieces of text are conceptually similar, their corresponding vectors will be positioned close to one another.

  • Semantic Proximity: "The king's crown" and "The monarch's headpiece" will result in vectors that are nearly identical because they describe the same concept.
  • Semantic Distance: "The king's crown" and "A recipe for chocolate cake" will result in vectors that are geometrically distant because they share no conceptual overlap.

2. Similarity Metrics: How We Measure "Closeness"

Once we have transformed our data into vectors, we need a mathematical way to calculate the distance between them. In on-device AI development, we generally rely on three primary metrics:

A. Cosine Similarity
This is the gold standard for Natural Language Processing (NLP). Instead of measuring the straight-line distance between two points, it measures the angle between two vectors.

  • Why it matters: It ignores the magnitude (length) of the vector and focuses on the direction. This is critical because a short sentence and a long paragraph might discuss the same topic; their vectors will point in the same direction even if the paragraph’s vector is "longer."

B. Euclidean Distance (L2)
This measures the straight-line distance between two points in space. It is most effective when the magnitude of the vector is just as important as its direction.

C. Dot Product
A mathematical operation that combines magnitude and angle. This is often used in high-performance neural networks where vectors are already normalized, allowing for lightning-fast calculations.

AICore: The System-Level Revolution

Google’s introduction of AICore marks a massive shift in how we handle AI on Android. In the past, if you wanted to run a Large Language Model (LLM) or an embedding engine, you had to bundle the model within your app. This was a disaster for resources. A single model can take up gigabytes of RAM and drain the battery in minutes.

The Shared Provider Model

Just as CameraX abstracts fragmented camera hardware into a unified API, AICore acts as a system-level service that abstracts AI hardware (NPUs and TPUs).

  • Centralized Model Management: AICore manages the lifecycle of models like Gemini Nano. It handles the heavy lifting of downloading, updating, and loading models into the NPU.
  • Resource Arbitration: It ensures that multiple apps aren't fighting for the NPU simultaneously, managing the "scheduling" of AI inference tasks so the device stays responsive.
  • Privacy First: The data never leaves the device. AICore provides the interface for the app to send a prompt and receive a vector without any cloud round-trips.

Think of the transition from local app-specific models to AICore as similar to moving from raw SQLite cursors to Room. AICore is the "Room" for LLMs; it handles the "migration" of model weights and the "threading" of hardware acceleration.

Mapping AI Concepts to Modern Kotlin 2.x

Building a Vector Repository requires a bridge between the asynchronous, heavy-compute nature of AI and the reactive nature of the Android UI. Kotlin 2.x provides the perfect toolset for this.

  1. Coroutines and Structured Concurrency: Generating embeddings is a blocking, CPU/NPU-intensive operation. We utilize Dispatchers.Default for mathematical calculations to ensure we don't freeze the Main thread.
  2. Kotlin Flow for Streaming Results: Vector search often involves "Top-K" retrieval (e.g., "Give me the 5 most similar results"). As the repository scans the vector space, we can use Flow to stream results back to the UI as they are found, rather than waiting for the entire search to complete.
  3. Context Receivers: In Kotlin 2.x, we can use context receivers to ensure that any function performing a vector search has access to the EmbeddingEngine without explicitly passing it as a parameter every time, leading to cleaner, more maintainable code.
  4. Serialization for Persistence: Vectors are essentially FloatArrays. To store these in a local database, we use kotlinx.serialization to efficiently encode these arrays into binary formats like ProtoBuf, minimizing disk I/O.

Step-by-Step Implementation Guide

Let’s build a "Knowledge Base" where we store facts and search through them semantically using the MediaPipe Text Embedder.

1. Gradle Dependencies

First, we need to bring in the MediaPipe tasks and Hilt for dependency injection.

dependencies {
    // MediaPipe Text tasks for embedding generation
    implementation("com.google.mediapipe:tasks-text:0.10.14")

    // Jetpack Compose & Lifecycle
    implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0")
    implementation("androidx.lifecycle:lifecycle-runtime-compose:2.7.0")

    // Hilt for Dependency Injection
    implementation("com.google.dagger:hilt-android:2.50")
    kapt("com.google.dagger:hilt-compiler:2.50")
}

Enter fullscreen mode Exit fullscreen mode

2. The Vector Repository

The repository handles the "heavy lifting" of AI inference and the vector math required for Cosine Similarity.

@Singleton
class VectorSearchRepository @Inject constructor(context: Context) {

    private val textEmbedder: TextEmbedder = TextEmbedder.createFromOptions(
        context,
        TextEmbedder.TextEmbedderOptions.builder()
            .setBaseOptions(
                com.google.mediapipe.tasks.core.BaseOptions.builder()
                    .setModelAssetPath("universal_sentence_encoder.tflite")
                    .build()
            )
            .build()
    )

    private val vectorStore = mutableListOf<VectorItem>()

    suspend fun addTextToRepository(text: String) = withContext(Dispatchers.Default) {
        val result: TextEmbedderResult = textEmbedder.embed(text)
        val embedding = result.embeddingResult().embeddings().get(0).floatArray()
        vectorStore.add(VectorItem(text, embedding))
    }

    suspend fun search(query: String): List<Pair<String, Float>> = withContext(Dispatchers.Default) {
        val queryResult = textEmbedder.embed(query)
        val queryVector = queryResult.embeddingResult().embeddings().get(0).floatArray()

        vectorStore.map { item ->
            val similarity = calculateCosineSimilarity(queryVector, item.embedding)
            item.text to similarity
        }.sortedByDescending { it.second }
    }

    private fun calculateCosineSimilarity(vectorA: FloatArray, vectorB: FloatArray): Float {
        var dotProduct = 0.0f
        var normA = 0.0f
        var normB = 0.0f
        for (i in vectorA.indices) {
            dotProduct += vectorA[i] * vectorB[i]
            normA += vectorA[i] * vectorA[i]
            normB += vectorB[i] * vectorB[i]
        }
        return if (normA == 0f || normB == 0f) 0f else dotProduct / (sqrt(normA) * sqrt(normB))
    }
}

Enter fullscreen mode Exit fullscreen mode

3. The ViewModel and UI

We use a ViewModel to manage the UI state and ensure that our search operations don't leak memory or block the UI.

@HiltViewModel
class VectorViewModel @Inject constructor(
    private val repository: VectorSearchRepository
) : ViewModel() {

    private val _searchResults = MutableStateFlow<List<Pair<String, Float>>>(emptyList())
    val searchResults = _searchResults.asStateFlow()

    fun performSearch(query: String) {
        viewModelScope.launch {
            val results = repository.search(query)
            _searchResults.value = results
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

In the Compose UI, the user enters a query like "Tell me about puppies," and the system retrieves the "Golden Retriever" fact, even if the word "puppy" was never explicitly used in the source text.

Advanced Implementation: Semantic Memory and RAG

In a production-grade application, a Vector Search Repository is more than just a search bar; it is the Semantic Memory of the application. This leads us to Retrieval-Augmented Generation (RAG).

RAG allows the device to search through thousands of local documents, retrieve the most relevant snippets, and feed those snippets into Gemini Nano. This "grounds" the LLM in factual, local context, effectively preventing the "hallucinations" that plague many AI models.

The Production Pipeline

  1. Input: The user asks a question.
  2. Retrieval: The app converts the question to a vector and searches the local Vector Repository (stored in Room via BLOBs).
  3. Augmentation: The top 3 most relevant snippets are retrieved.
  4. Generation: The snippets and the original question are sent to Gemini Nano via AICore.
  5. Output: The user receives a response that is both intelligent and factually accurate based on their own data.

Common Pitfalls to Avoid

1. The Main Thread Trap

Running textEmbedder.embed() on the Main thread is a guaranteed way to trigger an Application Not Responding (ANR) dialog. AI inference is computationally expensive. Always wrap your AI calls in withContext(Dispatchers.Default).

2. Memory Leaks and Model Lifecycle

TFLite models occupy significant native memory. If you create multiple instances of a TextEmbedder, you will quickly run into OutOfMemoryError. Use Hilt’s @Singleton scope to ensure only one instance of the model exists for the entire application lifetime.

3. Vector Normalization

If you use Euclidean Distance instead of Cosine Similarity without normalizing your vectors first, your results will be skewed by the length of the text rather than its meaning. Stick to Cosine Similarity for text-based applications as it inherently handles vector magnitude.

4. Asset Loading Latency

Loading a .tflite model from assets can take several hundred milliseconds. If this happens during the first screen render, the user will experience a visible stutter. Initialize your repository lazily or use a splash screen to mask the loading time.

Conclusion: The New Standard for Android UX

The era of keyword-based search is coming to an end. As users demand more intuitive, "human-like" interactions with their devices, building a robust Vector Search Repository becomes a mandatory skill for modern Android developers.

By leveraging AICore, MediaPipe, and Kotlin 2.x, we can build applications that don't just store data—they understand it. We are moving from apps that are passive tools to apps that act as intelligent partners, capable of navigating the complex geometry of human meaning.

Let's Discuss

  1. How do you see semantic search changing the way users interact with productivity apps like Notes or Email?
  2. Given the privacy benefits of AICore, would you prefer on-device vector search over cloud-based solutions like Pinecone or Weaviate for your next project?

The concepts and code demonstrated here are drawn directly from the comprehensive roadmap laid out in the ebook
On-Device GenAI with Android Kotlin: Mastering Gemini Nano, AICore, and local LLM deployment using MediaPipe and Custom TFLite models. You can find it here: Leanpub.com

Check also all the other programming & AI ebooks with python, typescript, c#, swift, kotlin: Leanpub.com

Android Kotlin & AI Masterclass:
Book 1: On-Device GenAI. Mastering Gemini Nano, AICore, and local LLM deployment using MediaPipe and Custom TFLite models.
Book 2: Edge AI Performance. Optimizing hardware acceleration via NPU (Neural Processing Unit), GPU, and DSP. Advanced quantization and model pruning.
Book 3: Android AI Agents. Building autonomous apps that use Tool Calling, Function Injection, and Screen Awareness to perform tasks for the user.