SlideShare ist ein Scribd-Unternehmen logo
1 von 37
Android Technical Lead
Applause Inc.
Przemek Jakubczyk
pjakubczyk
@pjakubczyk
1
A guide to make crashproof libraries
It's always your fault
2
Background
At Applause I am responsible for quality
Applause SDK is crash and bug reporting library
shipped to over 1000 customers
SDK works with apps around the world
3
History
Joined 2 years ago to project with no QA
Today ~2200 unit tests covering 3k methods
82% lines covered
Last major problem was support for Marshmallow
4
Other than that over 6 months of no customer complain
How your SDK should look like
Be universal
Work in every provided configuration
Be robust
Work in any environment
Be defensive
5
GRADLE
6
Just use it.
7
because it’s the base for Android build system
Gradle
use simple tricks to protect your
styling
8
android {
resourcePrefix 'applause_'
}
or pass custom values to source
code via DSL
defaultConfig {
resValue "string",
"applause_library_version”,
"$version"
}
Gradle
easier integration with your Groovy
scripts
for example create own distribution
task
task buildAll (
dependsOn: 'assembleRelease',
type: Copy) {
from “build/outputs/”
into “another_path”
}
9
Gradle
10
Not possible :)
Often heard question. How to pass arguments to tasks?
Create new task for each configuration.
Gradle
11
task buildAll (
dependsOn:
["assembleFreeRelease,
assemblePaidRelease"]
)
Java
12
Java
catching Exception doesn’t solve problem
often hides real cause
tempting but dangerous
13
try {
network.getClients();
} catch (Exception e) {
// handle exception
}
Java
Null Object Pattern
instead constantly checking for not null
value
14
public interface Api {
void post(String action);
Api NULL = new Api() {
void post(String action){}
};
}
getApi().post(“Works”)
Java
NPE
Null Pointer Exception is the most popular
exception thrown in runtime.
NullObject pattern partially solves the problem.
Use empty objects, collections etc;
15
List<User> fetchUsers(){
try {
return api.getAllUsers();
} catch (IOException e){
return new
ArrayList<Users>();
}
}
Java
Usually library is started by one static method
… and next versions provide more functionality
Init interface becomes complex
16
Java
public static void start(
String baseUrl,
String defaultUser,
String defaultPassword,
boolean cacheRequests,
boolean forceHttps,
int timeout
)
17
Conf conf = new Conf.Builder()
.withUrl(“http://api.github.com”)
.withUser(“pjakubczyk”)
.withPassword(“droidcon2016Berlin”)
.withCache(false)
.withHttps(true)
.withTimeout(15)
.build();
Library.start(conf);
Java
Builder pattern organize configuration
Easier data validation
Pass only parameters user wants
Handling default values
18
Examples
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
19
Stetho.newInitializerBuilder(context)
.enableWebKitInspector(myInspector)
.build()
Concurrency
New Thread is your enemy
20
ExecutorService EXECUTOR =
Executors.newSingleThreadExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "NetworkExecutor");
}
});
The String
String is a bad information holder
Often passing 2 string parameters to method
Lead to simple mistakes
User vs Person
Instead of getters maybe method ‘serializeForAuth’ ?
21
Android
22
Android
View.inEditMode() determines if view is drawn in Android Studio
Usually to disable other components
Let’s invert the usage
23
24
public void onFinishInflate(){
TextView title = findViewById(R.id.title);
if(inEditMode()) {
int count = title.getText().length();
if(count > 30){
title.setTextSize(24.0f);
} else {
title.setTextSize(30.0f);
}
}
}
Android
Build.VERSION.SDK_INT
ApplicationInfo.targetSdkVersion
the library doesn’t know where it’s run
25
Android
usually interface for loading pictures from to web to Widget looks like this:
pictureLoader.load(“url_to_resource”, imageView);
passing arguments extending from View, Activity etc.
often lead to Memory leak
queue is flooded with requests holding all references
Go back to javadoc and check java.lang.ref.WeakReference<T> :) 26
Android
Wrap OS Exceptions with your own implementation
BitmapFactory
Throws unchecked OutOfMemoryError
27
28
public static class BitmapCreatorException extends Exception {
public BitmapCreatorException(String detailMessage) {
super(detailMessage);
}
public BitmapCreatorException(Throwable throwable) {
super(throwable);
}
}
29
Bitmap createScaled(Bitmap bitmap, int width, int height) throws
BitmapCreatorException {
try {
Bitmap scaledBitmap =
Bitmap.createScaledBitmap(bitmap, width, height, true);
return nullCheck(scaledBitmap);
} catch (OutOfMemoryError error) {
throw new BitmapCreatorException(error);
}
}
Bitmap Provider
30
Bitmap nullCheck(Bitmap bitmap) throws BitmapCreatorException {
if (bitmap == null)
throw new BitmapCreatorException("Bitmap is empty");
else
return bitmap;
}
Bitmap Provider
Android
ProGuard is outstanding tool to shrink code and inject bytecode optimizations
While shipping your code you must either provide:
copy&paste configuration to ProGuard (latest plugin supports auto-configuration)
be transparent to ProGuard.
Configuration vs Transparency?
Transparency!
31
Android
http://www.methodscount.com/
32
Product Method count
com.squareup.okhttp3:okhttp:3.0.1 2704
io.relayr:android-sdk:1.0.2 5413
io.reactivex:rxjava:1.1.0 4605
com.google.code.gson:gson:2. 1341
com.applause:applause-sdk:3.4.0 5041
com.fasterxml.jackson.core:jackson-databind:2.7.0 10732
com.parse:parse-android:1.13.0 4543
The End
33
License
34
Licence
35
by default you own the copyright
no licence doesn’t conclue you can use it in your project
open code (found online) != open source movement
transferring code goes along with transferring the ownership
Check the licence
Check if it infects yours
(Apache, MIT vs GPL v2, LGPL, BSD)
Thank you
36
A guide to make crashproof libraries
It's always your fault
37

Weitere ähnliche Inhalte

Andere mochten auch

Crafting Great Hypotheses - Droidcon 2016
Crafting Great Hypotheses - Droidcon 2016Crafting Great Hypotheses - Droidcon 2016
Crafting Great Hypotheses - Droidcon 2016Hoang Huynh
 
7 Ways to improve your gradle build
7 Ways to improve your gradle build7 Ways to improve your gradle build
7 Ways to improve your gradle buildTania Pinheiro
 
Android through the Eyes of a Photographer
Android through the Eyes of a PhotographerAndroid through the Eyes of a Photographer
Android through the Eyes of a PhotographerAchim Fischer
 
Droidcon Italy 2015: can you work without open source libraries?
Droidcon Italy 2015: can you work without open source libraries?Droidcon Italy 2015: can you work without open source libraries?
Droidcon Italy 2015: can you work without open source libraries?gabrielemariotti
 
Testable Android Apps DroidCon Italy 2015
Testable Android Apps DroidCon Italy 2015Testable Android Apps DroidCon Italy 2015
Testable Android Apps DroidCon Italy 2015Fabio Collini
 
Mastering Material Motion
Mastering Material MotionMastering Material Motion
Mastering Material MotionMike Wolfson
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & viewsma-polimi
 
Introduction to Retrofit and RxJava
Introduction to Retrofit and RxJavaIntroduction to Retrofit and RxJava
Introduction to Retrofit and RxJavaFabio Collini
 
Android architecture
Android architecture Android architecture
Android architecture Trong-An Bui
 
A Journey Through MV Wonderland
A Journey Through MV WonderlandA Journey Through MV Wonderland
A Journey Through MV WonderlandFlorina Muntenescu
 
Android Components & Manifest
Android Components & ManifestAndroid Components & Manifest
Android Components & Manifestma-polimi
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Androidma-polimi
 
MVVM & Data Binding Library
MVVM & Data Binding Library MVVM & Data Binding Library
MVVM & Data Binding Library 10Clouds
 
Data Binding in Action using MVVM pattern
Data Binding in Action using MVVM patternData Binding in Action using MVVM pattern
Data Binding in Action using MVVM patternFabio Collini
 
Set it and forget it: Let the machine learn its job - Guy Baron, Vonage
Set it and forget it: Let the machine learn its job - Guy Baron, VonageSet it and forget it: Let the machine learn its job - Guy Baron, Vonage
Set it and forget it: Let the machine learn its job - Guy Baron, VonageDroidConTLV
 
Android clean architecture workshop 3h edition
Android clean architecture workshop 3h editionAndroid clean architecture workshop 3h edition
Android clean architecture workshop 3h editionJorge Ortiz
 
MVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mixMVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mixFlorina Muntenescu
 

Andere mochten auch (20)

Crafting Great Hypotheses - Droidcon 2016
Crafting Great Hypotheses - Droidcon 2016Crafting Great Hypotheses - Droidcon 2016
Crafting Great Hypotheses - Droidcon 2016
 
7 Ways to improve your gradle build
7 Ways to improve your gradle build7 Ways to improve your gradle build
7 Ways to improve your gradle build
 
forpress-droidconit2016
forpress-droidconit2016forpress-droidconit2016
forpress-droidconit2016
 
Android through the Eyes of a Photographer
Android through the Eyes of a PhotographerAndroid through the Eyes of a Photographer
Android through the Eyes of a Photographer
 
Droidcon Italy 2015: can you work without open source libraries?
Droidcon Italy 2015: can you work without open source libraries?Droidcon Italy 2015: can you work without open source libraries?
Droidcon Italy 2015: can you work without open source libraries?
 
Testable Android Apps DroidCon Italy 2015
Testable Android Apps DroidCon Italy 2015Testable Android Apps DroidCon Italy 2015
Testable Android Apps DroidCon Italy 2015
 
Mastering Material Motion
Mastering Material MotionMastering Material Motion
Mastering Material Motion
 
Android activities & views
Android activities & viewsAndroid activities & views
Android activities & views
 
Introduction to Retrofit and RxJava
Introduction to Retrofit and RxJavaIntroduction to Retrofit and RxJava
Introduction to Retrofit and RxJava
 
Android MVVM
Android MVVMAndroid MVVM
Android MVVM
 
Android architecture
Android architecture Android architecture
Android architecture
 
A Journey Through MV Wonderland
A Journey Through MV WonderlandA Journey Through MV Wonderland
A Journey Through MV Wonderland
 
Android Components & Manifest
Android Components & ManifestAndroid Components & Manifest
Android Components & Manifest
 
Events and Listeners in Android
Events and Listeners in AndroidEvents and Listeners in Android
Events and Listeners in Android
 
MVVM & Data Binding Library
MVVM & Data Binding Library MVVM & Data Binding Library
MVVM & Data Binding Library
 
Data Binding in Action using MVVM pattern
Data Binding in Action using MVVM patternData Binding in Action using MVVM pattern
Data Binding in Action using MVVM pattern
 
Set it and forget it: Let the machine learn its job - Guy Baron, Vonage
Set it and forget it: Let the machine learn its job - Guy Baron, VonageSet it and forget it: Let the machine learn its job - Guy Baron, Vonage
Set it and forget it: Let the machine learn its job - Guy Baron, Vonage
 
Android clean architecture workshop 3h edition
Android clean architecture workshop 3h editionAndroid clean architecture workshop 3h edition
Android clean architecture workshop 3h edition
 
Android Platform Architecture
Android Platform ArchitectureAndroid Platform Architecture
Android Platform Architecture
 
MVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mixMVVM and RxJava – the perfect mix
MVVM and RxJava – the perfect mix
 

Ähnlich wie Droidcon Berlin Barcamp 2016

It's always your fault. Poznań ADG 2016
It's always your fault. Poznań ADG 2016It's always your fault. Poznań ADG 2016
It's always your fault. Poznań ADG 2016Przemek Jakubczyk
 
Android Applications Development: A Quick Start Guide
Android Applications Development: A Quick Start GuideAndroid Applications Development: A Quick Start Guide
Android Applications Development: A Quick Start GuideSergii Zhuk
 
Visage Android Hands-on Lab
Visage Android Hands-on LabVisage Android Hands-on Lab
Visage Android Hands-on LabStephen Chin
 
Framework engineering JCO 2011
Framework engineering JCO 2011Framework engineering JCO 2011
Framework engineering JCO 2011YoungSu Son
 
What's new in android 2018 (dev fest)
What's new in android 2018 (dev fest)What's new in android 2018 (dev fest)
What's new in android 2018 (dev fest)Shady Selim
 
Getting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with ThymeleafGetting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with ThymeleafMasatoshi Tada
 
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023Nicolas HAAN
 
android design pattern
android design patternandroid design pattern
android design patternLucas Xu
 
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdfcNguyn506241
 
Infinum Android Talks #14 - Data binding to the rescue... or not (?) by Krist...
Infinum Android Talks #14 - Data binding to the rescue... or not (?) by Krist...Infinum Android Talks #14 - Data binding to the rescue... or not (?) by Krist...
Infinum Android Talks #14 - Data binding to the rescue... or not (?) by Krist...Infinum
 
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018Somkiat Khitwongwattana
 
Divide and Conquer – Microservices with Node.js
Divide and Conquer – Microservices with Node.jsDivide and Conquer – Microservices with Node.js
Divide and Conquer – Microservices with Node.jsSebastian Springer
 
Hacking the Codename One Source Code - Part IV - Transcript.pdf
Hacking the Codename One Source Code - Part IV - Transcript.pdfHacking the Codename One Source Code - Part IV - Transcript.pdf
Hacking the Codename One Source Code - Part IV - Transcript.pdfShaiAlmog1
 
Code Obfuscation for Android & WP7
Code Obfuscation for Android & WP7Code Obfuscation for Android & WP7
Code Obfuscation for Android & WP7Jeff Bollinger
 
React Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsReact Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsMatteo Manchi
 

Ähnlich wie Droidcon Berlin Barcamp 2016 (20)

It's always your fault. Poznań ADG 2016
It's always your fault. Poznań ADG 2016It's always your fault. Poznań ADG 2016
It's always your fault. Poznań ADG 2016
 
It's always your fault
It's always your faultIt's always your fault
It's always your fault
 
Android Applications Development: A Quick Start Guide
Android Applications Development: A Quick Start GuideAndroid Applications Development: A Quick Start Guide
Android Applications Development: A Quick Start Guide
 
AngularJS in large applications - AE NV
AngularJS in large applications - AE NVAngularJS in large applications - AE NV
AngularJS in large applications - AE NV
 
Visage Android Hands-on Lab
Visage Android Hands-on LabVisage Android Hands-on Lab
Visage Android Hands-on Lab
 
Framework engineering JCO 2011
Framework engineering JCO 2011Framework engineering JCO 2011
Framework engineering JCO 2011
 
What's new in android 2018 (dev fest)
What's new in android 2018 (dev fest)What's new in android 2018 (dev fest)
What's new in android 2018 (dev fest)
 
Getting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with ThymeleafGetting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with Thymeleaf
 
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
Comment développer une application mobile en 8 semaines - Meetup PAUG 24-01-2023
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
Vaadin & Web Components
Vaadin & Web ComponentsVaadin & Web Components
Vaadin & Web Components
 
android design pattern
android design patternandroid design pattern
android design pattern
 
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
49.INS2065.Computer Based Technologies.TA.NguyenDucAnh.pdf
 
Node js
Node jsNode js
Node js
 
Infinum Android Talks #14 - Data binding to the rescue... or not (?) by Krist...
Infinum Android Talks #14 - Data binding to the rescue... or not (?) by Krist...Infinum Android Talks #14 - Data binding to the rescue... or not (?) by Krist...
Infinum Android Talks #14 - Data binding to the rescue... or not (?) by Krist...
 
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
Hello, Android Studio 3.2 & Android App Bundle @ I/O Extended Bangkok 2018
 
Divide and Conquer – Microservices with Node.js
Divide and Conquer – Microservices with Node.jsDivide and Conquer – Microservices with Node.js
Divide and Conquer – Microservices with Node.js
 
Hacking the Codename One Source Code - Part IV - Transcript.pdf
Hacking the Codename One Source Code - Part IV - Transcript.pdfHacking the Codename One Source Code - Part IV - Transcript.pdf
Hacking the Codename One Source Code - Part IV - Transcript.pdf
 
Code Obfuscation for Android & WP7
Code Obfuscation for Android & WP7Code Obfuscation for Android & WP7
Code Obfuscation for Android & WP7
 
React Native for multi-platform mobile applications
React Native for multi-platform mobile applicationsReact Native for multi-platform mobile applications
React Native for multi-platform mobile applications
 

Mehr von Przemek Jakubczyk

Android Auto instrumentation
Android Auto instrumentationAndroid Auto instrumentation
Android Auto instrumentationPrzemek Jakubczyk
 
How to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android appHow to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android appPrzemek Jakubczyk
 
How to recognise that the user has just uninstalled your android app droidc...
How to recognise that the user has just uninstalled your android app   droidc...How to recognise that the user has just uninstalled your android app   droidc...
How to recognise that the user has just uninstalled your android app droidc...Przemek Jakubczyk
 

Mehr von Przemek Jakubczyk (8)

Android Auto instrumentation
Android Auto instrumentationAndroid Auto instrumentation
Android Auto instrumentation
 
RoboSpock Poznań ADG 2016
RoboSpock Poznań ADG 2016RoboSpock Poznań ADG 2016
RoboSpock Poznań ADG 2016
 
RoboSpock
RoboSpockRoboSpock
RoboSpock
 
How to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android appHow to recognise that the user has just uninstalled your android app
How to recognise that the user has just uninstalled your android app
 
How to recognise that the user has just uninstalled your android app droidc...
How to recognise that the user has just uninstalled your android app   droidc...How to recognise that the user has just uninstalled your android app   droidc...
How to recognise that the user has just uninstalled your android app droidc...
 
Uninstall opera
Uninstall operaUninstall opera
Uninstall opera
 
Android accounts & sync
Android accounts & syncAndroid accounts & sync
Android accounts & sync
 
Robospock droidcon '14
Robospock   droidcon '14Robospock   droidcon '14
Robospock droidcon '14
 

Kürzlich hochgeladen

BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceDelhi Call girls
 
Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfCWS Technology
 
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRnishacall1
 
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Servicenishacall1
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPsychicRuben LoveSpells
 

Kürzlich hochgeladen (6)

BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort ServiceBDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
BDSM⚡Call Girls in Sector 71 Noida Escorts >༒8448380779 Escort Service
 
Leading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdfLeading Mobile App Development Companies in India (2).pdf
Leading Mobile App Development Companies in India (2).pdf
 
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCRFULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
FULL ENJOY - 9999218229 Call Girls in {Mahipalpur}| Delhi NCR
 
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
9999266834 Call Girls In Noida Sector 52 (Delhi) Call Girl Service
 
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
Obat Penggugur Kandungan Di Apotik Kimia Farma (087776558899)
 
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost LoverPowerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
Powerful Love Spells in Arkansas, AR (310) 882-6330 Bring Back Lost Lover
 

Droidcon Berlin Barcamp 2016

  • 1. Android Technical Lead Applause Inc. Przemek Jakubczyk pjakubczyk @pjakubczyk 1
  • 2. A guide to make crashproof libraries It's always your fault 2
  • 3. Background At Applause I am responsible for quality Applause SDK is crash and bug reporting library shipped to over 1000 customers SDK works with apps around the world 3
  • 4. History Joined 2 years ago to project with no QA Today ~2200 unit tests covering 3k methods 82% lines covered Last major problem was support for Marshmallow 4 Other than that over 6 months of no customer complain
  • 5. How your SDK should look like Be universal Work in every provided configuration Be robust Work in any environment Be defensive 5
  • 7. Just use it. 7 because it’s the base for Android build system
  • 8. Gradle use simple tricks to protect your styling 8 android { resourcePrefix 'applause_' } or pass custom values to source code via DSL defaultConfig { resValue "string", "applause_library_version”, "$version" }
  • 9. Gradle easier integration with your Groovy scripts for example create own distribution task task buildAll ( dependsOn: 'assembleRelease', type: Copy) { from “build/outputs/” into “another_path” } 9
  • 10. Gradle 10 Not possible :) Often heard question. How to pass arguments to tasks?
  • 11. Create new task for each configuration. Gradle 11 task buildAll ( dependsOn: ["assembleFreeRelease, assemblePaidRelease"] )
  • 13. Java catching Exception doesn’t solve problem often hides real cause tempting but dangerous 13 try { network.getClients(); } catch (Exception e) { // handle exception }
  • 14. Java Null Object Pattern instead constantly checking for not null value 14 public interface Api { void post(String action); Api NULL = new Api() { void post(String action){} }; } getApi().post(“Works”)
  • 15. Java NPE Null Pointer Exception is the most popular exception thrown in runtime. NullObject pattern partially solves the problem. Use empty objects, collections etc; 15 List<User> fetchUsers(){ try { return api.getAllUsers(); } catch (IOException e){ return new ArrayList<Users>(); } }
  • 16. Java Usually library is started by one static method … and next versions provide more functionality Init interface becomes complex 16
  • 17. Java public static void start( String baseUrl, String defaultUser, String defaultPassword, boolean cacheRequests, boolean forceHttps, int timeout ) 17 Conf conf = new Conf.Builder() .withUrl(“http://api.github.com”) .withUser(“pjakubczyk”) .withPassword(“droidcon2016Berlin”) .withCache(false) .withHttps(true) .withTimeout(15) .build(); Library.start(conf);
  • 18. Java Builder pattern organize configuration Easier data validation Pass only parameters user wants Handling default values 18
  • 19. Examples Retrofit retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); 19 Stetho.newInitializerBuilder(context) .enableWebKitInspector(myInspector) .build()
  • 20. Concurrency New Thread is your enemy 20 ExecutorService EXECUTOR = Executors.newSingleThreadExecutor(new ThreadFactory() { @Override public Thread newThread(Runnable r) { return new Thread(r, "NetworkExecutor"); } });
  • 21. The String String is a bad information holder Often passing 2 string parameters to method Lead to simple mistakes User vs Person Instead of getters maybe method ‘serializeForAuth’ ? 21
  • 23. Android View.inEditMode() determines if view is drawn in Android Studio Usually to disable other components Let’s invert the usage 23
  • 24. 24 public void onFinishInflate(){ TextView title = findViewById(R.id.title); if(inEditMode()) { int count = title.getText().length(); if(count > 30){ title.setTextSize(24.0f); } else { title.setTextSize(30.0f); } } }
  • 26. Android usually interface for loading pictures from to web to Widget looks like this: pictureLoader.load(“url_to_resource”, imageView); passing arguments extending from View, Activity etc. often lead to Memory leak queue is flooded with requests holding all references Go back to javadoc and check java.lang.ref.WeakReference<T> :) 26
  • 27. Android Wrap OS Exceptions with your own implementation BitmapFactory Throws unchecked OutOfMemoryError 27
  • 28. 28 public static class BitmapCreatorException extends Exception { public BitmapCreatorException(String detailMessage) { super(detailMessage); } public BitmapCreatorException(Throwable throwable) { super(throwable); } }
  • 29. 29 Bitmap createScaled(Bitmap bitmap, int width, int height) throws BitmapCreatorException { try { Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, width, height, true); return nullCheck(scaledBitmap); } catch (OutOfMemoryError error) { throw new BitmapCreatorException(error); } } Bitmap Provider
  • 30. 30 Bitmap nullCheck(Bitmap bitmap) throws BitmapCreatorException { if (bitmap == null) throw new BitmapCreatorException("Bitmap is empty"); else return bitmap; } Bitmap Provider
  • 31. Android ProGuard is outstanding tool to shrink code and inject bytecode optimizations While shipping your code you must either provide: copy&paste configuration to ProGuard (latest plugin supports auto-configuration) be transparent to ProGuard. Configuration vs Transparency? Transparency! 31
  • 32. Android http://www.methodscount.com/ 32 Product Method count com.squareup.okhttp3:okhttp:3.0.1 2704 io.relayr:android-sdk:1.0.2 5413 io.reactivex:rxjava:1.1.0 4605 com.google.code.gson:gson:2. 1341 com.applause:applause-sdk:3.4.0 5041 com.fasterxml.jackson.core:jackson-databind:2.7.0 10732 com.parse:parse-android:1.13.0 4543
  • 35. Licence 35 by default you own the copyright no licence doesn’t conclue you can use it in your project open code (found online) != open source movement transferring code goes along with transferring the ownership Check the licence Check if it infects yours (Apache, MIT vs GPL v2, LGPL, BSD)
  • 37. A guide to make crashproof libraries It's always your fault 37

Hinweis der Redaktion

  1. Mind that previous part will guard second one
  2. Good that we are talking about tasks