Getting Started with Android Development
You can complete all the Android-version of the labs in this course on the Android emulator, you don’t need a physical device. Labs 3 and 4 built on Jetpack Compose and require minimum SDK Android 12 (S, API Level 31). If your phone cannot (yet) be upgraded to Android 12, you’d have to test these labs on the emulator, in which case, double check that your development host has enough resources (e.g., disk space) to host the emulator.
Some vendors build more closely to Android hardware spec than others. Some phones are better provisioned than others. Some vendors add their own OS mods on top of Android, layering a new wrapper or changing the expected behavior of Android APIs. Given our limited resources, we have tested the labs only on plain Android APIs, as documented in the Android Developers web site. The last time anybody counted (2015), there were 24,000 different Android devices from 1,300 brands. Even Google doesn’t have the resources to evaluate compatibility on all of them. We know that the labs run on Google’s Pixel 3 or later phones. If you have phones from a different vendor, we cannot guarantee that your phone would be adequately provisioned, or built sufficiently to Google’s spec, to be able to run all of the labs. We can help you debug, but in the end the phone may simply be not up to spec. We simply don’t have the means (funds and time) to try out all available Android devices in the market to check for compatibility. Recall that all Android-version of the labs do run on the Android emulator, so you don’t need a physical device to complete the labs.
We will develop in Kotlin only, not Java. Google has switched to “Kotlin first” since Google I/O 2019 and Jetpack Compose requires Kotlin. For 3rd-party libraries and SDKs, Kotlin can co-exist seamlessly with Java. Android Studio automatically converts Java code pasted into a Kotlin file. You can also invoke Java to Kotlin conversion from Android Studio’s menu:
Code > Convert Java File to Kotlin File.
The course does not support any cross-platform frameworks, including React Native and Flutter. All course projects must be built natively. Exception: if you want to build a 3D game, you’re welcome to use Unity3D. If you are contemplating cross-platform mobile development, please consult our collection of articles on industry experiences with cross-platform solutions.
Developing Android app
- you have a device running Android 12 (S) (API level 31) or later.
To check your Android version, on your phone navigate to
Settings > (scroll all the way down) About phone > Android version.
- you will develop only in Kotlin, not Java.
- you will need to do some XML scripting.
- you are using Android Studio Chipmunk 2021.2.1 Patch 2 or later.
- your entire team should be on the same version of Android Studio and Android API level.
You can use a different IDE (such as VSCode or IntelliJ), though we’ll “support” only Android Studio.
Setting up Android Studio
- Download Android Studio Chipmunk 2021.2.1 Patch 2. Versions earlier than 2020.3.1 (Arctic Fox) did not support Jetpack Compose. Install and launch.
- On the
Welcome to Android Studioscreen, choose
More Actio... > SDK Manager(screenshot) check
Android 12.0 (S)or later SDK and click
Tools > SDK Manageronce passed the Welcome screen.
- Staying in the
SDK Manager, click on the
SDK Toolstab and install
Google Play Licensing Library(screenshot) if not already installed.
Open up Android Studio’s preferences/settings pane, navigate to
Editor > General [drop down] > Auto Import, then in the
Kotlinsection (not the
Javasection) at the bottom of the pane check
Add unambiguous imports on the fly(screenshot). Do not check
Optimize imports on the fly.
On macOS, you can access the preferences pane from the main menu,
Android Studio > Preferences. On Windows, from Android Studio’s Welcome screen, navigate to
Customize > All settings...(screenshot) or access it from the main menu,
File > Settingsonce you’re past the Welcome screen.
With auto import enabled, as you enter code, Android Studio will automatically detect and determine which library you need to import. It will then prompt you to hit the Alt-Enter or Opt-Enter key combination, or sometimes along with Shift key also, to automatically import said library. Go ahead and import the suggested libraries; exception: don’t import any library starting with
kotlinx.android.synthetic.This library has been deprecated and importing it could seriously mess up your UI element bindings.
Sometimes, there are multiple library choices and Android Studio would have you choose. Most of the time the choice will be rather obvious (don’t choose the Audio library if you’re not implementing Audio, or do pick the one with the word
composeif you’re using Jetpack Compose, for example). We will provide a full list of imports as an Appendix to each lab spec, so you can double check when in doubt. Or you could cut and paste all of the imports into your source files before any code. If you choose to do so, be sure that you do NOT check
Optimize imports on the fly, otherwise Android Studio will automatically remove them all for being unused and therefore unnecessary.
About Android Studio
- Depending on the compute power and memory capacity of your development host, Android Studio can be slow doing the following:
- Gradle syncing and project indexing
- Gradle build can be especially SLOW (What is Gradle?)
- Code may sometimes be underlined while correct as it takes time to refactor
- When references are unresolved for no good reason, try, in succession:
Build > Clean Project Build > Rebuild Project File > Sync Project with Gradle Files File > Invalidate Caches/Restart...
- If Android Studio does unexpected things, e.g.,
Log.d()doesn’t print any debug messages, restart and see if malbehavior persists.
For Android Studio to run a little faster:
- Disable all non-essential plugins, we have only the following (in order of appearance) enabled:
- Smali Support
- WebP Support
- Give it as much memory as you can afford:
Android Studio > Preferences > Appearance & Behavior > System Settings > Memory Settingsmax out IDE and Daemon heap sizes
Don’t forget to hit
OKfor your changes to take.
Recommended optional plugins
<summary>WiFi</summary> Whereas Android Studio Bumblebee's Wi-Fi pairing is done by QR code, you need to connect your phone to your development host by USB cable to set up ADB Wi-Fi. Once set up, you can connect to the phone even with your WiFi turned off.
If you’re used to developing on Xcode and would like your Android Studio to have Xcode-like syntax highlighting, you can use an Xcode color scheme, which has Xcode-Dark Theme and Material Theme UI as dependencies. You may want to install the Xcode Keymap plugin also. If you use a Mac as your development host, you may want to install Android Tool for conveniently capturing device screenshots and screen recordings from your Mac.
- A run down of various Android Studio’s
View > Tool Windows:
- Project: the project navigation pane: the left pane.
- Run: if you use
println()in your code, it prints out on the
println()also outputs here, along with a lot of other messages from Android’s subsystems, but if your app crashes, you can view the stack trace here along with the offending lines in your source code.
- Problems: compile time errors
- Logcat: output of
Log.e(). You can specify filter to show only certain messages.
- Build: build error, library linking error.
- Gradle: where you can generate your app’s SHA1.
- For less clutter, enable only
View > Appearance > Navigation Bar.
Kotlin and SDK version
Min SDK Version on the
Default Config tab to
31 (API 31: Android 12 (S)). Choosing a higher or lower API level may cause incompatibilities (due to deprecation in the case of higher API levels). If you’re running Android Studio Chipmunk, set your
Target Sdk Version on the same tab to
32 and the
Compile Sdk Version on the
Properties tab also to
32. If you’re running Android Studio Dolphin, set both
Compile SDKs to
Depending on your version of Android Studio, the screenshots in our documentations may not look exactly the same as what you see on screen. If you are completely lost and cannot make sense of the screenshots, please consult the teaching staff.
Android Studio panes
- Left: Project directory, structure
- Right: text and layout/design editors
- Bottom: build result, debugging output
- You can call up any particular view under
View > Tools Window, which also lists the keyboard shortcuts
- Some useful keyboard shortcuts.
- Disabling all but the
Toolbarin the lower half of the
View > Appearancemenu presents a cleaner Android Studio interface, making more screen real estate available.
- On the left pane, under the gear icon, select “Open Files with Single Click” and “Always Select Opened File” to make Android Studio feels a bit more responsive.
- Mouse over an API will pop up the API’s documentation
- The filenames on the navigation pane change colors according to whether it has been modified, committed, etc.
Running and debugging
To enable USB debugging, on your Android phone tap on
Settings > About Phone > Build Number seven times (and click your heels three times?) to enable the Developer options. Turn
Developer options on and then turn on
Settings > System > Advanced > Developer Options. Turn on both
Use developer options and
USB debugging (and optionally
Connect your device with a USB cable to your laptop. Android Studio should now recognize your device and use it automatically to run your app. Click play to build and run.
The Android Developers site has a page explaining the rest of the
Developer options, though you can safely skip it at this point, except to turn on
You may also find ADB Wi-Fi useful for on-device debugging over WiFi. ADB WiFi requires your phone to be connected by USB to be paired initially. Starting from the Bumblebee (2021.1.1) version, Android Studio also supports WiFi pairing natively, which you activate using a QR code.
On an emulator
- Start the
Tools > AVD Manageron Android Studio’s main menu bar.
- Click the
+ CREATE VIRTUAL DEVICE ...“button” in the lower half of the screen (screenshot).
- Pick a
Phonedevice listed with “Play Store” enabled and click
Be sure to pick a device listed with “Play Store” enabled, otherwise your emulator will not work for labs 2 and 4. At the time of writing, only Pixel 4, 3a, and 3 emulators are listed with Play Store enabled.
by modifying config.ini
You can also enable
Play Storemanually for your device of choice by following the instructions in this stackoverflow article.
- Choose a system image. Choose EXACTLY API Level 31 (Release name: S). Choose a system image listed with “(Google APIs)”. Click on the word
Release Namecolumn and then click
FINISHon the download screen once the download is done. Then click
- In the next screen, set
Hardware - GLES 2.0and click
- Close the
If your Android emulator terminated unexpectedly and you have non-ASCII characters in your username, follow this stackoverflow answer to store your emulator on a path with only ASCII characters [thanks to Zhou Tianer, W’22].
Back in Android Studio, make sure your virtual device is selected in the box next to the green play button (screenshot).
To run your app
- Hit the green play button
- Play button changes icon to reload icon
- To see the log, choose from the main menu
View > Tools Window > Logcat
- Click the stop button (seven positions to the right of the play button) on Android Studio to close the app (not the emulator)
- Instead of the play button, hit the bug button
- To view the debug output, choose from the main menu
View > Tools Window > Debug(this menu item shows up only when you’re debugging a process)
Running the labs on the Android emulator
If you have problem running the emulator, check out Troubleshooting and known issues for Android Emulator and system images.
There is no special set up necessary to run the basic
Chatter lab (lab0) and the Images lab. The virtual camera on the emulator captures a stock scene for both photos and videos.
Looks like videos captured on the emulator can only be played back on Android. In particular, we cannot play them back on iOS devices nor on QuickTime Player (incompatibility with the narrowband H.264 codec used?).
To use the emulator with the other labs, you need to access its
Extended controls: click on the
... at the bottom of the right menu bar (screenshot).
To use the emulator with the Maps lab, you need to set two of the extended controls (click on the
... at the bottom of the right menu bar (screenshot)):
Location: to set the virtual location where you want your emulated device to be located:
- click on the Location menu item,
- search for your intended location,
- for ease of future re-location, you can click on
SAVE POINT, give the location an easy to remember name, and click
- click on
SET LOCATION(screenshot with 5 saved points).
Repeat the above process to switch to a different location (which should be picked up by the Maps lab).
Virtual sensors: to change the virtual facing of the emulated device, change its x-, y-, and z-rotations. The compass direction would likely not be accurate, but it does change as you rotate the emulated device around and this should be picked up by the Maps lab.
To record audio on the Android emulator, click the
Microphone item on the extended controls menu and enable
Virtual microphone uses host audio input screenshot.
Due to privacy concern, this option must be set every time you restart the emulator.
On macOS BigSur 11.3, both recording and playing back audio on the Android emulator work fine. If you’re having problem, rebooting the emulator (hold down on the power button on the emulator) may fix it.
On other OS/platforms, the quality of the recorded audio will depend on the quality of your computer’s mic. Playing back audio also would depend on the quality of your audio and speaker systems. Your mileage may vary.
Also double check that the virtual microphone is turned on and volume on the emulator is cranked up.
To use the emulator with the Signin/Biometric lab, click the
Fingerprint item on the extended controls menu.
- You first need to register your virtual fingerprint with the emulated device as you would your real fingerprint on a physical device
- With the
Fingerprintextended control window open, on the emulator’s Home Screen, scroll up to reveal the
Launcherdrawer and open the
- Search for
Securityand open it (screenshot).
- Then click on
Pixel Imprintwhich should be in the second,
DEVICE SECURITYsection of the
- On the next screen, choose to set up
Pixel Imprint + PIN(screenshot).
- You will be asked to choose and enter a PIN, twice, and then to decide whether to have any notification shown on locked screen.
- After all that, you can finally register your virtual fingerprint, click
- When prompted to “Touch the Sensor” on the back of your phone (screenshot),
go to the
Extended controlswindow of the Android emulator (that you opened at the start of this process) and click on the
TOUCH THE SENSORbutton (screenshot).
- Keep on clicking on the
TOUCH THE SENSORbutton on the emulator’s extended control screen while the screen saying “Lift, then touch again” is up (screenshot).
- Once the virtual fingerprint is registered, you should see the “Fingerprint added” screen (screenshot).
DONEand you should be back on the “Security” screen with “1 fingerprint set up” (screenshot). Congratulations!
- With the
- Running the “SignIn” lab, whenever you are prompted for the “Biometric ID” (screenshot), click on the
TOUCH THE SENSOR" button on the emulator'sFingerprint` extended controls as you previously when registering your virtual fingerprint (screenshot).
To use the Android emulator with ARCore, please check out Installing ARCore on Android Emulator.
If your project builds on
OpenGL ES, you may want to check out:
- OpenGL ES 2.0 Graphics Programming for Android - JAVA
- OpenGL ES 3.1 Graphics Programming for Android - Native
Publishing an Android app
GooglePlay’s Testing Tracks can be used for beta testing (even A/B test)
To publish your app, follow Google’s Publish your app instructions.
- Gradle Basics for Android Developers
- Why Android created emulator rather than simulator?
- Simulator or Emulator? What is the difference?
- How to install Google Play app in Android Studio emulator?
Publishing an Android app
|Prepared for EECS 441 by Alexander Wu, Mark Wassink, Yibo Pi, and Sugih Jamin||Last updated: September 2nd, 2022|