Slide 1

Slide 1 text

Aung Kyaw Paing (Vincent) Senior Consultant @ thoughtworks | GDE Thailand aungkyawpaing.dev Android API 101 A intro to Android for non-Android devs

Slide 2

Slide 2 text

Android OS - Eventual Open source - Languages : Kotlin, Java & C - Run on Android Runtime (ART) - “Unused Memory is useless memory” principle

Slide 3

Slide 3 text

Android App - Sandboxed, every app creates it own users, i.e different users cannot see each other’s content - Unique application identifier (Reverse domain names) - Install via Android Package format (APK)

Slide 4

Slide 4 text

Build System Android officially uses Gradle as its build system. However, you can also use other build systems like buck build.

Slide 5

Slide 5 text

Android Components

Slide 6

Slide 6 text

● Activities ● Service ● Broadcast Receiver ● Content Provider 4 Main Components in Android API

Slide 7

Slide 7 text

Activity Consider this an entry point for your app. It holds the UI that the user currently sees

Slide 8

Slide 8 text

● Entry point for background tasks ● Foreground services where user is aware of (music playback, uploading a document.. etc), cannot be killed by System. ● Background service where user is not aware of (syncing data quietly), can be killed by System Service

Slide 9

Slide 9 text

● Receives events emitted by system (and other apps) such as dialing, low battery etc. ● Used mostly for alarm-based tasks or reacting to system changes Broadcast Receivers

Slide 10

Slide 10 text

● Exposes data for other apps to use ● Used for accessing system datas such as Contacts, Media etc.. Content Provider

Slide 11

Slide 11 text

Activating Components Components are activated through Intent (with the exception of Content Provider. Intent is the API that tells the system what the user wants to do.

Slide 12

Slide 12 text

//Tell system user want to view url val intent = Intent(Intent.ACTION_VIEW).apply { data = "http://www.example.com".toUri() } startActivity(intent) //Staring a service val serviceIntent = Intent(this, MyService::class.java) startService(serviceIntent) //or startForegroundService() //Register battery low events (you can also do this via Manifest) registerReceiver(BatteryLevelReceiver(), IntentFilter(Intent.ACTION_BATTERY_CHANGED))

Slide 13

Slide 13 text

Android Manifest

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Android Manifest An XML file that tells the OS everything about your app; what it is, what features does it use, what are the entry points… etc.

Slide 16

Slide 16 text

A typical Manifest contains ● Application Metadata ● Components Definitions ● Permission and Features requirements Android Manifest

Slide 17

Slide 17 text

Slide 18

Slide 18 text

Slide 19

Slide 19 text

APK

Slide 20

Slide 20 text

What’s inside an APK? - Manifest - DEX file (compiled code) - Resources - Assets

Slide 21

Slide 21 text

Decompile/Reverse Engineering https://github.com/skylot/jadx https://developer.android.com/studi o/debug/apk-analyzer

Slide 22

Slide 22 text

Splitting APK - CPU architecture - Screen density You can also generate a universal apk along with splitted apk

Slide 23

Slide 23 text

Android App Bundle A publishing format that contains universal codes, and let Google Play do the split and signing

Slide 24

Slide 24 text

APK ownership Each app is signed by a unique keystore+pass, where each keystore can have different alias+pass. If someone steal and modify the content, they cannot use the same keystore, and republish on stores and devices,

Slide 25

Slide 25 text

Google App Signing Instead of signing your app, you generate an upload key, and Google uses this key to sign you instead.

Slide 26

Slide 26 text

● Don’t need to worry about losing/managing keystore file ● Better Security ● App Bundle has Instant Apps, Dynamic Features Google App Signing Pros ● How can we know for sure Google won’t modify the contents? ● Platform tied-in ● App Bundle causes crash in some devices Cons https://commonsware.com/blog/2020/09/23/uncomfortable-questions-app-signing.html

Slide 27

Slide 27 text

Android Debug Bridge (ADB)

Slide 28

Slide 28 text

ADB ADB opens a communication to your debug application. You can install this via brew or use android sdk platform-tools

Slide 29

Slide 29 text

adb pair 192.168.0.242:38157 Wireless Debugging

Slide 30

Slide 30 text

#Push a file to Device adb push $FILE_IN_YOUR_PC$ $PATH_IN_YOUR_DEVICE$ adb push ~/document.txt /sdcard/document.txt #Pull a file from Device adb pull $FILE_IN_YOUR_DEVICE$ $FILE_IN_YOUR_PC$ adb pull /sdcard/document.txt ~/document.txt Transfer to/from device

Slide 31

Slide 31 text

# Input text adb shell input text user1 # Input Key Events (Enter, Tab etc) adb shell input keyevent 61 # Pro tips : define aliases alias testLogin=adb shell input text user1 && adb shell input keyevent 61 adb shell input text password Typing Text

Slide 32

Slide 32 text

Typing Text adb shell input text user1 && adb shell input keyevent 61 adb shell input text password

Slide 33

Slide 33 text

# Screenshot adb shell screencap $PATH_IN_DEVICE # Record video adb shell screenrecord $PATH_IN_DEVICE # Pro tips: pull screen record adb shell screenrecord $PATH_IN_DEVICE adb shell pull $PATH_IN_DEVICE Screenshot & Screen record

Slide 34

Slide 34 text

adb shell am start -W -a android.intent.action.VIEW -d adb shell am start -W -a android.intent.action.VIEW -d "https://example.com" com.google.chrome # Create an alias alias adOpenIntent=adb shell am start -W -a android.intent.action.VIEW -d Testing DeepLinks

Slide 35

Slide 35 text

Recap ● 4 main components: Activity, Service, Broadcast Receiver & Content Provider ● Manifest tells your app’s capabilities to the OS ● Never lose your keystore ● Weigh the pros and cons of app bundle & play app signing ● Utilize ADB for efficiency in your work (Shameless plug: https://www.aungkyawpaing.dev/useful-adb-commands/)

Slide 36

Slide 36 text

Q & A