You build an Android APK from source code by using the project's Gradle build system to compile code and resources, then packaging and signing the result. For a normal app repository created in Android Studio, that means opening or importing the project, letting Gradle sync and download dependencies, then running ./gradlew assembleDebug for an installable test APK or creating a signed release APK through Android Studio or the command line. The key condition: you need a compatible JDK, the Android SDK, and a project whose Gradle wrapper and plugin versions match. AOSP platform source is a different, much heavier process covered later.

This guide assumes the common case — an app-level source tree — and walks through prerequisites, debug builds, release signing, output locations, failures, and how to verify the result. Exact menu labels shifted in recent Android Studio releases, so treat interface names as approximate and trust the Gradle task names.

What "source code" means here, and which build path you need

The phrase covers two very different jobs, and mixing them up wastes hours.

The first is an app project: a repository with settings.gradle or settings.gradle.kts, module folders, a gradlew wrapper, and an AndroidManifest.xml inside each module. This is what you get from Android Studio's project templates, from most GitHub Android repositories, and from open-source apps. You build it with Gradle in minutes on a laptop.

The second is the Android platform itself — the AOSP tree, which produces system images rather than a single installable app APK. Google's AOSP documentation describes that flow: you source build/envsetup.sh, pick a target with lunch product_name-release_config-build_variant, and build. That tree is tens of gigabytes, needs a Linux host with specific packages, and yields emulator or device images. It is not what most people mean when they ask how to build an APK from source.

A quick test: if the repository root contains a gradlew file and a module with build.gradle, you're in app-project territory. If it contains envsetup.sh, build/make, and a device/ directory, you're looking at platform source.

Also note the packaging distinction. Gradle can produce an APK or an Android App Bundle (.aab). Bundles are preferred for Play Store uploads because Google Play generates optimized APKs per device; APKs are better for quick testing, sideloading, and sharing a single file. Everything below focuses on APKs, with notes where the bundle path differs.

Prerequisites: JDK, Android SDK, and a matching Gradle wrapper

Get these in place before touching the build button, because most first-build failures trace back to one of them.

A JDK. Modern Android Gradle Plugin versions require a specific JDK major version — JDK 17 has been the common baseline for recent plugin releases, and newer plugin versions may require a higher one. Don't guess: open the project's gradle/libs.versions.toml or the top-level build.gradle and check the Android Gradle Plugin version, then match the JDK to that plugin's documented requirement. Android Studio bundles a JDK, which is the simplest route if you build inside the IDE.

The Android SDK. Install it through Android Studio's SDK Manager, or standalone command-line tools. You need the platform matching the project's compileSdk, plus build tools. The SDK location goes in local.properties as sdk.dir=/path/to/sdk (Android Studio writes this automatically) or in the ANDROID_HOME environment variable.

The Gradle wrapper. Repositories ship gradlew (macOS/Linux shell script) and gradlew.bat (Windows batch file) plus a gradle/wrapper/ folder pinning a Gradle version. Always use the wrapper rather than a system-installed Gradle — the wrapper guarantees the version the project expects. If gradlew lacks execute permission on macOS or Linux, run chmod +x gradlew first.

Network access and disk space. First builds download Gradle itself, the Android Gradle Plugin, and the project's dependency graph. A few gigabytes of free disk and a stable connection prevent a class of confusing mid-build timeouts.

One caution about the wrapper: a repository's pinned Gradle version and its Android Gradle Plugin version must be mutually compatible, and older projects sometimes fail on newer JDKs. If the project is years old, expect to either install the JDK era it targets or update its plugin versions — and updating can cascade into code changes.

Build a debug APK first, before worrying about release

A debug APK is the fastest way to confirm your environment works, and it's signed with an automatically generated debug key so it installs immediately.

1: Open a terminal and change into the project root — the directory containing gradlew. 2: On macOS or Linux, run ./gradlew assembleDebug. On Windows, run gradlew.bat assembleDebug. 3: Watch the output. The first run downloads dependencies, so it can take several minutes; later runs are much faster. 4: On success, Gradle reports BUILD SUCCESSFUL and writes the APK to the module's output directory.

The conventional output path is project-name/module-name/build/outputs/apk/debug/, with the file typically named -debug.apk. Android Studio's documentation confirms APKs land under project-name/module-name/build/outputs/apk/, with debug or release subfolders depending on the variant. Multi-module projects produce one APK per application module; library modules don't generate installable APKs.

How to check success beyond the log line. Verify the file exists and is a valid package by running the SDK's aapt2 dump badging path/to/app-debug.apk (or open it in Android Studio's APK Analyzer). You should see the package name, version code, and launchable activity. Then install it on a device or emulator with adb install path/to/app-debug.apk and confirm it launches. A build that succeeds but produces an APK that crashes on launch usually signals a runtime dependency or manifest problem, not a packaging failure.

In Android Studio, the equivalent is selecting the debug build variant and choosing Build > Generate Bundle(s) / APK(s) > Generate APK(s). The IDE shows a notification with a link to the APK and to the APK Analyzer when it finishes. The menu wording has changed across releases — Google notes that Android Studio Meerkat relabeled and reordered some Build actions — so if your labels differ, look for the generate-APK entry under the Build menu.

Debug APKs are for testing and sharing with collaborators. Don't distribute them publicly: the debug key is shared and insecure, and debug builds may include extra logging.

Produce a signed release APK

A release build is what you'd actually ship, and it must be signed with your own key. Google's documentation is explicit: if you generate an APK from a release variant, it is unsigned by default and you must sign it manually, or use the guided signing flow instead.

Step 1: Create a keystore if you don't have one. Run the JDK's keytool:

keytool -genkeypair -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias

Choose strong passwords and store the keystore and credentials somewhere durable and backed up. Losing this key means you cannot publish updates to the same app listing under the same identity. Never commit the keystore or passwords to version control.

Step 2: Choose a signing route.

Guided flow: Build > Generate Signed Bundle / APK, select APK, point to the keystore, enter the alias and passwords, choose the release variant, and finish. This signs and builds in one pass. Gradle configuration: define a signingConfigs block in the module's build file referencing the keystore, and attach it to the release build type. Credentials should come from environment variables or a local properties file excluded from Git, not hardcoded. Manual signing: build an unsigned release APK, then sign with apksigner from the build-tools directory — apksigner sign --ks my-release-key.jks --out app-release-signed.apk app-release-unsigned.apk — and verify with apksigner verify.

Step 3: Build the release variant. The task is assembleRelease; with a configured signing config, the output is signed. Output lands in build/outputs/apk/release/.

Step 4: Shrink and optimize if appropriate. Release builds commonly enable code shrinking and resource shrinking (R8), which strips unused code and reduces APK size. These settings live in the release build type; if a library relies on reflection, you may need keep rules, or shrinking will break it at runtime — a classic symptom is a release-only crash that never appears in debug.

Step 5: Verify before distributing. Run apksigner verify --print-certs on the final file, install it on a clean device, and test the paths most likely to differ between debug and release: networking, authentication, and anything touching obfuscated classes.

A compact comparison of the two paths:

Criterion Debug APK Release APK
Gradle task assembleDebug assembleRelease
Signing Automatic debug key Your keystore, configured or manual
Shrinking Off Often on (R8)
Typical use Testing, internal sharing Distribution, store upload
Output folder build/outputs/apk/debug/ build/outputs/apk/release/

Building from the command line when there's no Android Studio

Headless environments — CI servers, remote shells, containers — are a normal way to build Android projects, and everything works through the wrapper.

The command-line documentation is direct: you can execute all build tasks available to an Android project using the Gradle wrapper, invoked as gradlew.bat on Windows or gradlew.sh on Linux and Mac from the project root. There is no requirement to open the IDE at all.

A minimal CI-style sequence looks like this: install a JDK matching the plugin, set ANDROID_HOME to an SDK installation (or run the SDK manager to fetch the needed platform and build tools), accept any license prompts, then run ./gradlew assembleRelease or ./gradlew assembleDebug. For a signed release in CI, inject keystore credentials through environment variables and keep the keystore itself in a secret store.

Two practical notes. First, CI machines start cold, so every build re-downloads dependencies unless you cache the Gradle home directory and the SDK — caching typically cuts minutes off each run. Second, ./gradlew clean deletes intermediate build files; Android Studio exposes the same as Clean Project and Clean and Assemble Project with Tests. Reach for clean when outputs look stale or a resource change isn't reflected, not habitually, since it forces a full rebuild.

If you want a bundle instead of an APK, the task is bundleRelease, and the artifact is an .aab. Google's guidance is that bundles are the better packaging for Play Console uploads, while APKs suit quick testing and sharing deployable artifacts — so choose based on destination, not habit.

Common failures and what they actually mean

Most build failures fall into recognizable buckets. Distinguish the symptom from the cause before changing anything, and prefer reversible fixes first.

"Unsupported class file major version" or JDK errors. The JDK is too new or too old for the project's Android Gradle Plugin. Fix: check the plugin version, install the matching JDK, and point JAVA_HOME at it. This is the single most common first-build wall.

"SDK location not found." local.properties is missing or ANDROID_HOME is unset. Fix: create local.properties with sdk.dir= pointing at your SDK, or export the variable.

Dependency resolution failures. Often a repository declaration missing from settings.gradle, a private artifact requiring credentials, or a network/proxy problem. Fix: read the exact artifact coordinate in the error, confirm the repository that hosts it is declared, and check connectivity. Avoid "fixing" this by bumping versions blindly.

"Execution failed for task ':app:processDebugManifest'." Usually a manifest merge conflict — two libraries declaring conflicting attributes. The error names the conflicting entries; resolve with tools:replace or by aligning library versions.

Release-only crashes. Almost always R8 stripping something used reflectively. Fix: add keep rules for the affected classes, or temporarily disable shrinking to confirm the diagnosis before writing rules.

Out-of-memory during the build. Gradle's daemon heap may be too small for a large project. Fix: raise org.gradle.jvmargs in gradle.properties, for example to a larger -Xmx value.

Stale or corrupted incremental state. Symptom: changes don't appear, or tasks fail oddly after an interrupted build. Fix: ./gradlew clean, then rebuild.

Stop condition. If the project's own build files are internally inconsistent — plugin and Gradle versions that no combination supports without source changes — or if failures persist after a clean build with a verified JDK and SDK, stop guessing. Reverting to the project's documented build environment, or asking the project maintainers, beats stacking speculative changes. For proprietary projects, that may mean contacting whoever owns the build configuration.

Verify, install, and understand what you produced

A successful Gradle run is necessary but not sufficient. Verification separates "it compiled" from "it works."

Check the artifact with aapt2 dump badging or the APK Analyzer: package name, version code and name, minimum and target SDK levels, permissions, and the launchable activity. These values should match the project's build configuration — for example, compileSdk, minSdk, targetSdk, versionCode, and versionName as declared in the module's build file.

Install on a device with adb install -r path/to/app.apk, or use bundletool to deploy directly from an app bundle to a connected device, which Google documents as the bundle-testing route. Test on the lowest supported API level if you can; emulators make this cheap.

One signing subtlety worth knowing: the same package name signed with a different key cannot be installed as an update over an existing installation. If a device already has a Play-installed copy, sideloading your build will conflict, and you'll need to uninstall the existing app first — which also wipes its local data. That's expected behavior, not a bug.

Finally, treat version control hygiene as part of the build. Build outputs, local.properties, keystores, and signing passwords shouldn't be committed. A reproducible build — same source, same toolchain, same output — is the goal, and it starts with keeping environment-specific files out of the repository.

Frequently asked questions

Can I build an APK without Android Studio? Yes. The Gradle wrapper handles the entire build, and Google's command-line documentation states all build tasks are available through it. You still need a JDK and the Android SDK installed.

Why is my release APK unsigned? By default, generating an APK from a release variant produces an unsigned file, and Google's documentation says you must sign it manually — or use the signed-bundle/APK flow or a configured signing config. Attach signing in Gradle, sign with apksigner, or use the guided dialog.

What's the difference between the APK and the .aab? The APK is a directly installable package; the bundle is an upload format that Google Play uses to generate per-device APKs. Use the APK for testing and direct sharing; use the bundle for Play Store distribution.

Does building AOSP produce an installable app APK? No. Building the Android platform source produces system images for devices or emulators, using the envsetup.sh and lunch workflow. App APKs come from app-level Gradle projects, not the platform tree.