Post on 15-Jul-2015
transcript
© Sean Katz
Best ANDROID DevelopmentPractices
1
© Sean Katz
Hello!I AM SEAN KATZ
I love (Android) developmentHope you’ll love it too.
- Software Dev Manager @ Autodesk- Previously Co-Founder @ for-each- 10 Years work in software dev- Passionate about Mobile & Web- Father of Mini
2
© Sean Katz
SUBJECTS TODAY
1. Architecture2. Gradle3. Android Studio4. Git5. Manifest6. APIs & Support Lib7. App Code Skeletons
1. Boilerplate code2. Performance3. Debug & Measure4. CI5. Security6. Graphical Design
3
© Sean Katz
What is Tipz.io ?
○ 2 in 1:● Bite-Size Tips platform for acquiring skills● Open Source Initiative for educational purposes
○ Contribute! Web, Android & iOS dev, DevOps, UX/UI work,
marketing, branding, SEO… show your skills on tipz!
○ Earn endless love & real-life reputation
4
© Sean Katz
Give a Star on GitHub!
5
1. 2. 3.
© Sean Katz
Tips on learning Tipz - browse tipz.io
6
© Sean Katz
Tips on learning Tipz - browse commits
7
© Sean Katz
Tips on learning Tipz - blame a file
8
© Sean Katz
Tips on learning Tipz - browse pull reqs
9
© Sean Katz
Tips on learning Tipz - continuously learn
10
© Sean Katz
Contributors gets backstage access
11
© Sean Katz
“
FULL BLOWN ARCHITECTURE
Weeks of programming can save you hours of planning
12
© Sean Katz
What is an Android app?
Choose one or more:✓ Activity - display❏ Service - do❏ Content Provider - data❏ Broadcast - communicate
13
© Sean Katz
PLAN ARCHITECTURE
ACTIVITY (or FRAGMENT)
ViewableInteractionsForeground
SERVICE
EngineBlackBoxBackgroundNetwork
Go Fetch content
Broadcast status
CONTENTPROVIDER
DataURIsEncapsulation
Results? Data CRUD
Sync
14
© Sean Katz
“
GRADLE
Computers are good at following instructions, but not at reading your mind
- Donald Knuth
15
© Sean Katz
Hello: GRADLE
● Build system○ Like Grunt○ Forget ‘Ant’
● Groovy● Terminal run
16
© Sean Katz
GRADLE Tipz
1. Always use a gradle wrapper ☞2. Leverage a top-level build.gradle ☞3. Set names to your modules ☞4. Prefer a remote maven dependency ☞5. Always choose dependencies explicitly ☞6. Read the changelog of the Android Build
Tools ☞17
© Sean Katz
“
ANDROID STUDIO
My favorite things in life don't cost any money. It's really clear that the most precious resource we all have is time
- Steve Jobs
18
© Sean Katz
Hello: ANDROID STUDIO
19
© Sean Katz
ANDROID STUDIO Tipz
1. Install plugins ☞2. Color your logcat ☞3. Use Android tools4. Enforce code styles ☞5. Inspect code + Lint6. Know (and use) code templates ☞
20
© Sean Katz
“
GIT
Coming together is a beginning. Keeping together is progress. Working together is success
- Henry Ford
21
© Sean Katz
Hello: GIT
● Popular SCM● Branch away● Collaborate● Code Review● Open Source
22
© Sean Katz
Hello: GIT & SourceTree
23
© Sean Katz
Hello: GIT flow
● Master● Develop● Feature/*● Release/*● Hotfix/*
24
© Sean Katz
GIT Tipz
1. Your git should hold all the files needed to compile the project from scratch ☞
2. Always start with a gitignore file first ☞3. Don't invent 'gitignore', Look for an
already published gitignore for various use cases ☞
25
© Sean Katz
“The Internet is the first thing that humanity has built that humanity doesn't understand, the largest experiment in anarchy that we have ever had
MANIFEST
- Eric Schmidt
26
© Sean Katz
Hello: MANIFEST
27
© Sean Katz
MANIFEST Tipz
1. Meet Android Dashboard ☞2. Manifest's Package name vs build.
gradle's Application ID ☞3. Carefully add new permissions ☞4. Think About android:allowBackup ☞5. Guard your components with android:
exported='false' ☞28
© Sean Katz
“By failing to prepare, you are preparing to fail
ANDROID APIs & SUPPORT LIBRARY
- Benjamin Franklin
29
© Sean Katz
ANDROID APIsCode name Version API level
Lollipop 5.0 21
KitKat 4.4.x 19
Jelly Bean 4.1.x, 4.2.x, 4.3.x 16 - 18
Ice Cream Sandwich 4.0.3, 4.0.4 15
Ice Cream Sandwich 4.0.1, 4.0.2 14
Honeycomb 3.x 11-13
Froyo, Gingerbread 2.2.x - 2.3.x 8-10
Cupcake, Donut, Eclair 1.x - 2.1.x 1-7
Minimum API
< ~8% users
Target APICompiled SDK
30
© Sean Katz
❤ SUPPORT LIBRARYCorresponding Features from support API level
● getExternalStorageState● Notification (additions - wear, car, ...)
21
● ConnectivityManager● Notification (additions)● Nested Fragments (v4) (issue)
16 - 18
● Fragment● ActionBar, ActionBarDrawerToggle● Material design, Toolbar● ViewPager, FragmentPagerAdapter● LocalBroadcastManager● Loader● Share, ShareActionProvider● SearchView● GridLayout
14
11-13
8-10
1-7
Minimum API
< ~8% users
Target APICompiled SDK
31
© Sean Katz
❤ SUPPORT LIBRARY - standalonesCorresponding Features from support API level
● DrawerLayout● SlidingPaneLayout● PrintHelper● SwipeRefreshLayout ● CardView● RecyclerView● Palette● Multidex
Standalone
© Sean Katz
APIs & SUPPORT LIB Tipz
1. Always compile to the latest SDK ver ☞2. Selecting the Android version you
support (minSdkVersion) ☞3. Prefer Support Library over built in APIs
(even if your minimum API allows it) ☞
33
© Sean Katz
“Simplicity is the ultimate form of sophistication
APP CODE SKELETONS
- Leonardo da Vinci
34
© Sean Katz
APPLICATION CLASS Tipz
1. Use Application class, don't abuse ☞2. Don't use class as a single tone ☞3. Don't do lengthy operations ☞4. Assume the Application class values
never gets deleted ☞
35
© Sean Katz
LIFECYCLE Tipz
1. Create a Launcher Activity ☞2. A runtime check for version upgrade ☞3. If you need to know when your Activity is
in resume mode ☞4. Fragments are automatically recreated
upon Activity re-create ☞5. Pick your Fragment: Static or Dynamic ☞
36
© Sean Katz
SERVICES, PROVIDERS & DB Tipz
1. Provide an easy instantiation to your component ☞
2. DB operations are heavy ☞3. You probably should use IntentService ☞
37
© Sean Katz
SHARED PREFS Tipz
https://github.com/tipz/tipz-android/pull/1/1. Shared Preference commit() is not the
only option2. Smartly organized SharedPreferences3. Do not store secrets in the Shared
Preferences (+ encryption example)
38
© Sean Katz
GENERAL CODE Tipz
1. Adapter view data is always there ☞2. Keep your logs tidy ☞3. Goodbye old List/Grid and Adapters,
Hello RecyclerView ☞4. Request Resources and not Context ☞5. When in need of debug sections, use
BuildConfig.DEBUG ☞39
© Sean Katz
“Measuring programming progress by lines of code is like measuring aircraft building progress by weight
BOILERPLATE CODE
- Bill Gates
40
© Sean Katz
Hello: BOILERPLATE CODE○ BaseFragment - tag, debug, inflate
○ BaseLoadingFragment - progress views○ BaseDataFragment - connects to data
○ BaseDataAdapterFragment - w/ adapter○ BaseDataAdapterPagingFragment
○ Service○ IntentService - off-the-ui-thread, queue
○ WakefulIntentService - stays awake○ BaseService - broadcasts, access ease
○ ContentService - provider, REST api’s
41
© Sean Katz
BOILERPLATE Samplez
1. “Copy and paste is a design error”2. BaseDataFragment ☞3. RecyclerViewAdapter ☞4. BaseContentProvider (+DB) ☞5. BaseActivity ☞
42
© Sean Katz
“It's hardware that makes a machine fast. It's software that makes a fast machine slow
PERFORMANCE
- Craig Bruce
43
© Sean Katz
1. Context and memory leaks ☞2. Avoid Internal Getters/Setters ☞3. Minimize view hierarchies4. Don’t overdraw5. Let the pros handle images ☞6. Bulk/Transactional inserts are faster ☞
PERFORMANCE Tipz
44
© Sean Katz
“Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
DEBUG & MEASURE TOOLS
- Brian Kernighan
45
© Sean Katz
StrictMode
46
Or, Report to logcat ☞
Activate in developer tools
© Sean Katz
GPU Overdraw
47
No color 0X
Blue 1X
Green 2X
Light Red 3X
Dark Red 4X +
© Sean Katz
Profile GPU Rendering
48
Blue Draw (Java)
Red Process (Render)
Orange Execute (Pipeline)
Green 60 FPS target
© Sean Katz
Don’t keep activities
49
© Sean Katz
Show Layout Bounds (Developer tools)
50
© Sean Katz
Show Layout Bounds (Android Device Monitor)
51
© Sean Katz
Network Statistics
52
Tag network using:android.net.TrafficStats
© Sean Katz
Method Tracing
53
© Sean Katz
Java Heap + MAT
54
© Sean Katz
System Information
1. Activity Manager State2. Package Information3. Memory Usage4. Memory use over time5. Graphics State
55
© Sean Katz
“An iPod, a phone, an internet mobilecommunicator... these are NOT three separatedevices! And we are calling it iPhone! Today Apple is going to reinvent the phone
Continuous Integration
- Steve Jobs56
© Sean Katz
Hello: CI
● Automation○ Trigger○ Build○ Test○ Deploy
● Tools○ Jenkins○ ship.io (SaaS)
57
© Sean Katz
CI Tipz
1. Use a version numbering scheme ☞2. Automated versioning ☞3. Store your debug key in git ☞
58
© Sean Katz
“The only truly secure system is one that is powered off, cast in a block of concrete and sealed in a lead-lined room with armed guards
SECURITY
– Gene Spafford
59
© Sean Katz
SECURITY Tipz
1. Use proguard (see next slide)
2. Silent the logs ☞3. Do not store secrets in storage ☞4. Run Android Lint for security checks5. android:exported='false' ☞6. Safe keep Production signing key ☞
60
© Sean Katz
PROGUARD Tipz
1. Proguard your release, not debug ☞2. What's in the stock Android proguard ☞3. Run Proguard on a frequent basis ☞4. Create separate proguard files ☞5. Planning on using json parsers? ☞6. Proguard for 3rd party libraries ☞
61
© Sean Katz
REVERSE ENGINEERING Tipz
1. Reverse own app for security checkup2. Steps:
a. Rename app.APK to app.ZIPb. extract app.ZIPc. classes.dex -> jar (tool)
3. Output:a. Java files: jar -> clear text java (tool)b. Resources: Folder “res” with the resources
62
© Sean Katz
“There is no reason anyone would want acomputer in their home
GRAPHICAL DESIGN
- Ken Olson, 1977
63
© Sean Katz
Android Design Cheat-Sheet
64
http://possiblemobile.com/wp-content/uploads/2014/01/Android-Design-Cheat-Sheet-highres.png
© Sean Katz
PRODUCTIVE TEAMWORK
1. Productive Teamwork Blog Post ☞a. Understanding the different configurationsb. Planning a dynamic layoutc. Real estate of the screend. Stretching techniques (9 patch)e. Full screen imagesf. Folder and Files naming conventionsg. Marketing (Google Play) assets
65
© Sean Katz
RESOURCES Tipz
1. Naming conventions for your ID's ☞2. You can use '@null' in attributes values ☞3. Carefully use platform drawables:
'@android:drawable/...' ☞4. Custom attributes should use res-auto
xmlns ☞5. Re-use XML layouts with <include.../> ☞
66
© Sean Katz
GENERAL GRAPHICAL Tipz
1. Prefer Toolbar over ActionBar ☞2. Hello CardView ☞3. Use AppCompat themes ☞4. Styles, Themes and dimens = CSS ? ☞
67
© Sean Katz
THANKS!Any questions?
68blog.android-develop.com
@_SeanKatz
sean.katz@gmail.com
www.linkedin.com/in/seankatz
© Sean Katz
Line Icons by Webalys, Virgil Pana and Mirko Monti are published under a Creative Commons Attribution license and Free for both personal and commercial use. You can copy, adapt, remix, distribute or transmit them. If you use these sets on your presentation remember to keep the “Credits” slide or provide a mention and link to these resources:
● Mirko Monti - Simple line icons● Virgil Pana - E-commerce icons● Webalys - Streamline iconset 69
ASSETS CREDITS