Build and publish your android
custom component as a library
@bashizip
#DroidConKE 2019
Slide 2
Slide 2 text
Patrick Bashizi
bashizip
Slide 3
Slide 3 text
Who I am
● Senior Consultant, Software Engineer
● Write code for Gov & NGO
● Trainer
● Java Ecosystem mainly
● Android Developer & Advocate since 2011
● GDG Kinshasa Lead
● Frenchie...but can speak swahili and a bit of English
Slide 4
Slide 4 text
1. How to create an Android custom view library
2. How to publish your Android library
3. How to use your Android library
4. Best practices around building Android libraries
Summary
Slide 5
Slide 5 text
APK vs AAR
Same structure but :
● Android App Module compiles to APK
● Android Library compiles to AAR
Slide 6
Slide 6 text
Why building / using Libraries ?
● Code reuse
● Innovation
● Performance
● Open source contribution
Slide 7
Slide 7 text
So Why custom components ( View ) ?
● Performance. If you have a lots of views in your layout and you want
to optimize it by drawing a single custom view to make it lighter.
● A big view hierarchy that is complex to operate and support.
● A complete custom view that requires manually drawing.
Slide 8
Slide 8 text
Your need ?
- A special Button, Textview, ImageView ?
- A special LinearLayout, RelativeLayout ?
- A custom View from scratch ?
- A custom ViewGroup from Scratch?
Slide 9
Slide 9 text
No content
Slide 10
Slide 10 text
Building our custom View :
MoodySmiley
1. Create Android Project with git integration
2. Create a Library Module
3. Extends android.view.View or ViewGroup
4. Declare variables / attributes
5. Override @OnDraw( )
6. Override @OnLayout, @OnMesure ( If needed )
7. Manage styling attributes
Slide 11
Slide 11 text
Show me the code !
Source code : https://github.com/bashizip/moodysmiley-demo
Slide 12
Slide 12 text
Publishing & Using
(code lab)
Slide 13
Slide 13 text
No content
Slide 14
Slide 14 text
Some Best Practices (1/6)
● Intuitive: It should do what a user of the library expects it to do without having to look up
the documentation.
● Consistent: The code for the Android library should be well thought out and should not
change drastically between versions. Follows semantic versioning.
● Easy to use, hard to misuse: It should be easily understandable in terms of
implementation and its usage.
Slide 15
Slide 15 text
Some Best Practices (2/6)
● Avoid multiple arguments
Slide 16
Slide 16 text
Some Best Practices (3/6)
● Minimize permissions
Remember : Every permission you add to your Android library’s AndroidManifest.xml file will
get merged into the app that adds the Android library as a dependency !
Do not let your code crash just because you do not have a particular permission. Use this
method to check if a particular permission granted or not:
Slide 17
Slide 17 text
Some Best Practices (4/6)
● Minimize requisites
This will hide the app in the Play Store for devices that do not have Bluetooth !
Just because the app added your library !
Slide 18
Slide 18 text
Some Best Practices (5/6)
Instead use the following code snippet to detect the feature from your library during runtime and
enable/disable feature accordingly
Slide 19
Slide 19 text
Some Best Practices (6/6)
● Support different versions : minSdkVersion ${theLowestPossible}
● Provide documentation : README, Wiki, JavaDoc, etc
● Bundle a simplistic app sample to showcase how to use your library
Slide 20
Slide 20 text
Where to Go From Here?
Android Custom View deep dive :
Huyen Tue Dao
https://academy.realm.io/posts/360andev-huyen-tue-dao-measure-layout-draw-repeat-custom-views-and-viewgroups-and
roid/
Chiu-Ki-Chan
http://chiuki.github.com/deep-dive-android-custom-components
Best Practices
https://blog.octo.com/en/android-library-development-best-practices-guide/