Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

DEVELOPING ANDROID

APPLICATIONS WITH KOTLIN

Spring 2022
Reichman University
Written By Eran Katsav (All rights reserved)
COURSE STAFF

• Eran Katsav (Me, lecturer)


Email: erankatsav@gmail.com
Office hours: Sunday, 8:00 PM, ZOOM, An appointment needed

• Marko Katziv (Teaching Assistant)


Email: btlltk13@gmail.com
Office hours: Thursday, 9:00 PM, ZOOM, An Appointment needed
COURSE REQUIREMENTS

• Two submission assignments – 30% of the final grade


• One will focus on Views and UI – You can start today! (Tutorial) - Submission on
the 30th of March
• The other will contain more complex UI logic and multi screen Android app –
Submission on the 27th of April.
• Final Project - Done in pairs or threesomes – 70% of the final grade
• The final project Will be a market level Android app interacting with both local and
remote database, built according to the Jetpack architecture guidelines and will
include most of the Android Components learned in class.
• The final project’s submission is in the end of the semester – a project review
meeting will be held in the last week of the semester.
COURSE REQUIREMENTS 2

• JetBrains IntelliJ Community Edition (For Kotlin code)


Download from here

• Android Studio latest version


Download from here

• An Android OS physical device is highly recommended (preferred one with


clean Android OS like Android One)
COARSE SCHEDULE

Week 1 Android OS Overview & Intro to Kotlin


Week 2 Kotlin – Functional Programming

Week 3 OOP with Kotlin


Week 4 Asynchronous programming with Coroutines
Week 5 Android Building blocks Part 1:
Activities, Intents, Permissions, Lifecycle and Persistent storage
Week 6 Android Building blocks Part 2: Fragments & Adapters
Week 7 MVVM (Model View View-Model) and Live Data
Week 8 Local Databases with Room – Jetpacked Project
Week 9 Phone Services
Week 10 Dependency Injection with Hilt
Week 11
Full Jetpack Architecture App with Hilt, Room and Retrofit 2
Week 12 The Broadcasting system - Broadcast Receivers and Alarm Service

Week 13 Non-UI Related tasks - Services, Job Services and Work Manager
TODAY TOPICS

• Android Open Source Project


• Android Platform Architecture
• JVM, DVM and ART
• ART Garbage collector
• Android OS Historical Overview
• Introduction – Android Native Development
• AndroidX & Android Jetpack Components
• Kotlin overview
• Start coding with Kotlin:
• Kotlin vals & vars
• Kotlin’s Nullability
ANDROID HISTORICAL OVERVIEW

• Android Inc founded in 2003 and in 2005 Purchased by Google


• First Android phone device was released in 2008 – HTC Dream
• From 2012 it is the leading OS for mobile devices and from and since 2017 it is the
leading OS for all devices (87% percent of mobile phones).
• Android is an open source project that can be downloaded from here
• Android exist in other platforms besides mobile devices like Android TV, Android
Wear , Android for Cars and Chrome OS
• Latest Android release is Android 12
• Android releases distribution can be found in Android Studio (under help me choose)
ANDROID OPEN SOURCE PROJECT
(AOSP)

• Android is an open source operating system for mobile devices and a corresponding open source project
led by Google.
• You can download the source code from here and create custom variants of the Android OS and you
don’t need to report back.
• free to download, free to alter, free to build into a product that can power just about anything for
Samsung and for everyone else.
• It’s part of Google’s Agenda that everyone should have internet with them.
• Even Google's own phones aren't using an open-source version of Android.
• When each manufacture wants also the Google mobile and play services (which includes the location
service, speech recognition, as set of default applications like photos and Gmail and much more), He has
to adhere the Google as a partner, accept its terms (through these rules google is making money).
• By providing Android as source code anyone can use instead of a standalone product like Windows,
Google can reap the benefits while another company does the work of building and maintaining a living
operating system.
GOOGLEANDROID
MOBILE &OPEN
PLAY SOURCE
SERVICESPROJECT
(AOSP)

• Android is an open source operating system for mobile devices and a corresponding open source project
• The GMS
led by(Google
Google.Mobile Services) Core goes hand-in-hand with Google Play Services and what are also
known as can
• You GApps (Google's
download Apps).code from here and create custom variants of the Android OS and you
the source
don’tCore
• The GMS need is
to the
report back.part of the Android OS
un-free
• freePlay
• Google to download,isfree to alter, free to build into a product that can power just about anything for
SamsungServices an app
and for everyone that
else. connects to the GMS framework and provides a set of APIs that developers
can use to enhance their apps and to make them perform the same across multiple different versions of
• It’s part of Google’s Agenda that everyone should have internet with them.
Android.
• EvenasGoogle's
• It works a set of own phones
API that aren't
is built in using an open-source
a client-server modelversion of Android.
and allows developers to use things like Location,
Maps to show
• When eachthemanufacture
location required,
wants alsoSpeech recognition
the Google mobileused
and by
playallot of other
services apps,
(which Google
includes theDrive to store data
location
service,
like game speechor
progress recognition, as set
use even use of default
Google applications like photos and Gmail and much more), He has
Sign-in.
to adhere the Google as a partner, accept its terms (through these rules google is making money) and
• Appsmake
like Gmail,
the OSChrome, Search
support any andapplication
other even more need
found in these services
the Google playsostore.
that they will work.
By providing
• To •install the GMSAndroid
yourselfasrequires
source code anyone
system can use to
permission instead of a into
put files standalone
foldersproduct like running
inside the Windows,system, and
Google can reap the benefits while another
you can't replace or overwrite files that are in use. company does the work of building and maintaining a living
operating system.
ANDROID PLATFORM
ARCHITECTURE
VIRTUAL MACHINES

• Virtual Machine enables the same code to be run on multiple platform


independent of the underlying hardware.
• Advantages: The application code is isolated from the core OS & It provides
cross compatibility or platform independency.
• Java uses JVM to convert the Java Byte-code (class files) into a specific
machine code.
• Until Android 5.0 Android used Dalvik Virtual Machine(DVM) which was
optimized for low-powered handheld devices. Each application is given its
own VM (single instance is of JVM is shared with multiple applications).
• In Dalvik the java class files are are complied by the DEX Compiler into
Dalvik Byte code (.dex) and the executable is APK not JAR like the JVM
ANDROID RUNTIME (ART)

• Nowadays Android replaced DVM with ART and added to JIT(Just In Time) the AOT(Ahead-of-Time) compilation
• In ART, During the app’s installation phase, AOT compilation statically translates the DEX bytecode into machine
code and stores in the device’s storage (the app is compiled only once and not on each launch).
• ART benefits:
• Apps run faster as DEX bytecode translation done only during installation.
• Reduced startup time of applications as native code is directly executed.
• Improved battery performance as power utilized to interpreted byte codes line by line is saved.
• Improved garbage collector (will be discussed later on)
• ART with only AOT takes long installation time and space So from Android 7.0 ART is Hybrid and uses also JIT
• Now with ART and the new JIT the application installation is faster. The new JIT constantly does profiling and
improves applications as they run.
ART GARBAGE COLLECTOR

• As we know the Garbage collector is the one responsible to free up memory by deallocating unmapped objects.
• Although GC event is quite fast in a wrong time it can affect performance.
• We want to be efficient about the events so the ART divides the objects into generations.
• A newly created object belong to the young generation and the longer it survives then it moves to the old
generation and there also the permanent generation (usual for the VM objects).
• The idea is that each generation has a limit, when we reach that limit a GC event is trigged this way we can
minimize the GC events. The GC event cause the system to stop executing the current process.
• When the young generation fills up, this causes a minor garbage collection. A young generation full of dead objects
is collected very quickly.
• Eventually the old generation needs to be collected. This event is called a major garbage collection.
• Make sure your code isn’t polluting the memory heap with allot of object since GC events can affect performance.
NATIVE ANDROID DEVELOPMENT

• Native Android development done In Java or Kotlin


• Kotlin is the preferred language by Google since 2019
• Non native platform allow one code base for both iOS and Android.
• The main one is Flutter which is built into Android Studio
• Native Advantages:
• Your application has full access to all of the phone Resources and Services.
• You can build the most Responsive Application with the best performance.
• You can use the brand new API out of the box and don’t have to wait for the mediator platform to create its own API which is
based on the Native one (no bridges needed).
• You can use ALL of the API features including low level libraries for BLE communication and more
• Generally speaking, native apps are more secure cause they have less vulnerabilities.
• Less system bugs (fewer systems, fewer mistakes…).
KOTLIN

• First published by JetBrains in 2011.


• Statically typed cross platform language.
• Fully compatible with Java and can run on the JVM
• It is much more concise then Java - less code and better readability lead to fewer errors.
• Source code in .kt files
• Kotlin main key features: Null safety, type inference, no semicolons J, class extensions, data classes,
functions as first class members which used anonymously with lambdas, destructuring declerations,
default function arguments and other very useful things.
• Besides her ability to run on the JVM, it can be used with Kotlin Mobile SDK to write one code base
for both iOS and Android, you can write server side code and web application because you can
transpile your Kotlin code to JavaScript
KOTLIN IN 100 SECONDS

https://www.youtube.com/watch?v=xT8oP0wy-A0
ANDROID JETPACK AND ANDROIDX

• From the Android Jetpack section in the Android Developer site:


Jetpack is a suite of libraries to help developers follow best practices, reduce boilerplate code, and write
code that works consistently across Android versions and devices so that developers can focus on the
code they care about.
• In Google I/O 2018 Jetpack was introduced and since then it includes all of the new additions to the
Android API:
• View Models and Live Data
• ROOM local database
• Navigation
• And much more
• Support Libraries enables backward compatibility of the new features on old Android SDK versions.
• The new support Library is called AndroidX – this is the main package name and it is written in Kotlin
BONUS - ANDROID VERSIONS
ANDROID SDK MAIN FEATURES (1)

• Android 3.0: Honeycomb (API 11) - 2011


• Honeycomb was released only for tablets – The large screen capabilities introduced Fragments.
• Android 5.0 Lollipop (API 21) - 2014
• Android Runtime (ART) with ahead-of-time (AOT) compilation and improved garbage collection (GC), replacing Dalvik that
combines bytecode interpretation with trace-based just-in-time (JIT) compilation.
• Material Design was introduced which uses more grid-based layouts, responsive animations and transitions, padding, and depth
effects such as lighting and shadows – this is the ruling design guidelines.
• Android 6.0 Marshmallow (API 23) - 2016
• Runtime permissions - App permissions now granted individually at run-time, not all-or-nothing at install time.
• Adoptable storage, an option to format and mount the memory card as extension of the internal storage instead of default
separate portable storage.
• Android 7.0 Nougat (API 24) – 2017
• New JIT Compiler, making for 75 percent faster app installations and a 50 percent reduction in compiled code size. Just in
Time (JIT) compiler with code profiling to ART, which lets it constantly improve the performance of Android apps as they run.
ANDROID SDK MAIN FEATURES (2)

• Android 8.0: Oreo (API 26) – 2017


• Notification Channels – Each notification must be assign to a channel
• Background execution limits – for the first time hard limitation enforced on apps residing in the background.
• App Architecture components were introduced (ViewModel, LiveDta, Room…)
• Android 9.0 (API 28) 2018
• Google announced Jetpack which represents the next generation of the Android Support Library - AndroidX
• Android 10.0 (API 29) - 2019
• More background limitations: New permissions required to access location in background and to access photo, video and audio files,
Background apps can no longer jump into the foreground.
• Android 11.0 (API 30) - 2020
• New Permission control including one time permission and permission reset option
• Android 12.0 (API 31) – 2021
• Material you an updated design language based one Material design

You might also like