Building "blocks" of Android code means turning a large app into small, self-contained pieces that you can develop, test, and reuse independently. The reliable way to do that is to decide what a block represents first—a UI screen, a data source, a feature, or a build module—then define a clear boundary, expose only the API the rest of the app needs, and wire the pieces together through dependency injection or an event mechanism. Android supports this at two levels: Gradle modules and Kotlin/Java packages and classes. Most teams need both. The steps below cover the app-level approach; a later section addresses the separate case of platform-style Android.bp modules, which belong to AOSP and system image builds rather than ordinary app development.
Clarify what "block" means in your project
Android documentation has historically described an application as a federation of loosely coupled components rather than one cohesive unit. That framing is useful here: a code block should be something that can be understood, replaced, or tested without reading the rest of the app.
In day-to-day app work, "block" usually refers to one of four things:
A Gradle module. A separate directory with its own build.gradle file, such as :feature:checkout or :core:network. This is the strongest boundary because the compiler and build system enforce it. A package plus public API. A set of classes inside one module, where internal classes are marked internal in Kotlin so only the intended surface is visible. A component. An Activity, Fragment, Service, BroadcastReceiver, or ContentProvider registered in the manifest. These are Android's native building blocks and have lifecycle rules of their own. A reusable UI element. A composable function or custom View that renders one thing and takes its data as parameters.
The practical rule: use Gradle modules for boundaries you want enforced, packages for boundaries inside a module, and components for things the operating system must launch. If you only separate files into folders, nothing stops one block from reaching into another's internals.
Set up the prerequisites before splitting anything
Before you create your first module, get the project into a state where splitting is safe.
1: Put the project under version control and make sure the app builds and tests pass on the current commit. You need a known-good baseline to compare against. 2: Confirm your toolchain versions. Check the Android Gradle Plugin and Gradle versions in your project files and keep them consistent across all modules. Version catalogs (libs.versions.toml) are the modern way to share dependency versions so modules don't drift. 3: Confirm the minimum SDK. If you're adding the Blockly visual-programming library, its Android guide states a minSdkVersion of 18 or higher; ordinary modularization has no such floor beyond what your app already targets. 4: Map the dependencies inside your app. List which screens use which data sources and utilities. You're looking for clusters—groups of classes that mostly talk to each other and rarely talk to the rest.
A simple check: open your main source folder and note every package. If a package has more than roughly a dozen classes and is used from many places, it's a candidate for extraction.
Decide the boundary and define its contract
A block is only as good as its contract. Before moving code, write down three things:
What goes in. The inputs the block needs—data models, configuration, callbacks. What comes out. The results, state, or events the block produces. What it must not expose. Internal helpers, database schemas, HTTP client details, and anything else callers shouldn't depend on.
For example, a :feature:profile block might expose a single ProfileScreen() composable and a ProfileViewModel factory. It hides its repository, its JSON parsing, and its layout details. When the profile screen changes internally, no other module needs to be touched.
Prefer interfaces at the boundary over concrete classes. If :feature:checkout depends on an interface PaymentProcessor rather than a specific implementation, you can swap in a fake for tests and a real gateway in production without changing the feature code.
Create Gradle modules in a workable order
Once the contract is clear, extract in dependency order—lowest-level blocks first.
1: Create the module directory and Gradle file. In Android Studio, use File > New > New Module and choose the template that matches the block (Android Library for UI plus Android APIs, Kotlin/JVM Library for pure logic). Or create the folder manually with a build.gradle.kts and add it to settings.gradle.kts with an include(":core:model") line. 2: Start with pure-Kotlin blocks. Data models, formatting utilities, and business rules that don't touch Android APIs go into com.android.library or plain java-library/kotlin modules. These build fastest and are the easiest to test. 3: Then extract data and network blocks. A :core:network module holding your HTTP client and a :core:database module holding Room entities and DAOs. Keep the client configuration in one place so every feature uses the same timeouts and interceptors. 4: Then extract feature blocks. Each feature module depends on the core modules it needs and exposes its entry point. Feature modules generally should not depend on each other; if two need shared behavior, move it down into a core module. 5: Keep the app module thin. :app should assemble the pieces: it owns the Application class, the main Activity, navigation wiring, and dependency injection setup. It depends on features; features don't depend on it.
Success check: after each extraction, run a full build and your test suite. If the build fails only inside the new module, the boundary is probably wrong—the code you moved still reaches back into the app module's classes. Failure symptom: circular dependencies between modules. Gradle will refuse to build them, which is the point; fix it by moving the shared piece down a level rather than by merging the modules back.
Make the block self-contained with dependency injection
A block that constructs its own dependencies is hard to test and hard to reuse. Two common approaches:
Manual injection. The block exposes a factory or constructor that takes its dependencies as parameters. Simple and transparent, and fine for small apps. A DI framework. Hilt or Koin manage the graph. Each module declares what it provides and what it needs; the app module assembles the graph.
Whichever you choose, the rule is the same: the block receives its collaborators rather than reaching for global singletons. A ProfileRepository should be passed into the view model, not fetched from a static holder inside the module. That's what makes a fake repository possible in a unit test.
Also keep Android framework entry points at the edge of the block. A view model shouldn't hold a Context; a repository shouldn't know about Activity. When platform types stay at the boundary, the core of the block stays testable on the JVM without an emulator.
Separate UI blocks from logic blocks
UI and logic age at different rates and have different testing needs, so they deserve separate blocks.
UI block: composables or views, view models, navigation within the feature. It depends on the design system module and the logic modules it presents. Logic block: use cases, repositories, validation, formatting. It has no Android UI dependencies and can run in fast JVM tests. Design system block: shared colors, typography, and reusable components. Every feature depends on it; it depends on nothing feature-specific.
This split pays off in testing. A pricing calculation can be verified with dozens of unit tests in seconds, while the screen that displays it needs only a handful of UI tests. If your logic block needs Robolectric or an emulator to test, some Android dependency has leaked in—look for Context, Log, or Uri usage and abstract it behind an interface.
Test each block at its own boundary
A block isn't finished until you can verify it in isolation.
1: Unit-test the contract. For each public function or view model, test the inputs and outputs you documented. Use fakes for the interfaces the block depends on. 2: Add a module-level test source set. Each Gradle module gets its own src/test and, if needed, src/androidTest directories. Tests live with the block, not in a central pile. 3: Check the dependency direction. A quick audit: does any core module import a feature module? It shouldn't. Does a feature module import another feature's internals rather than its public API? That's a leak waiting to become a build break. 4: Run the whole suite after wiring. The app module's integration tests are where blocks meet; they catch mismatches in contracts that unit tests can't see.
Stop condition: if you find yourself adding implementation project(":feature:other") to one feature so it can reuse a screen, that's a signal the shared piece belongs in a core or design-system module. Pushing forward with cross-feature dependencies usually produces a tangled graph that's harder to untangle later.
Understand the different "blocks" in AOSP builds
If your work is on the Android platform itself rather than an app, "blocks of code" may mean Soong modules. AOSP replaced the old make-based system with Soong, which uses Android.bp files—declarative, JSON-like descriptions of modules. By design these files contain no conditionals or control flow; complexity lives in Go build logic instead. A module starts with a module type and a set of name: "value" properties, and every module needs a unique name.
This is a different discipline from app modularization: you're describing build targets for the platform, and the valid module types come from the Soong modules reference generated by m soong_docs. Don't mix the two mental models—an Android.bp file has nothing to do with your app's Gradle modules.
Add custom visual-programming blocks when that's the goal
If "blocks of Android code" means draggable blocks in a Blockly-powered editor inside an Android app, the structure is different again. According to Google's Blockly for Android guidance, a block has three components: a block definition object (text, color, fields, connections), a toolbox reference so users can drag it onto the workspace, and a generator function that turns the block into code. The generator is always written in JavaScript even when the target language isn't JavaScript. Blockly for Android supports only JSON-formatted block definitions, and building definitions programmatically in JavaScript—supported on the web—is unsupported on Android. Use the Blockly Developer Tools with the language set to JSON to produce definitions, then hook up the toolbox entry and generator.
Check: drop the new block on the workspace, connect it where it belongs, and confirm the generated code compiles or runs as expected. If the block appears but produces no code, the generator function is usually missing or not registered.
Avoid the common failure modes
Extracting too early. Splitting a two-screen app into ten modules adds build configuration without adding clarity. Extract when a boundary is stable and reused. Extracting too late. A 200-class app module resists splitting because everything touches everything. Start with pure logic and work outward. Leaky boundaries. Passing a database entity straight through a block's public API couples callers to your schema. Map to a boundary model instead. Over-shared "common" modules. A :core:utils module that everything depends on becomes a dumping ground. Split it by concern—:core:time, :core:strings—so dependencies stay meaningful. Ignoring build time. Many small modules can slow builds if configured poorly. Keep pure-Kotlin modules lean and avoid unnecessary annotation processors in leaf modules.
A worked example of the whole flow
Suppose an app has a checkout screen that calculates tax, calls a payment API, and shows a confirmation. You decide on three blocks plus the app shell.
:core:money — pure Kotlin: currency formatting and rounding rules. No Android imports. Unit tests run on the JVM. :feature:checkout — view models, UI, and the CheckoutScreen() entry point. Depends on :core:money and on a PaymentProcessor interface it defines. :data:payments — implements PaymentProcessor using your HTTP client. Depends on :core:network. :app — wires :feature:checkout to :data:payments through DI and hosts navigation.
The checkout feature can now be developed and tested against a fake PaymentProcessor before the real gateway integration exists. If the payment provider changes, only :data:payments changes. If the tax rules change, only :core:money changes and its fast tests confirm the fix. That's the payoff of building in blocks: a change lands in one place, and the compiler tells you if a contract broke.
FAQ
Do I need a separate Gradle module for every screen?
No. Group screens that share data and navigation into one feature module, and split only when a boundary is stable and reused. Modules cost build configuration and maintenance, so create them where the enforced boundary earns its keep.
Can two feature modules depend on each other?
Technically Gradle may allow it in some configurations, but you shouldn't. Mutual dependencies mean the boundary is wrong; move the shared code into a core module both can depend on.
How do I know a block is truly independent?
It builds and tests on its own, its unit tests need no emulator, and no file inside it imports from a module that sits above it in the dependency graph. If you can delete the block and the rest of the project still compiles (minus the wiring), the boundary holds.
Is modularizing worth it for a small app?
Often not at first. Start with clear packages and internal visibility, and promote a package to a module when it stabilizes and gets reused. The contract discipline matters more than the module count.