SlideShare ist ein Scribd-Unternehmen logo
1 von 55
Downloaden Sie, um offline zu lesen
10 ways to improve your Android app
Boris Farber
@borisfarber
● Long launch time
● Janky scrolling
● Unresponsive app
Data intensive apps
IF YOU HAVE A SMALL APP
FORGET THESE SLIDES
1 - Activity leaks
Activity
● Holding references to unused Activity
● Activity holds its layout ⇒ holds all views
public class LeakActivity extends Activity {
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
NastyManager.getInstance().addListener(this);
// ...
Listeners leak
@Override
public void onDestroy() {
super.onDestroy();
NastyManager.getInstance().removeListener(this);
}
Listeners leak + fix
remove listener
● Activities/fragments etc - they have a life cycle
● Activity will not be GC-ed
● Static references
○ become dangling "pointers"
○ m_staticActivity = staticFragment.getActivity()
Static References
Activity
2 - Activity leaks
Outer class (Activity)
Inner class (Handler)
public class MainActivity extends Activity {
// ...
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
}
};
// ...
This is a leak
handler.postDelayed(...)
What happens if
private static class MyHandler extends Handler {
private final WeakReference<MainActivity> mActivity;
// ...
public MyHandler(MainActivity activity) {
mActivity = new WeakReference<MainActivity>(activity);
// ...
}
@Override
public void handleMessage(Message msg) {
}
// ...
}
This is a leak + fix
Outer class (Activity)
Inner class (Handler)
● Non static Handler → Activity leak
● Both classes have a different a different lifetimes
Prefer static to non static inner classes
● Remove away static references
● Use event bus
● Unregister listeners
● Prefer static inner classes to non static ones
Avoid Activity leaks
● Do code reviews
● Understand your app structure
● Use tools (MAT ...)
● Print logs on callbacks
Avoid Activity leaks
3 - Use UI thread only
for UI
● Scrolling
Symptoms
● Images
● Networking
● JSON
● Database access
Not on UI thread
● Images
● Networking
● JSON
● Database access
Not on UI thread
Use
libraries
Use
Loaders
4 - Use libraries
● Already solve the problem
● Requires specialization (easy to make mistake)
○ Threading
○ Memory
○ Networking
○ Images
Use libraries
● Don’t lose focus
● Developer your solution for a good reason
Use libraries
● Volley
● OkHttp
● Retrofit
Common libraries - Async Http
● Glide
● Picasso
● Fresco
● UIL
Common libraries - Image loading
● Solves your problem
● Plays nicely with your current dependencies
● Dex method count
● Dependencies
● Maintenance
● Runtime permissions
Pick 3rd party lib checklist
5 - Large JSONs
● Parsing has a performance effect
● Converted to class with getters and setters
JSON
● Small JSONs - GSON is the best
● Large JSONs
○ Jackson
○ ig-json-parser
○ GSON + immutables type adapters
JSON
● Keep UI thread only for UI
● Understand concurrency APIs
● Use libraries (memory, caching, json ...)
● Use loaders
Scrolling
6 - System abuse
● Don't call private APIs by reflection
● Don't call private native methods (NDK/C level)
● Don't use Runtime.exec
● "adb shell am" to communicate with other process is not something
we want to support
System abuse
● Don't abuse the system
● Know and use APIs
System abuse
7 - Deprecation
● API will be removed
● Your app will not work
● No way to update APIs and tools
Deprecation
● There is a compelling reason to move
○ Security
○ Correctness
○ Performance
Deprecation
● Removed at M (still available as dependency)
● Use HttpURLConnection
○ Simple API
○ Small size
○ Transparent compression
○ Response caching
Don’t use Apache Http Connection
● Know and use APIs
● Refactor your dependencies
● Update your dependencies and tools
Deprecation
8 - Bring your own
gloves
● Various versions, devices, OEMs
● APIs and libraries shipped with platform might behave differently
● Async task ...
Bring your own gloves
● Consider bundling your own version
● Check dex limit
● Check code bloat
Bring your own gloves
● Own wrapped library (SSL/crypto etc')
● Rooted devices
● Your own sensitive data/protocols
Security
9 - Architecture
● Activities
● Fragments
● Tasks
● Flags
● Add logs on callbacks
Understand app components life cycle
● Consistent
● Get people on board quickly
● Have someone experienced
● Pick your dependencies wisely
What can you do
● Sleeping most of the time
● Responsive when “awaken”
Design your app
10 - Know your APK
● https://goo.gl/94woV7
● Executables browser
● Innovation comes from frustration
ClassyShark
tree
view
detail
view
● Dex limit numbers
● Obfuscation
● Package counts
● Why I need the dependency
Know your executable
● https://medium.com/@BorisFarber/
● https://medium.com/@_tiwiz/shrinking-your-build-with-no-rules-
8d9fb88281ac#.j0gb0lf8i
Learn more
#ClassyShark
Boris Farber
@borisfarber
Thank You!

Weitere ähnliche Inhalte

Was ist angesagt?

JAVA INTRODUCTION
JAVA INTRODUCTIONJAVA INTRODUCTION
JAVA INTRODUCTIONProf Ansari
 
From zero to hero with React Native!
From zero to hero with React Native!From zero to hero with React Native!
From zero to hero with React Native!Commit University
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in androidRakesh Jha
 
Application Profiling for Memory and Performance
Application Profiling for Memory and PerformanceApplication Profiling for Memory and Performance
Application Profiling for Memory and Performancepradeepfn
 
LDNSE: Testdroid for Mobile App and Web Testing (London Selenium Meetup)
LDNSE: Testdroid for Mobile App and Web Testing (London Selenium Meetup)LDNSE: Testdroid for Mobile App and Web Testing (London Selenium Meetup)
LDNSE: Testdroid for Mobile App and Web Testing (London Selenium Meetup)Bitbar
 
Automated Exploratory Testing
Automated Exploratory TestingAutomated Exploratory Testing
Automated Exploratory TestingJustin Ison
 
Android Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAndroid Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAnuradha Weeraman
 
Google Developer Day 2010 Japan: 高性能な Android アプリを作るには (ティム ブレイ)
Google Developer Day 2010 Japan: 高性能な Android アプリを作るには (ティム ブレイ)Google Developer Day 2010 Japan: 高性能な Android アプリを作るには (ティム ブレイ)
Google Developer Day 2010 Japan: 高性能な Android アプリを作るには (ティム ブレイ)Google Developer Relations Team
 
iOS development best practices
iOS development best practicesiOS development best practices
iOS development best practicesMichal Juhas
 
Interview Question & Answers for Selenium Freshers | LearningSlot
Interview Question & Answers for Selenium Freshers | LearningSlotInterview Question & Answers for Selenium Freshers | LearningSlot
Interview Question & Answers for Selenium Freshers | LearningSlotLearning Slot
 
Interview question & Answers for 3+ years experienced in Selenium | LearningSlot
Interview question & Answers for 3+ years experienced in Selenium | LearningSlotInterview question & Answers for 3+ years experienced in Selenium | LearningSlot
Interview question & Answers for 3+ years experienced in Selenium | LearningSlotLearning Slot
 
Plug yourself in and your app will never be the same (2 hour edition)
Plug yourself in and your app will never be the same (2 hour edition)Plug yourself in and your app will never be the same (2 hour edition)
Plug yourself in and your app will never be the same (2 hour edition)Mikkel Flindt Heisterberg
 
Experiences building apps with React Native @UtrechtJS May 2016
Experiences building apps with React Native @UtrechtJS May 2016Experiences building apps with React Native @UtrechtJS May 2016
Experiences building apps with React Native @UtrechtJS May 2016Adrian Philipp
 

Was ist angesagt? (19)

JAVA INTRODUCTION
JAVA INTRODUCTIONJAVA INTRODUCTION
JAVA INTRODUCTION
 
From zero to hero with React Native!
From zero to hero with React Native!From zero to hero with React Native!
From zero to hero with React Native!
 
Multithreading and concurrency in android
Multithreading and concurrency in androidMultithreading and concurrency in android
Multithreading and concurrency in android
 
Application Profiling for Memory and Performance
Application Profiling for Memory and PerformanceApplication Profiling for Memory and Performance
Application Profiling for Memory and Performance
 
L18 applets
L18 appletsL18 applets
L18 applets
 
applet using java
applet using javaapplet using java
applet using java
 
LDNSE: Testdroid for Mobile App and Web Testing (London Selenium Meetup)
LDNSE: Testdroid for Mobile App and Web Testing (London Selenium Meetup)LDNSE: Testdroid for Mobile App and Web Testing (London Selenium Meetup)
LDNSE: Testdroid for Mobile App and Web Testing (London Selenium Meetup)
 
Automated Exploratory Testing
Automated Exploratory TestingAutomated Exploratory Testing
Automated Exploratory Testing
 
Android Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the TrenchesAndroid Best Practices - Thoughts from the Trenches
Android Best Practices - Thoughts from the Trenches
 
Google Developer Day 2010 Japan: 高性能な Android アプリを作るには (ティム ブレイ)
Google Developer Day 2010 Japan: 高性能な Android アプリを作るには (ティム ブレイ)Google Developer Day 2010 Japan: 高性能な Android アプリを作るには (ティム ブレイ)
Google Developer Day 2010 Japan: 高性能な Android アプリを作るには (ティム ブレイ)
 
iOS development best practices
iOS development best practicesiOS development best practices
iOS development best practices
 
Interview Question & Answers for Selenium Freshers | LearningSlot
Interview Question & Answers for Selenium Freshers | LearningSlotInterview Question & Answers for Selenium Freshers | LearningSlot
Interview Question & Answers for Selenium Freshers | LearningSlot
 
Interview question & Answers for 3+ years experienced in Selenium | LearningSlot
Interview question & Answers for 3+ years experienced in Selenium | LearningSlotInterview question & Answers for 3+ years experienced in Selenium | LearningSlot
Interview question & Answers for 3+ years experienced in Selenium | LearningSlot
 
Aosp+
Aosp+Aosp+
Aosp+
 
Plug yourself in and your app will never be the same (2 hour edition)
Plug yourself in and your app will never be the same (2 hour edition)Plug yourself in and your app will never be the same (2 hour edition)
Plug yourself in and your app will never be the same (2 hour edition)
 
Java Applets
Java AppletsJava Applets
Java Applets
 
How to Setup A Pen test Lab and How to Play CTF
How to Setup A Pen test Lab and How to Play CTF How to Setup A Pen test Lab and How to Play CTF
How to Setup A Pen test Lab and How to Play CTF
 
Experiences building apps with React Native @UtrechtJS May 2016
Experiences building apps with React Native @UtrechtJS May 2016Experiences building apps with React Native @UtrechtJS May 2016
Experiences building apps with React Native @UtrechtJS May 2016
 
Features of java
Features of javaFeatures of java
Features of java
 

Andere mochten auch

ClassyShark slides
ClassyShark slidesClassyShark slides
ClassyShark slidesBoris Farber
 
Android async task
Android async taskAndroid async task
Android async taskMadhu Venkat
 
Mastering java bytecode with ASM - GeeCON 2012
Mastering java bytecode with ASM - GeeCON 2012Mastering java bytecode with ASM - GeeCON 2012
Mastering java bytecode with ASM - GeeCON 2012Anton Arhipov
 
Add ClassyShark to your Android toolbox
Add ClassyShark to your Android toolboxAdd ClassyShark to your Android toolbox
Add ClassyShark to your Android toolboxBoris Farber
 
Android - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTaskAndroid - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTaskHoang Ngo
 
Android Development: Build Android App from Scratch
Android Development: Build Android App from ScratchAndroid Development: Build Android App from Scratch
Android Development: Build Android App from ScratchTaufan Erfiyanto
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The BasicsMike Desjardins
 
Mobile App Development
Mobile App DevelopmentMobile App Development
Mobile App DevelopmentChris Morrell
 
Avg technologies android app_performance__trends_report_h1 2016
Avg technologies android app_performance__trends_report_h1 2016Avg technologies android app_performance__trends_report_h1 2016
Avg technologies android app_performance__trends_report_h1 2016AVG Technologies
 
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental pluginMastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental pluginXavier Hallade
 
The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017LinkedIn
 

Andere mochten auch (12)

Efficient Android Threading
Efficient Android ThreadingEfficient Android Threading
Efficient Android Threading
 
ClassyShark slides
ClassyShark slidesClassyShark slides
ClassyShark slides
 
Android async task
Android async taskAndroid async task
Android async task
 
Mastering java bytecode with ASM - GeeCON 2012
Mastering java bytecode with ASM - GeeCON 2012Mastering java bytecode with ASM - GeeCON 2012
Mastering java bytecode with ASM - GeeCON 2012
 
Add ClassyShark to your Android toolbox
Add ClassyShark to your Android toolboxAdd ClassyShark to your Android toolbox
Add ClassyShark to your Android toolbox
 
Android - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTaskAndroid - Thread, Handler and AsyncTask
Android - Thread, Handler and AsyncTask
 
Android Development: Build Android App from Scratch
Android Development: Build Android App from ScratchAndroid Development: Build Android App from Scratch
Android Development: Build Android App from Scratch
 
Android Development: The Basics
Android Development: The BasicsAndroid Development: The Basics
Android Development: The Basics
 
Mobile App Development
Mobile App DevelopmentMobile App Development
Mobile App Development
 
Avg technologies android app_performance__trends_report_h1 2016
Avg technologies android app_performance__trends_report_h1 2016Avg technologies android app_performance__trends_report_h1 2016
Avg technologies android app_performance__trends_report_h1 2016
 
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental pluginMastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
 
The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017The Top Skills That Can Get You Hired in 2017
The Top Skills That Can Get You Hired in 2017
 

Ähnlich wie 10 ways to improve your Android app performance

Xamarin.android memory management gotchas
Xamarin.android memory management gotchasXamarin.android memory management gotchas
Xamarin.android memory management gotchasAlec Tucker
 
Introduction to Android Development and Security
Introduction to Android Development and SecurityIntroduction to Android Development and Security
Introduction to Android Development and SecurityKelwin Yang
 
Liferay portals in real projects
Liferay portals  in real projectsLiferay portals  in real projects
Liferay portals in real projectsIBACZ
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidStanojko Markovik
 
Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Opersys inc.
 
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Milan 2017 - D...
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Milan 2017 - D...Monitoring Big Data Systems Done "The Simple Way" - Codemotion Milan 2017 - D...
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Milan 2017 - D...Demi Ben-Ari
 
Demi Ben-Ari - Monitoring Big Data Systems Done "The Simple Way" - Codemotion...
Demi Ben-Ari - Monitoring Big Data Systems Done "The Simple Way" - Codemotion...Demi Ben-Ari - Monitoring Big Data Systems Done "The Simple Way" - Codemotion...
Demi Ben-Ari - Monitoring Big Data Systems Done "The Simple Way" - Codemotion...Codemotion
 
Five android architecture
Five android architectureFive android architecture
Five android architectureTomislav Homan
 
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Berlin 2017
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Berlin 2017Monitoring Big Data Systems Done "The Simple Way" - Codemotion Berlin 2017
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Berlin 2017Demi Ben-Ari
 
Android development - the basics, MFF UK, 2012
Android development - the basics, MFF UK, 2012Android development - the basics, MFF UK, 2012
Android development - the basics, MFF UK, 2012Tomáš Kypta
 
Android Jump Start
Android Jump StartAndroid Jump Start
Android Jump StartConFoo
 
Recap of the google io 2017
Recap of the google io 2017Recap of the google io 2017
Recap of the google io 2017Karan Trehan
 
Android developer fundamentals training overview Part II
Android developer fundamentals training overview Part IIAndroid developer fundamentals training overview Part II
Android developer fundamentals training overview Part IIYoza Aprilio
 
Lecture #4 activities &amp; fragments
Lecture #4  activities &amp; fragmentsLecture #4  activities &amp; fragments
Lecture #4 activities &amp; fragmentsVitali Pekelis
 
Intro to XPages for Administrators (DanNotes, November 28, 2012)
Intro to XPages for Administrators (DanNotes, November 28, 2012)Intro to XPages for Administrators (DanNotes, November 28, 2012)
Intro to XPages for Administrators (DanNotes, November 28, 2012)Per Henrik Lausten
 
Building a data pipeline to ingest data into Hadoop in minutes using Streamse...
Building a data pipeline to ingest data into Hadoop in minutes using Streamse...Building a data pipeline to ingest data into Hadoop in minutes using Streamse...
Building a data pipeline to ingest data into Hadoop in minutes using Streamse...Guglielmo Iozzia
 
Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework Mutual Mobile
 
Leveraging Android's Linux Heritage
Leveraging Android's Linux HeritageLeveraging Android's Linux Heritage
Leveraging Android's Linux HeritageOpersys inc.
 

Ähnlich wie 10 ways to improve your Android app performance (20)

Xamarin.android memory management gotchas
Xamarin.android memory management gotchasXamarin.android memory management gotchas
Xamarin.android memory management gotchas
 
Introduction to Android Development and Security
Introduction to Android Development and SecurityIntroduction to Android Development and Security
Introduction to Android Development and Security
 
Liferay portals in real projects
Liferay portals  in real projectsLiferay portals  in real projects
Liferay portals in real projects
 
The Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with androidThe Good, the Bad and the Ugly things to do with android
The Good, the Bad and the Ugly things to do with android
 
Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013Android Internals at Linaro Connect Asia 2013
Android Internals at Linaro Connect Asia 2013
 
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Milan 2017 - D...
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Milan 2017 - D...Monitoring Big Data Systems Done "The Simple Way" - Codemotion Milan 2017 - D...
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Milan 2017 - D...
 
Demi Ben-Ari - Monitoring Big Data Systems Done "The Simple Way" - Codemotion...
Demi Ben-Ari - Monitoring Big Data Systems Done "The Simple Way" - Codemotion...Demi Ben-Ari - Monitoring Big Data Systems Done "The Simple Way" - Codemotion...
Demi Ben-Ari - Monitoring Big Data Systems Done "The Simple Way" - Codemotion...
 
Five android architecture
Five android architectureFive android architecture
Five android architecture
 
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Berlin 2017
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Berlin 2017Monitoring Big Data Systems Done "The Simple Way" - Codemotion Berlin 2017
Monitoring Big Data Systems Done "The Simple Way" - Codemotion Berlin 2017
 
Android development - the basics, MFF UK, 2012
Android development - the basics, MFF UK, 2012Android development - the basics, MFF UK, 2012
Android development - the basics, MFF UK, 2012
 
Android Jump Start
Android Jump StartAndroid Jump Start
Android Jump Start
 
Recap of the google io 2017
Recap of the google io 2017Recap of the google io 2017
Recap of the google io 2017
 
Android developer fundamentals training overview Part II
Android developer fundamentals training overview Part IIAndroid developer fundamentals training overview Part II
Android developer fundamentals training overview Part II
 
Lecture #4 activities &amp; fragments
Lecture #4  activities &amp; fragmentsLecture #4  activities &amp; fragments
Lecture #4 activities &amp; fragments
 
Intro to XPages for Administrators (DanNotes, November 28, 2012)
Intro to XPages for Administrators (DanNotes, November 28, 2012)Intro to XPages for Administrators (DanNotes, November 28, 2012)
Intro to XPages for Administrators (DanNotes, November 28, 2012)
 
Go at Skroutz
Go at SkroutzGo at Skroutz
Go at Skroutz
 
Building a data pipeline to ingest data into Hadoop in minutes using Streamse...
Building a data pipeline to ingest data into Hadoop in minutes using Streamse...Building a data pipeline to ingest data into Hadoop in minutes using Streamse...
Building a data pipeline to ingest data into Hadoop in minutes using Streamse...
 
Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework Android Frameworks: Highlighting the Need for a Solid Development Framework 
Android Frameworks: Highlighting the Need for a Solid Development Framework 
 
Leveraging Android's Linux Heritage
Leveraging Android's Linux HeritageLeveraging Android's Linux Heritage
Leveraging Android's Linux Heritage
 
Android Internals
Android InternalsAndroid Internals
Android Internals
 

10 ways to improve your Android app performance