Skip to content

01 - Android Intro

Topics

  • Android overview
  • Basic Android Studio usage
  • Kotlin language
  • UI creation
  • App lifecycle and state
  • Local storage and data access (SQLite)
  • Sensors (proximity, geomagnetic, motion, GPS, …)
  • Web services (REST API)
  • Modern architecture

Android

Operating system, devised for mobile equipment (mostly)

Usage: phones, tablets, TV-s, watches, glasses, cars, laptops, cameras, game consoles, …

Market share among smartphones – ca 85% (iOS 14%)

Open-source project

Google apps and services are closed source (mail, map, etc.)

History

  • 2003 – founded (lead: Andy Rubin)
    • Initial idea – OS for cameras
    • New plan – Mobile OS, (others: Symbian/Nokia and Win Mobile)
  • 2005 – Google acquires the whole project
  • 2007 – Open Handset Alliance
    • Google, HTC, Sony, Samsung, Dell, Motorola, LG, Qualcomm, Intel, etc…
  • 2008 – Android 1.0 (HTC Dream, no touchscreen)
  • 2009 – Android 1.5 Cupcake (iPhone 2007, iPhone 3G 2008)
  • 2010 – Android 2.2 Froyo, 2.3 Gingerbread
  • 2011 – Android 3.0 Honeycomb (tablets only)
  • 2011 - Android 4.0 Ice Cream Sandwich
    • HOLO UI
  • 2014 - Android 5 Lollipop
    • Material design
    • Dalvik vs ART (Android Runtime) (JIT or precompile, garbage collection)
  • 2015 - Android 6 Marshmallow
  • 2016 – Android 7 Nougat
  • 2017 – Android 8 Oreo
  • 2018 – Android 9 Pie
  • 2019 – Android 10 Q – moves closer to iOS, security clamped down

Version distribution

2020

  • Pie – 9.X – 10%
  • Oreo – 8.X – 28%
  • Nougat – 7.X - 19%
  • Marshmallow – 6.X – 17%
  • Lollipop - 5.X – 14%
  • KitKat – 4.4 – 7%
  • 4.4 and higher - ca 95%

Market

Latest info (2022)

  • 17 Jelly Bean 99.8%
  • 19 KitKat 99.4%
  • 22 LolliPop 97.3%
  • 23 Nougat 85.6%
  • 27 Oreo 78.7%
  • 28 Pie 69%
  • 29 Android 10 50.8%
  • 30 Android 11 24.3%

Android Architecture

Market

App types

  • NDK - C/C++
    • Close to hardware and operating system
  • SDK - Native <- this course!!!!
    • Kotlin/Java (ART/Dalvik), using system libraries
  • Hybrid – React Native, Ionic, etc.
  • Cross platform – Xamarin (C#), Flutter (Dart) etc.
  • Html/JS – Progressive Web Apps
    • One codebase/layout for different platforms
    • Problems with UI, weak access to hardware
  • There is also course on Hybrid Mobile Apps - ICD0018 (fall semester)

App architecture - AndroidManifest.xml

  • The manifest file presents essential information about your app to the Android system, information the system must have before it can run any of the app's code.
  • Describes the components of the application — the activities, services, broadcast receivers, and content providers that the application is composed of.
  • Declares which permissions the application must have in order to access protected parts of the API and interact with other applications.
  • Declares hardware requirements.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.akaver.demo01">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Demo01">

       <service
            android:name=".MyBgrService"
            android:enabled="true"
            android:exported="false"></service>

        <activity
            android:name=".ActivitySecond"
            android:exported="true" />

        <activity android:name=".MainActivity" android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

App architecture - build.gradle (app)

  • Declares the minimum level of the Android API that the application requires (minSdk).
  • Additional libraries (dependencies)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    compileSdk 31

    defaultConfig {
        applicationId "com.akaver.demo01"
        minSdk 22
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {

    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.3.1'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
    testImplementation 'junit:junit:4.+'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation 'com.google.android.gms:play-services-maps:17.0.0'
    implementation 'com.android.volley:volley:1.2.1'
}

App code

  • MainActivity.kt
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
package com.akaver.demo01

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}
  • actvity_main.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • UI generated from XML

Market

Other resources

  • Images
  • Animations
  • Menu
  • Strings
  • Misc files

APK

  • Android Application Package
  • ZIP file, combines all the resources and java bytecode
  • Signed with developer key
  • Developer key must be the same from version to next version
    • Don’t lose your keys (passwords)
  • Android Studio takes care of APK creation
  • APK-s can be downloaded from store, using 3-rd party utilities
  • Resources can be used as is
  • Most elements/code can be decompiled/recompiled

Google Play - app store

  • Almost no review process
  • Problems are dealt with afterwards
  • App hijacking, etc. are real problems

App security

  • Every app works in its own private virtual machine (Zygote)
  • Need permission for system resources/hardware
    • confirmed on app install
    • reconfirmed when app has not been used for some time
  • Data is private, no other app can access directly other app data
  • Everything is possible on rooted device
  • End user is the weakest link

Developer problems

  • Gazillion different hardware devices and capabilities
  • Lots of different Android implementations
    • Samsung TouchWiz
    • HTC Sense
    • ...
  • Migration to newer versions very slow (or not done at all)
  • Rooted phones
  • Ca 2X time spent on development compared to iOS
  • Ca 60% better income on iOS

Testing on devices

Market Market

Device variability

Market Market

Other hardware

Market Market Market Market Market

Hands on demo time - Notes

findViewById vs
Add kotlin-android-extensions to build.gradle (deprecated) for initial view elemnts to code binding.

1
2
3
4
5
6
plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

apply plugin: 'kotlin-android-extensions'

Remove virtual keyboard from screen when done with it

1
2
3
4
5
fun buttonGreetClicked(view: View) {
    textViewGreeting.text = "Hello, " + editTextName.text + "!"
    val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus()?.windowToken, 0)
}