Courses / Mobile Application Development (CDE-315) – BS CS 5th Semester
Week 16 Lab 16 – Generate an APK File and Install It on an Android Device or Emulator
Week 16 – Mobility, Wireless Communication and Location-Aware Applications · 120 min read · 11 MCQs to check yourself
How to generate an APK in Android Studio
Generate a signed APK or App Bundle (AAB) step by step
Objective
Turn the CampusMate app you built in Labs 1–15 into an installable APK: build a debug APK, create a keystore and a signed release APK (and an AAB for Google Play), install it on an emulator and a real phone, and inspect it with the APK Analyzer. CLOs: CLO-2 (build and publish apps), CLO-3 (compare debug vs release, APK vs AAB, size), CLO-4 (use Android Studio build tools, adb, apksigner).
What you will produce
| File | Use |
|---|---|
app-debug.apk |
Testing, sharing with classmates |
app-release.apk (signed with your key) |
Installing on any phone |
app-release.aab |
Uploading to Google Play |
Workflow
| Step | What you do | Where |
|---|---|---|
| 1 | Set the version, app name and icon | build.gradle.kts, strings.xml, Image Asset |
| 2 | Build a debug APK | Build menu |
| 3 | Create a keystore and a signed release APK | Generate Signed App Bundle or APK |
| 4 | Build an AAB | Same dialog → Android App Bundle |
| 5 | Install the APK (drag-and-drop, adb, phone) | Emulator / phone |
| 6 | Inspect the APK and fill the report table | APK Analyzer |
Step 1 – Set the version
In app/build.gradle.kts:
// Gradle script (Kotlin DSL) – not app code
defaultConfig {
applicationId = "com.example.campusmate" // unique, never change after publishing
versionCode = 1
versionName = "1.0"
}
Also set a proper app name (strings.xml → app_name) and icon (right-click res → New → Image Asset → Launcher Icons (Adaptive and Legacy)).
Step 2 – Build a debug APK
Build → Build App Bundle(s) / APK(s) → Build APK(s). When the build finishes, click locate in the notification.
Output: app/build/outputs/apk/debug/app-debug.apk
- Signed automatically with the debug key (
~/.android/debug.keystore). - Fine for testing and for sharing with classmates; not accepted by Google Play.
Command-line alternative (Terminal tab in Android Studio):
./gradlew assembleDebug # macOS/Linux
gradlew assembleDebug # Windows
Step 3 – Create a keystore and a signed release APK
- Build → Generate Signed App Bundle or APK… → choose APK → Next.
- Key store path → Create new…
Field Example / rule Key store path D:\keys\campusmate.jks(outside the project folder)Password strong password – remember it! Alias campuskeyKey password can be the same Validity 25 years or more Certificate Your name, organisation (university), city, country code PK - Next → choose build variant release → Create.
- Output:
app/release/app-release.apk(orapp/build/outputs/apk/release/).
Back up the .jks file and passwords (e.g. in your Google Drive and a USB) – an app can only be updated with the same signing key. Never upload the keystore to GitHub.
Optional – enable shrinking for release (smaller APK):
buildTypes {
release {
isMinifyEnabled = true
isShrinkResources = true
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
Test the release APK after enabling R8 – if something crashes (e.g. Gson models), add keep rules.
Step 4 – Build an Android App Bundle (for Google Play)
Repeat Generate Signed App Bundle or APK → choose Android App Bundle → same keystore → release. Output: app/release/app-release.aab. An AAB cannot be installed directly – Play converts it to optimised APKs. (Advanced: Google's bundletool can create installable APKs from an AAB.)
Step 5 – Install the APK
(a) Emulator – drag and drop
Start an emulator, then drag app-debug.apk onto the emulator window; the app appears in the app drawer.
(b) adb (Android Debug Bridge)
adb is in Android/Sdk/platform-tools. In Android Studio's Terminal:
adb devices # list connected devices/emulators
adb install app/build/outputs/apk/debug/app-debug.apk
adb install -r app-release.apk # -r = reinstall/update keeping data
adb uninstall com.example.campusmate # remove by package name
adb shell pm list packages | findstr campus # Windows (use grep on macOS/Linux)
Common adb errors:
| Error | Meaning / fix |
|---|---|
INSTALL_FAILED_UPDATE_INCOMPATIBLE |
An app with the same package but a different signature is installed (debug vs release) – uninstall it first |
INSTALL_FAILED_VERSION_DOWNGRADE |
Installed versionCode is higher – increase versionCode or uninstall |
INSTALL_FAILED_OLDER_SDK |
Device Android version is below minSdk |
unauthorized in adb devices |
Accept the USB debugging prompt on the phone |
(c) Real phone without a cable
- Send the APK to the phone (WhatsApp as a document, Google Drive, email, USB copy).
- Open the file → Android asks to allow "Install unknown apps" for that source (Settings → Apps → Special app access → Install unknown apps) → allow → Install.
- Google Play Protect may show a warning for apps from unknown developers – choose More details → Install anyway for your own app.
From 2026 Google is rolling out developer verification for apps installed on certified Android devices (first in Brazil, Indonesia, Singapore and Thailand, globally from 2027). Installing your own builds with adb and Android Studio continues to work for development; a free student/hobbyist distribution account for small numbers of devices is part of the programme.
Step 6 – Inspect the APK
Build → Analyze APK… → choose the APK. Note:
- Total and download size.
classes.dex(your code),res/,resources.arsc,AndroidManifest.xml,lib/(native libraries),META-INF/(signature for v1 scheme).- Compare debug vs release (minified) sizes.
Verify the signature from the Terminal:
apksigner verify --print-certs app-release.apk # in Sdk/build-tools/<version>/
Report table (fill in)
| Item | Debug APK | Release APK |
|---|---|---|
| File size | ||
| Signed with | debug key | campuskey |
| Installed on | ||
| Installation method |
Short and long questions
1. (Viva) What is the difference between a debug APK and a release APK?
Model answer
A debug APK is built with the debug build type, is debuggable and is signed automatically with the debug key – used for testing only. A release APK is optimised (optionally minified with R8), not debuggable and signed with the developer's release/upload keystore – used for distribution and accepted by stores.
2. (Viva) List the information you entered when creating the keystore.
Model answer
Keystore path, keystore password, key alias, key password, validity in years, and certificate details (name, organisational unit, organisation, city, state and country code).
3. (Viva) What causes INSTALLFAILEDVERSIONDOWNGRADE and how is it fixed?
Model answer
The APK being installed has a lower versionCode than the version already on the device. Increase versionCode in build.gradle and rebuild, or uninstall the existing app first.
4. (Viva) Describe three ways you installed your APK.
Model answer
(1) Dragging app-debug.apk onto the running emulator. (2) Using adb in the terminal: adb devices then adb install path/to/app.apk (adb install -r to update). (3) Sending the APK to the phone (WhatsApp/Drive/USB), opening it, allowing "Install unknown apps" for that source and tapping Install.
This lesson ends with a quiz of 11 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: Week 16 Theory – Challenges with Mobility and Wireless Communication; Location-Aware Applications · Next: Week 17 Theory – Performance/Power Trade-offs; Mobile Platform Constraints; Emerging Technologies
All lessons in Mobile Application Development (CDE-315) – BS CS 5th Semester