An-Droid 101
Developing Libraries
Nishant Srivastava
Android Dev/Interaction Designer
Organizer@GDG New Delhi
Goals
WHAT ?
WHY ?
HOW ?
To answer the below with respect to Android Libraries
What is a Software Library ?
A software library generally consists of pre-written code, classes, procedures, scripts, configuration data and more.
It is designed to assist both the programmer and the programming language compiler in building and executing software.
WHY ?
Developers are lazy
..hence they reuse code.
..because
- Build modular and independent blocks
- Write better softwares
- Speed up compile time
- Easier implementation
- Make code more testable
..and best of em all being ...
Make people happy..
.. OK , I guess BATMAN doesn't smile anyways.. :P
Before software can be reusable it first has to be usable.
- Ralph Johnson
Your code depends on Android idioms/resources
1.UI
2.Looper/Handler
3.Resources
4.Native Code
..and many more...
Why writing an Android library ?
AAR vs JAR
JAR - Java ARchive
.jar == Java library
Contains
java code
Used for
Functionality such as data handling, data manipulation, service integration
AAR - Android ARchive
.aar == Android library
Contains
java code + android resources + android manifest stub
Used for
Functionality that includes everything a jar can + UI
Finally ...a break ..
HOW ?
Step 1 : Gradle
..in their own words..
Emerge from Build Hell today through Gradle, the modern open source polyglot build automation system..
Step 2 : Create Project
How hard can it be ?
Fire up your Android Studio and Create a New Project
Step 2 : {Gradle Build in progress}
Step 3 : Create a new module
Create a new module
Step 3 : Create a new module (..contd)
Go on..
Step 3 : Create a new module (..contd)
We are going to stick to making an AAR module.
Once selected, my suggestion is to rename
- module name to library
- application name to <yourlibraryname>
- Package name to <domain.company.library.yourlibraryname>
Step 3 : Create a new module (..contd)
Step 4 : Crush Code (..literally)
.....and build your awesome library...
Step 4 : {Create and Build your library}
- Entity class declaration
- Do
- Don't
- Do
void init(ApiSecret apisecret);
void init(String apikey, int refresh, long interval, String type);
Make Your Libraries
Stable
- Multi-Thread compatibility
- Singleton implementation
- Synchronization
- Immutable Entity
- Lifecycle mangement
- Object lifetime alignment
Make Your Libraries
Stable
Make Your Libraries
- Intuitive
- Consistent
- Easy to use, Hard to misuse
Easy to Use
public boolean hasPermission(Context context, String permission) {
int result = context.checkCallingOrSelfPermission(permission);
return result == PackageManager.PERMISSION_GRANTED;
}
Minimize Permissions
String feature = PackageManager.FEATURE_BLUETOOTH;
public boolean isFeatureAvailable(Context context, String feature) {
return context.getPackageManager().hasSystemFeature(feature);
}
Minimize Requisites
Things to take care of..
public boolean isMarshmallow(){
return Build.VERSION.SDK_INT>= Build.VERSION_CODES.M;
}
Support different versions
Things to take care of..
Make your code Testable
- Mock
- No static methods
- Avoid final classes
// In code
boolean debuggable = false;
MyAwesomeLibrary.init(debuggable);
// In build.gradle
debuggable = true
Donot log in production
Things to take care of..
Do not crash silently - Log all exceptions and print them in the logcat
Handle poor network conditions
Reluctance to include large libraries
dependencies {
provided 'com.squareup.okhttp3:okhttp:3.1.2'
}
Donot require libraries
Things to take care of..
Check in classpath
private boolean hasOKHttpOnClasspath() {
try {
Class.forName("ccom.squareup.okhttp3.OkHttpClient");
return true;
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return false;
}
..simply put follow the rule of SPOIL-ing your Library ..
Simple - Briefly and Clearly expressed
Purposeful - Having or showing resolve
OpenSource - Universal Access, Free license
Idiomatic - Natural to the native environment
Logical - Clear, Sound Reasoning
Step 5 : {Create and Build your library}
$ ./gradle build
or
$ ./gradlew build
Step 6
http://developer.android.com/tools/testing/testing_android.html
Write & Use Unit Tests
Step 7
$ gradle clean connectedCheck
Run Your Tests
...and if you don't ...this is what happens..
Step 8
Readme.md
JavaDocs
Example Apps
Document
Step 9
Maven Central / Jcenter
Local/Remote Maven Repositories
Local AAR
Publish
Use your library in another project
....So you have built you awesome library, huh !
So the only thing left now is understanding how to use it in another project, right...
If you are not familiar with the way you include a dependency/library in your project via using gradle then here it is :
dependencies {
compile 'com.company.library:myawesomelibrary:23.1.1'
}
Libraries are a necessary evil
...and thats it for today folks!
Me after this session..literally :P