android applications development: a quick start guide
TRANSCRIPT
1
ANDROID APPLICATIONS DEVELOPMENT: A QUICK START GUIDE
Sergii ZhukAndroid Developer at DAXX BV
Kyiv, Bibliotech Talks, 2015-04-28
2
Agenda
• Android platform
• How to start
• User Interface
• Basic Android patterns
• How to distribute your app
• Career of Android Developer
• Useful links, books, communities
3
Android OS• Linux but user doesn’t have root rights by default
• Open Source Project
• Each device manufacturer modifies OS for his needs, source code is not available in general
4
Application execution
Efficient Android Threading by Anders Goransson, O'Reilly Media, Inc., 2014
5
Android OS Distribution – 04-2015
https://developer.android.com/about/dashboards/index.html
6
Android: not only smartphones
7
Android: not only smartphones
8
Android: not only smartphones
9
Native vs Hybrid Mobile apps
http://www.addthis.com/blog/2014/10/27/7-things-to-consider-when-making-ios-and-android-apps-with-cordova-or-phonegap/
10
Android native apps development
• Java: to be executed using Dalvik or ART VMs
• С/С++: Native Development Kit – to develop high-performance modules, will be also launched using VMs
11
So, let’s start!
• JDK
• Android SDK
• IDE (Eclipse, IntelliJIDEA, Android Studio)
• Gradle build system (optional)
12
HelloWorld (1)
13
HelloWorld (2)
14
HelloWorld (3)
15
Launch your app
• Emulator supplied with Android SDK
• Genymotion emulator (based on Oracle VirtualBox)
• On your smartphone
16
AndroidManifest.xml
17
Manifest file - AndroidManifest.xml• Permissions (Internet access, storage, …)
• Min and target API version
• Special hardware requests (Camera, Bluetooth, …)
• Some custom Google tools (Play Services, Google Maps)
• Definition of Activities and Services
18
Permissions:all or nothing
19
Application resources
• Images, Strings, layouts
• Separately from application source code
• Unique integer ID will be generated for each resource name which could be used as a constant
• You can supply alternative resource with the same name to specific folders indicating screen size, locale, screen rotation etc
20
Multiple screens support (1)
• Density-independent pixel (dp) – virtual pixel
• “wrap_content” and “match_parent” special values for flexibility
21
Multiple screens support (2)
• MDPI
• HDPI
• XHDPI
• XXHDPI
http://developer.android.com/guide/practices/screens_support.html
22
Resources example
23
Multiple screens support (3)
Density Device Screen resolutionMDPI Galaxy Tab 3 1280*800
HDPI HTC Desire 480*800
XHDPI Nexus 7 1200*1920
XXHDPI Nexus 5 1080*1920
24
UI building blocks
• ViewGroup – abstract container for Views to show them using special rules• View – abstract widget like TextView, EditText, Checkbox
25
Layout example<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="16dp" android:gravity="center_horizontal" android:orientation="vertical"> <TextView android:id="@+id/text" android:layout_width="120dp" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:text="I am a TextView"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="I am a Button"/></LinearLayout>
26
Layout example
27
Common ViewGroups
Horizontal or vertical row, easy to stretch
Position of each element could be configured depending on parent and/or neighbors
Linear Layout Relative Layout
28
Common layouts – adapter-based
List View Grid View
29
Adapter
30
Activity
• Provides a screen with which user can interact to do something
• At least one activity per application
• Notified of screen navigation/device state change through lifecycle callback methods
31
Activity stack
Efficient Android Threading by Anders Goransson, O'Reilly Media, Inc., 2014
32
Activity workflow
33
Service
• Used for long-running operations in the background and does not provide a user interface
• Runs on the main thread of hosting process, could be configured to use a separate thread
34
Save application data
• SharedPreferences (key/value pairs)
• Files (internal/external storage)
• SQLite database
35
SQLite in Android• Implements most of the SQL-92 standard for
SQL
• All data for the app is stored in one file (if wasn’t configured separately)
• Cross-platform – also used in iOS and Blackberry
http://www.grokkingandroid.com/sqlite-in-android/
36
• Serverless, always installed in the system
• By default doesn’t use encryption
• By default is stored in the app folder, could be opened by external tools on rooted devices:
/data/data/<package-name>/databases
SQLite in Android
37
External database access
http://independent.academia.edu/parthiban6
38
SQLite Android data types
Type Meaning
INTEGERAny number which is not a floating point number
REALFloating-point numbers (8-Byte IEEE 754 – i.e. double precision)
TEXTAny String and also single characters (UTF-8, UTF-16BE or UTF-16LE)
BLOBA binary blob of data
http://www.grokkingandroid.com/sqlite-in-android/
39
Content provider
• Presents data to external applications as one or more tables like in a relational database
• System-provided Content providers examples: Contacts, list of music tracks, list of gallery images
• Thread safe
40
Broadcast receiver
• Allows to register for system or application events
• Operates with Intent which is a lightweight messaging object
• Could be Asynchronous - all receivers of the broadcast are run in an undefined order, often at the same time or Ordered
41
System broadcast messages example
Event Description
Intent.ACTION_BOOT_COMPLETED Boot completed
Intent.ACTION_POWER_CONNECTED Power got connected to the device
Intent.ACTION_POWER_DISCONNECTED Power got disconnected to the device
Intent.ACTION_BATTERY_LOW Triggered on low battery. Typically used to reduce activities in your app which consume power
Intent.ACTION_PHONE_STATE_CHANGED The call state (cellular) on the device has changed
42
Use Case: Interaction with backend
• Synchronous HTTP request in the separate thread- OR use external library (Retrofit, Volley, Enroscar)
• Parse result (for json - GSON is de-facto standard)
• Post result to the UI
43
Use Case: Facebook integration
44
Use Case: geolocation
• GPS, Cell-ID, Wi-Fi. Need to declare permissions at AndroidManifest.xml
• “Cold start”: wait for GPS
• Callback on device being moved
• Rapid battery drain problem
45
Automated application testing• No?
• Espresso and JUnit - uses emulator or real device– Slow
• Robolectric: texts executed inside your desktop PC JVM
• Blackbox UI testing (Robotium)
46
How to build your app
• In IDE
• Old school: Ant, Maven
• Gradle
47
Gradle
• Good IDE integration
• Dependency management (Maven, Ivy) out of the box
• Sign applications configuration out of the box
• Backward compatibility breaks quite often on new Gradle version update
48
Gradle script: shortened exampleandroid { compileSdkVersion 15 buildToolsVersion "22.0.1"
defaultConfig { applicationId "org.sergez.myapplication.app" minSdkVersion 9 targetSdkVersion 22 versionCode 1 versionName "1.0" } compileOptions { sourceCompatibility JavaVersion.VERSION_1_6 targetCompatibility JavaVersion.VERSION_1_6 } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard.txt'), 'p-r.pro' } }}dependencies { compile 'com.android.support:support-v4:22.0.0' compile 'com.android.support:appcompat-v7:22.+' compile fileTree(dir: 'libs', include: ['*.jar'])}
49
Where to publish your app
• Google Play Store
• Samsung Apps
• Amazon Appstore for Android
• Asian application stores
50
Google Play Store (1)
• $25 for Developer Account with publishing rights
• Application is available in Play Store in 3-6 hours after being published
• Auto-moderation in general, manual post-moderation could be applied in some cases
51
Google Play Store (2)
• Allows to publish app in alpha- and beta test modes
• Customization of app visibility for different countries and devices
• User could rate the app and leave a feedback, developer could respond to this feedback
52
Monetizing your app: Play Store
• Paid apps– Play Store takes 30%– Android users don’t like to pay
• Ads
• Donations
• Freemium
53
Keep in mind when starting a new project
• Android target version
• Special UI design for tablets
• Availability of mockups and flow explanation
• Amount of non-standard UI elements
• UI will be outdated in 0.5-2 years
54
Get a job: where and how?
55
• Freelance
• Android app as only a small part of a big product
• Mobile development company (outsourcing)
• Mobile-first product
Get a job: where and how?
56
Useful resources
• https://developer.android.com
• http://android-developers.blogspot.com/
• E-book «The Busy Coder's Guide to Android Development»
• Android Performance Patterns – YouTube and G+
• DOU.ua Android Digest
57
Useful resources
• Udacity “UD853 - Developing Android Apps”
• Coursera “Mobile Cloud Computing with Android”
• Lynda, Udemy, Stanford Online
• AppTractor Podcast (in Russian)