Creation APK file is a key stage of development Android applications. Without it, it is impossible to test the app on a real device or publish it in Google Play. But how to properly assemble an APK in Android Studioso that it works without errors? This instruction will help you understand the types of assemblies, application signatures and optimization nuances. The first one is created automatically when the project starts, but is not suitable for distribution. The second requires a signature and additional settings. We will analyze both options in detail, and also tell you how to reduce the size of the APK and avoid common mistakes.
Many newbies get confused debug APK (for testing) and release APK (for publication). The first one is created automatically when the project starts, but is not suitable for distribution. The second requires a signature and additional settings. We will analyze both options in detail, and also tell you how to reduce the APK size and avoid common mistakes.
If you are just starting to develop for Android, this guide will become your cheat sheet. Experienced developers will find here up-to-date tips on optimizing the build and working with Gradle. Don't miss the section about Automatic APK signing via CI/CD โit will save hours of working time!
Types of APK files: which one to choose for your task
Q Android Studio You can build three main types of APK: debug (debug), release (release) and universal (universal). Each has its own characteristics and application.
Debug APK is generated automatically when you start the project through Run 'app'. It is signed with a standard key Android Debug Keystore, which is stored in ~/.android/debug.keystore. Such a file cannot be published - it is intended only for testing on an emulator or connected device.
Required for publication in stores release APK. It requires its own signing certificate, which you create once and keep secure. Without the correct signature Google Play Console simply will not accept your application.
The third option is universal APK (universal APK). It combines code and resources for all architectures (armeabi-v7a, arm64-v8a, x86), which increases its size. To save user traffic, it is better to use App Bundle, but more on that later.
- ๐ง Debug APK: for testing, signed automatically, not suitable for distribution
- ๐ฆ Release APK: for publication, requires manual signature, optimized
- ๐ Universal APK: contains all architectures, large size, compatible with all devices
Since 2021 Google Play recommends downloading Android App Bundle (.aab), and not APK. However, the ability to assemble APKs remains an important skill - for example, for testing on devices without access Play Market or for enterprise distribution.
Preparing the project before assembling the APK
Before building the APK, you need to check several key project parameters. This will get rid of signature errors, compatibility problems and excess file weight.
First, update build.gradle (Module: app). Make sure that the correct minSdkVersion, targetSdkVersion and compileSdkVersions are specified. For example, if your application supports Android 8.0 (Oreo)then minSdkVersion should be at least 26.
Second - check the dependencies in build.gradle. Outdated libraries may cause conflicts or increase the size of the APK. Use the command ./gradlew dependencies in the terminal to see the full list of connected modules.
Third - configure proguard-rules.pro. This file is responsible for reducing the code size and protecting against decompilation. Even if you don't use ProGuard, check that there are no rules that break functionality.
Update SDK versions in build.gradle|
Checking dependencies for relevance|
Setting up proguard-rules.pro (if used)|
Clearing the cache (Build โ Clean Project)-->
Don't forget about AndroidManifest.xml. Make sure that all the necessary permissions (<uses-permission>) and the correct ones intent-filter for MainActivityare written there. Errors in the manifest are one of the main causes of crashes after installing an APK.
โ ๏ธ Attention: If you use Firebase, Google Maps API or other services with keys, make sure that the release build contains working keys and not stubs from debug-versions.
Building a debug APK: a quick way for testing
A debug APK is created automatically every time you start the project via Android Studio. But sometimes you need to get the file itself - for example, for installation on several devices or transfer to a tester.
To collect debug APK manually:
- Select from the menu
Build โ Build Bundle(s) / APK(s) โ Build APK - Wait for the build to complete (a notification will appear at the bottom
APK(s) generated successfully) - Click
locatein the notification or find the file along the pathapp/build/outputs/apk/debug/app-debug.apk
The finished file can be immediately installed on the device via adb:
adb install app/build/outputs/apk/debug/app-debug.apk
The debug APK has limitations:
- ๐ซ Does not work on devices with USB debugging disabled
- ๐ซ Has a "DEBUG" watermark in the package name
- ๐ซ Not suitable for publication in stores
If you need an APK for testing on devices without Android Studio, but with debugging capabilities, use the command:
./gradlew assembleDebug
To speed up the build of a debug APK, disable linter checking in build.gradleby adding lintOptions { checkReleaseBuilds false }.
Creation release APK: signing and optimization
The release APK requires a signature with a digital certificate. Without it Google Play and most devices simply will not install the application. The process consists of two stages: generating a key and signing the APK itself.
Step 1. Creating a signature key
Use the command:
keytool -genkey -v -keystore my-release-key.jks -keyalg RSA -keysize 2048 -validity 10000 -alias my-alias
Where:
my-release-key.jksโ key file namemy-aliasโ key aliasvalidity 10000โ validity period (in days)
Step 2. Configuration signatures in Android Studio
- Go to
File โ Project Structure โ Modules โ app โ Signing - Specify the path to the
.jksfile, password and alias - In
build.gradleadd a block:android {signingConfigs {
release {
storeFile file('my-release-key.jks')
storePassword 'yourpassword'
keyAlias 'my-alias'
keyPassword 'yourpassword'
}
}
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
Now you can collect the signed APK via Build โ Generate Signed Bundle / APK โ APK. Select module app, build type release and specify the path to the key.
| Parameter | Value | Explanation |
|---|---|---|
storeFile | Path to .jks | Absolute or relative path |
storePassword | Key password | Minimum 6 characters |
keyAlias | Alias | Specified when generating the key |
minifyEnabled | true/false | Enables code compression |
โ ๏ธ Attention: Keep .jksthe file safe! Losing the key means that you will not be able to update the application in Google Play. It is recommended to make a backup copy and store it separately from the project.
APK optimization: how to reduce file size
A large APK takes longer to load, takes up more space on the device and can alienate users. Here are the main ways to reduce its size:
1. Use ProGuard or R8
These tools remove unused code and optimize bytecode. The Android Studio 3.4+ default is R8, which works faster ProGuard. To enable optimization, add to build.gradle:
buildTypes {release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
2. Remove unnecessary resources
Check the folder res/ for unused images, rows or layouts. To do this, use Lint (Analyze โ Run Inspection by Name โ Unused resources). You can also remove resources for unused languages โโor screen resolutions.
3. Optimize images
- ๐ผ๏ธ Use WebP instead of PNG/JPG (lossless compression)
- ๐จ Reduce resolution to necessary (for example,
xxxhdpiinstead ofxxxxhdpi) - ๐ ๏ธ Use TinyPNG or ImageOptim for additional compression
4. Separate APK by architecture
Universal APK contains code for all processors (armeabi-v7a, arm64-v8a, x86), which increases its size. Instead, you can collect separate APKs for each architecture:
splits {abi {
enable true
reset()
include 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
universalApk false
}
}
What is an App Bundle and why is it better than an APK?
Android App Bundle (.aab) is a new publishing format that automatically generates optimized APKs for each device. Instead of one large file, users receive only the resources and code they need for their specific model. This reduces the download size by 15-30% compared to a generic APK. From 2021 Google Play requires downloading specifically App Bundle for new applications.
Common errors when building APKs and how to fix them
Even experienced developers face problems when building APKs. Here are the most common mistakes and their solutions:
1. Signature error: "Failed to read key from keystore"
Reasons:
- ๐ Incorrect password for the key or alias
- ๐ Incorrect path to
.jksfile - ๐ The key is damaged
Solution: check all parameters in signingConfigs and try to generate a new key.
2. Build error: "Duplicate class found"
Cause: dependency conflict (for example, two libraries include the same version OkHttp).
Solution: add to build.gradle:
configurations {all {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
}
3. APK does not install: "App not installed"
Possible reasons:
- ๐ฑ The device already has a version with the same signature, but different
versionCode - ๐ APK signature does not match the previous version
- ๐ Not enough space on the device
Solution: delete the old version of the application or increase versionCode in build.gradle.
4. Error ProGuard: "Can't find referenced class"
Cause: ProGuard removed the class that is used through reflection.
Solution: add an exclusion rule in proguard-rules.pro:
-keep class com.example.MyClass { *; }
โ ๏ธ Attention: If you use Firebase, Google Play Services or other SDKs, check their documentation for special rules for ProGuardFor example, for Firebase Messaging You need to add:-keep class com.google.firebase.messaging..{;}
Automation of APK building via CI/CD
Manual APK building takes time, especially if you work in a team. Automation via CI/CD (for example, GitHub Actions, GitLab CI or Bitrise) solves this problem.
Example configuration for GitHub Actions (.github/workflows/build.yml):
name: Build Release APKon:
push:
tags:
- 'v*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Build Release APK
run: ./gradlew assembleRelease
- name: Upload APK
uses: actions/upload-artifact@v3
with:
name: app-release.apk
path: app/build/outputs/apk/release/app-release-unsigned.apk
For APK signing in CI/CD use repository secrets (for example, SIGNING_KEY_STORE, SIGNING_KEY_PASSWORD). Example command for signing:
jarsigner -verbose -sigalg SHA256withRSA -digestalg SHA-256-keystore $SIGNING_KEY_STORE
-storepass $SIGNING_STORE_PASSWORD
-keypass $SIGNING_KEY_PASSWORD
app-release-unsigned.apk my-alias
Advantages of automation:
- โฑ๏ธ The build is launched with each commit or tag
- ๐ Signing keys are stored in a secure storage
- ๐ฆ APK is automatically downloaded as an artifact
- ๐ค Can be integrated with testing (for example, Firebase Test Lab)
Build automation via CI/CD saves up to 30% of development time and eliminates human errors when signing APKs.
FAQ: Answers to common questions questions
Is it possible to convert an APK back to source code?
Technically yes, using tools like JADX or Apktool. However, the decompiled code will be difficult to read, and resources (for example, images) can be extracted almost in their original form. To protect the application, use ProGuard and check the API keys on the server side.
How to install an APK on an Android device without Google Play?
There are several ways:
- Via
adb:adb install path/to/app.apk - Transferring the file to the device (via USB, cloud or messenger) and opening through a file manager
- Using alternative stores (for example, APKMirror, Aptoide)
On devices with Android 8.0+ you need to allow installation from unknown sources in the security settings.
What is the difference between an APK and an App Bundle?
APK is a ready-to-install file containing all the code and resources. App Bundle (.aab) is a new format that Google Play uses to generate optimized APKs for each device. Main differences:
| APK | App Bundle |
|---|---|
| One file for all devices | Dynamic APK generation for a specific device |
| Larger download size | Smaller size (saving up to 30%) |
| Suitable for manual installation | Only for downloading to Google Play |
| You need to sign manually | Signature is controlled via Play Console |
How to check APK for viruses before installation?
Use online services like VirusTotal or MetaDefender. You can also scan the file with a local antivirus (for example, Kaspersky, Dr.Web). Please note that some legitimate applications may be falsely identified as malicious due to the use of adware SDKs or code obfuscation.
Can an application be updated if the signing key is lost?
No. Google Play requires that all updates be signed with the same key as the first version. If the key is lost, you will have to publish the application as a new one (with another package name). To avoid this, store backup copies of the key in a safe place (for example, in Google Cloud Storage or encrypted storage).