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!

10 ways to improve your Android app performance