Chapters

Hide chapters

Practical Android AI

First Edition · Android 13 · Kotlin 2.0 · Android Studio Otter

9. Best Practices, Ethics, and the Future of Android AI
Written by Zahidur Rahman Faisal

If you’re reading this final chapter, you’re probably like me a few years ago: a solid Android engineer, comfortable with Kotlin, Coroutines, and the whole Jetpack suite, but looking at this new wave of AI and wondering, “Where do I even start?” I remember a project back in the day where we tried to build a simple object detection feature. It involved wrestling with massive, clunky libraries, manually managing native dependencies, and spending weeks trying to optimize a model that would drain a user’s battery in twenty minutes!

Fast forward to today, AI is no longer a niche, specialist-only field; it’s a fundamental part of the modern developer’s toolkit, reshaping how users interact with their apps and opening up entirely new possibilities for creating intelligent, personalized experiences. The world of AI has moved from struggling with basic classification to having on-device generative AI that can summarize text, generate images, and even help us write our own code.

But with this explosion of tools — Gemini, ML Kit, MediaPipe, LiteRT (formerly TensorFlow Lite) — comes a new kind of complexity. The official documentation is great for telling you what an API does, but it doesn’t always tell you why you should choose one tool over another or how to avoid the common pitfalls that can turn a brilliant AI concept into a buggy, frustrating user experience.

That’s the goal of this book — this isn’t just a rehash of the docs. These are the lessons I wish I’d had when I was starting out. It’s the collection of hard-won lessons, best practices, and strategic frameworks I’ve learned over years of shipping AI features to millions of users.

This chapter covers the three crucial stages of building with AI on Android:

  1. The Big Decision: Start with the single most important architectural question you’ll face: Should your AI run on the user’s device or in the cloud? This choice impacts everything that follows.

  2. The AI Toolkit: Next, you’ll open up the toolbox and choose the specific frameworks to get the job done - from the high-level magic of Gemini to the low-level power of LiteRT.

  3. Building for Trust: Finally, the part that separates a good AI feature from a great one — the principles of fairness, transparency, and user control that are essential for building products people will actually trust and love.

The Big Decision: Where Does the “Thinking” Happen?

Before you write a single line of AI-specific code, before you even think about which model to use, you have to answer one fundamental architectural question:

“Where will the AI model perform its inference?”

Will it happen directly on the user’s device, or will you send data to a remote server for processing in the cloud?

This isn’t a minor implementation detail. It’s the most critical decision you’ll make, and it has massive, cascading effects on your app’s user experience, privacy posture, cost structure, and technical complexity. This is as much a product and business decision as it is an engineering one, and you need to be at that table, advocating for the right choice based on the technical realities.

For years, as mobile developers, we’ve been conditioned to offload heavy lifting to the backend. Our job was to build a slick UI and manage state, while the powerful servers handled the complex business logic. The rise of powerful on-device AI turns that model on its head. It represents a genuine paradigm shift for us. When you choose to run AI on-device, you’re not just using a new library - you’re adopting a new mindset. Suddenly, you have to think like an embedded-systems engineer again.

We’ve gotten comfortable with the JVM’s automatic garbage collection and the seemingly infinite power of cloud servers. On-device AI forces us back to first principles. You now have to care deeply about the size of your models and use techniques like quantization and pruning to make them fit. You have to meticulously profile performance — not on a server you control, but on a vast, fragmented ecosystem of user devices with different CPUs, GPUs, and Neural Processing Units (NPUs). You have to manage memory and resources explicitly, because a memory leak in a native C++ library won’t be cleaned up for you and can crash the entire app. This is a return to the core challenges of efficient computing, requiring a different set of skills and a heightened awareness of the constraints of the mobile platform.

Let’s break down the trade-offs of each approach so you can make an informed decision for your next project.

On-Device AI: The Pros and Cons of Local Intelligence

Running AI models directly on the user’s phone is the direction the industry is heading for a wide range of use cases — and for good reason. ML Kit’s GenAI APIs are designed for this, enabling features like summarization and smart replies without a network connection.

The Wins

  • Privacy and Security: When the model runs locally, the user’s data — whether photos, messages, or voice recordings — never leaves their device. This is a game-changer for apps in sensitive domains like healthcare, finance, or those aimed at children. Even for general-purpose apps, it’s a massive trust-builder. You’re not just telling users you respect their privacy; you’re proving it through your architecture.

  • Latency & Responsiveness: It’s instantaneous. There is no network round-trip. No waiting for a request to travel to a server, get processed, and come back. This ultra-low latency is what makes features like real-time AR filters, live translation, or motion detection feel snappy and magical. The AI becomes a seamless part of the user experience, not a feature you have to wait for.

  • Offline Functionality: The user could be on a plane, in a subway tunnel, or hiking in a remote area with no signal. Your AI feature will still function perfectly. This reliability is a huge UX win and can be a key differentiator for apps like travel tools or offline translators.

  • Cost: No scaling cloud bills. Every API call to a cloud-based AI service costs money. With on-device inference, there are no per-use charges. This can be a massive financial advantage, especially for free apps with large user bases or startups with thin margins. You can scale to millions of users without a proportional increase in your cloud expenses.

The Trade-offs You Accept

  • Hardware Constraints & Model Size: A smartphone, no matter how powerful, is not a cloud data center. You’re fundamentally limited by the device’s processor (CPU, GPU, NPU) and available RAM. You simply cannot run the massive, state-of-the-art models available in the cloud. This means you must invest significant effort in model optimization, using techniques like quantization to reduce precision and pruning to remove unnecessary parameters before deployment.

  • Battery Drain: Complex computations consume power. An inefficiently implemented on-device model can be a major battery hog — a cardinal sin in mobile development. You need to carefully profile your model’s energy consumption across a range of devices and optimize accordingly.

  • Update Complexity: When your model is bundled inside your app, updating it usually means shipping a full app update through the Play Store. This is a much slower iteration cycle than simply deploying a new endpoint to your server. While new services like Play for On-device AI aim to solve this by allowing dynamic delivery of models, it’s still a more complex workflow to manage than a typical backend deployment.

  • Development Complexity: You’re now responsible for ensuring your AI feature performs well across the incredibly fragmented Android ecosystem. A model that runs great on a flagship Pixel with a Tensor chip might crawl on a mid-range device with a different chipset. This requires extensive testing and, potentially, creating different versions of your model for different hardware profiles.

Cloud AI: When You Need the Heavy Artillery

Despite the powerful trend toward on-device processing, the cloud still has a critical role to play, especially when you need raw, unadulterated power.

Why You’d Choose It

  • Power & Scalability: Access to the biggest and best models. When your use case demands deep reasoning, the highest-quality image generation, or the analysis of massive datasets, the cloud is your best option. You get access to advanced models like Gemini Pro or Imagen 4, which are orders of magnitude more powerful than anything that can run on a phone.

  • Ease of Updates: Iterate at the speed of the web. You can tweak, update, or completely swap out your AI model on the backend at any time, and the changes are live for all users instantly. You don’t need to wait for app reviews or user updates. This allows for incredibly rapid experimentation and improvement.

  • Centralized Data Processing & Learning: If your feature relies on learning from the collective behavior of all your users — think of a recommendation engine like Netflix’s or a fraud-detection system — you need a central place to process that data. The cloud is purpose-built for this kind of large-scale aggregation and model training.

The Trade-offs

  • Latency: The network is your bottleneck. Even on a fast 5G connection, there will always be a noticeable delay as data travels to the server and back. For any feature that requires a real-time feel, this latency can be a deal-breaker.

  • Connectivity Dependence: No internet, no feature. This is the most obvious drawback. If your user is offline, your AI feature stops working — making it unsuitable for functionality that’s core to your app’s experience.

  • Privacy Concerns: You’re handling user data. The moment you send data to your server, you become a custodian of that user’s information. You must be extremely transparent about this in your privacy policy and implement robust security measures. For many users, this is a significant barrier to trust.

  • Cost: Pay-as-you-go can get expensive. You’re typically charged per API call or per unit of compute time. Manageable at a small scale, yes — but costs can quickly spiral as your user base grows. Monitor this carefully and ensure your business model supports it.

The Pragmatic Engineer’s Choice: The Hybrid Approach

After looking at these pros and cons, you might realize that for many sophisticated applications, the answer isn’t a strict “either/or.” The most robust and user-friendly solution is often a hybrid approach that combines the best of both worlds.  

This is the strategy I recommend for most complex projects. You use on-device AI as your “first line of defense.” It handles the tasks that need to be fast, private, and always available. This could be things like keyword spotting to activate a feature, pre-processing images to detect faces before uploading, or providing quick, simple text summaries.

Then, when the user needs more power, or the on-device model can’t handle the request, your app can gracefully fall back to a more powerful cloud-based model.

This hybrid model gives the user the best possible experience: the speed and privacy of on-device AI for everyday tasks, and the raw power of cloud AI for the heavy-duty features, all managed seamlessly within a single application.

To help you navigate this crucial decision, here’s a table that summarizes the key factors. Keep this handy as you plan your next AI feature:

Cloud AI On-device AI Factor Choose for any core functionality that must remain reliable regardless of connectivity. On-Device Fully functional without an internet connection Offline Use Choose when handling any sensitive user data (health, finance, private messages, or photos). On-Device High (data never leaves the device) Privacy Choose if you have a large user base and a business model that can't support scaling API costs. On-Device No per-inference cost; one-time development cost Cost Model Choose for tasks requiring deep reasoning, high-quality generation, or complex analysis. Cloud Limited by device hardware (smaller models) Model Complexity or Power Choose if your app is already resource-intensive or targets lower-end devices. Cloud Higher (consumes battery, storage, and RAM) Device Impact Choose when you need to iterate and improve your model frequently and rapidly. Cloud Slower (requires an app update or model delivery service) Update Agility Requires a stable internet connection Lower (data sent to servers) Usage-based (per API call or compute time) Virtually unlimited (access to state-of-the-art models) Lower (minimal impact on device resources) Instant (update the model on the server) When to choose it

Android AI Toolkit

Alright, you’ve made the big architectural decision about where the AI will run. Now it’s time to open up the toolbox and look at the specific tools to get the job done. The Android AI ecosystem is rich and varied, but it can also be confusing. The key is the “right tool for the job” philosophy. Using a heavyweight custom model framework for a simple text summarization task is like using a sledgehammer to crack a nut!

This table breaks down the main tools in the Android AI stack and what they’re best used for. Refer back to this as you’re implementing your AI feature:

Customization Level Primary Use Case Tool/API On-Device Adding common on-device generative AI features (e.g., summarization, translation) ML Kit GenAI APIs N/A AI-powered coding assistant for developers Gemini in Android Studio Cloud Accessing powerful, cloud-based generative AI models Firebase AI On-Device High-performance on-device vision, audio, and text tasks MediaPipe Solutions On-Device Running your own custom-trained TensorFlow Lite models Custom Models with LiteRT Low (Fine-tuned for specific tasks) N/A High (Full prompting capabilities) Medium (Customizable tasks and models) Very High (Full control over the model and runtime) On-Device/ Cloud Very Easy Very Easy Easy Medium Hard Ease of Use

AI-powered Programming: Gemini in Android Studio

Android Studio is the tool that will help you build everything else. Gemini in Android Studio is your AI-powered pair programmer. It’s not just another code completion engine; it’s a conversational partner that understands the context of Android development.

Gemini in Android Studio truly shines in its deep integration with the Android ecosystem. This is its superpower compared to a generic tool like ChatGPT. It can help you:

  • Fix Cryptic Gradle Errors: Just paste that wall of red text from your build output and ask what’s wrong. It has been trained on countless Android build issues and can often spot a misplaced comma or version mismatch you’ve been stuck on for hours.  

  • Analyze Crash Reports: It integrates with Logcat and App Quality Insights. You can literally ask, “Why is my app crashing with this stack trace?” and it will analyze the report and suggest potential causes and fixes.  

  • Mock Up and Troubleshoot Compose UIs: You can describe a UI in plain English, and it will generate the Compose code for you. It’s also great for debugging layout issues.

To get the most out of it, you need to learn how to “speak its language.” Don’t treat it like a search engine.

Mastering Prompts: Getting What You Want Done

Whether you’re using Gemini in Android Studio or calling the API from your app, the quality of your output is directly proportional to the quality of your input, or “prompt.” Prompt design is a skill, but it’s one you can learn.

Here are some practical tips for writing effective prompts:

Be Hyper-Specific with Your Prompts

This is the golden rule. A vague question gets a vague answer. Instead of asking, “How do I use the camera?” ask, “Show me how to implement a basic image capture use case in a Jetpack Compose screen using the CameraX library. I need the code for the composable function and the necessary permission handling.” The more context you provide, the better your results will be.

Define the Structure and the Output

Don’t just throw a long block of query or prompt at the model; use clear, specific instructions. Add context that the model needs to solve the problem effectively. Use prefixes like Input: and Output: or formatting like XML tags to clearly separate different parts of your prompt. This helps the model understand the task and the desired format.

Want your code generated in a specific way? Tell it. For example: “Refactor this function to use Kotlin coroutines. Make sure the network call happens on the IO dispatcher and the result is updated on the main thread. Add KDoc comments explaining each parameter.” This level of instruction yields far better results than just saying “make this asynchronous”.

Break Down Complex Problems

Don’t try to solve a complex, multi-step problem in a single prompt. Break the problem down into a sequence of simpler tasks. Make the output of the first prompt the input for the second, and so on.

For example, if you need to build a complex feature, don’t ask for the whole thing at once. Ask to create the data model first, then the repository, then the ViewModel, and finally the UI components to display the data.

By breaking the request into a series of simpler, logical steps, you guide the model and get a more comprehensive and accurate result overall.

Building AI That People Actually Trust

Now you know the architecture and the tools, you can build a technically functional AI feature, but the job isn’t done. Technical implementation is only half the battle. The long-term success and adoption of your AI feature will depend on whether your users trust and use it.

This section isn’t about optional, “nice-to-have” features. This is about our core responsibilities as engineers when we build systems that make decisions, generate content, and interact with users in increasingly human-like ways.

When you read corporate blog posts about “Responsible AI,” it’s easy to dismiss them as vague, high-level marketing talk. But when you dig into the details, you realize that these principles translate into concrete, actionable engineering tasks. Responsible AI is not an abstract ethical guideline; it is a multi-layered engineering discipline that needs to be designed and implemented with the same rigor as security or performance testing.

This discipline spans the entire stack. It starts with the data layer, where you must be conscious of mitigating the biases in the data used to train your models. It extends to the application layer, where you are responsible for implementing secure data handling, writing clear privacy policies. It manifests in the UI/UX layer, where your job is to make the AI’s work visible, explain its decisions in simple terms, and give users meaningful control. And it even connects to the platform layer, where you must respect the global settings users have for managing AI features and permissions across their devices.

Designing for Fairness: How to Avoid Building Biased Bots

First, let’s define “fairness” in a practical way that we, as engineers, can work with. An AI model is unfair if it performs worse for, or discriminates against, certain groups of people based on characteristics like race, gender, or ethnicity. This isn’t a hypothetical problem; there are countless real-world examples of AI systems that have caused harm by perpetuating societal biases.

  1. It Starts with the Data: The primary source of bias in AI is the data it was trained on. If a model was trained on a dataset where most of the pictures of doctors were men, it might be less likely to correctly identify a woman in a photo as a “doctor”. While we as app developers don’t always train the models we use (especially when using pre-trained models from external sources), we still have a responsibility to be aware of this potential and test for it in the context of our app.

  2. Proactive Testing and Monitoring: You cannot assume a model is fair. You must verify it. This means actively testing your AI features with a diverse range of inputs and user groups. Does your image recognition feature work as well for people with darker skin tones? Does your voice transcription handle different accents equally well? You need to build a framework for continuously monitoring the performance of your model across different user segments and develop a plan to address any disparities you find.

Putting Users in Control: The Non-Negotiable Settings

Giving users clear, accessible controls is a fundamental requirement for building an ethical and trustworthy application. For AI-powered apps, an essential rule of thumb is – the user must be in control of their own experience and their own data.

These are the essential controls you should build into any app with AI features:

  • A Clear Opt-Out: In your app’s settings, there should be a simple, easy-to-find toggle switch for each major AI feature, as well as optionally a master switch to disable all of them. The user should never feel like they are being forced to use an AI feature they don’t want.

  • Granular and Contextual Permissions: Follow the platform’s best practices for permissions. Don’t ask for access to the camera, microphone, or location when the user first launches the app. Instead, request each permission contextually, at the moment the feature needs it, and provide a clear explanation of why you need it. For example, when the user taps the voice input button, that’s the time to request microphone permission with a dialog that says, “Allow this app to access your microphone to enable voice commands”.

  • Data Management and Deletion: If your AI uses the user’s data to provide a personalized experience, you should give them a way to view and manage that data. A “data dashboard” where a user can see what information the app has collected and easily delete their history is a powerful tool for building transparency and control.

  • Respect System-Level Settings: Remember that users can disable AI features at the operating system level (for example, by turning off Google Assistant or revoking your app’s permissions in the system settings). Your app needs to detect these system-level changes and react gracefully, disabling the relevant features without crashing or showing a broken UI.

Conclusion

If you’ve made it to the end of this chapter, then you already understand something many developers never quite grasp: building AI features on Android isn’t just about gluing a model onto an app. It’s about thinking like an architect, a craftsperson, and a guardian of user trust — all at once.

By now, you’ve learned that the very first question you face isn’t “Which model should I use?” but “Where should the thinking happen?” That single decision: on-device, cloud, or a hybrid of both, shapes everything that follows. Pick the right path and the pieces fall into place. Pick the wrong one and you’ll be wrestling your own architecture for months.

You also explored the expanding toolbox Google has placed in your hands: Gemini in Android Studio, ML Kit’s on-device generative APIs, MediaPipe’s high-performance pipelines, Firebase AI for cloud power, and the raw, low-level control of custom LiteRT models. The real skill is knowing when to use what to get things done.

But perhaps the biggest shift is the rise of prompting as a core engineering skill. Whether you’re guiding Gemini in Android Studio to churn out clean Compose code or structuring precise few-shot prompts for a cloud model, you’re no longer just writing code… you’re steering intelligence. And like any craft, the more intentional your inputs, the more reliable the outputs.

Still, none of the technical mastery matters if your users can’t trust what you build. The future of Android AI won’t be written by the developers who build the flashiest demos — it’ll be written by the ones who build responsibly. Responsible AI is no longer optional, it’s engineering.

You’re stepping into an ecosystem that is evolving at breakneck speed. Models will change. APIs will come and go. But the principles you’ve learned here — architectural clarity, tool literacy, careful prompting, and ethical responsibility - will stay relevant, no matter how powerful the next models become.

So if there’s one message to carry with you from this chapter, it’s this:

AI isn’t replacing Android developers. It’s amplifying the ones who adapt.

Thank you for reading till the very end… I’m wishing you an exciting journey into the world of AI & Android ahead. Your journey as an AI engineer on Android is just beginning, and I, for one, can’t wait to see what you build!

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2026 Kodeco Inc.