SlideShare a Scribd company logo
Writing Android Libraries
Emanuele Zattin
@emanuelez
April 9, 2015
DroidCon Italy
Emanuele Zattin
Born and raised in Padova, Italy
Living in Copenhagen, Denmark since 2005
Automation enthusiast
Jenkins CI contributor since 2010
Java developer at Realm
About me
Embedded on-device mobile database
Easy — saves app makers months of time
Cross-platform — iOS & Android for now. More are coming
Fast — way faster than existing solutions
About Realm
Modularity
Reusability
Vanity
Why would you want to write a library?
Your code depends on Android idioms:
• UI
• Looper/Handler
• Sensors
• Native code
• Many more!
Why writing an Android library?
How hard can it be right?
Just fire Android Studio up and start a new project!
Step one: Getting started!
Android Studio supports the creation of:
Step one: Getting started!
Application Library
New project
✓ ✗
New module
✓ ✓
Option 1: Android Studio
1. Create a new application project
2. Add a library module
3. Remove the application module
Step one: Getting started!
Option 2: The Command Line
$ android create lib-project -t 1 -k it.droidcon.awesomelib -p . -g -v 1.1.3
-t: target (Use android  list  targets to get a list of target ids)
-k: package name
-p: path to the project
-g: make it a Gradle project (requires SDK >= 19)
-v: version of the Android Gradle plugin to use
Step one: Getting started!
Step one: Getting started!
Step two: Code, code, code!
API Design
It’s a huge subject that goes far beyond this presentation but here
are some pointers and reference:
Effective Java 2, Joshua Bloch
How To Design A Good API and Why it Matters, Joshua Bloch
Step two: Code, code, code!
Characteristics of a good API (Joshua Bloch)
• Easy to learn
• Easy to use, even without documentation
• Hard to misuse
• Easy to read and maintain code that uses it
• Sufficiently powerful to satisfy requirements
• Easy to extend
• Appropriate to audience
Step two: Code, code, code!
Testing is universally important but even more so for libraries
Testing an Android library is just like testing an Android application
Yes, JUnit 3 is not so good and lacks a particularly important
feature when testing libraries: parametric tests
Luckily there’s a solution: Burst
Step three: Test
Automate your tests!
Jenkins is a wonderful tool for this.
It offers more than one thousand plugins some of which
specialised for Android development.
Step three: Test
Some must-have Jenkins plugins include:
• Job Config History plugin
• Git plugin
• Gradle plugin
• Android Emulator plugin
• Jenkins comes with JUnit and Matrix job support out of the box
Step three: Test
Another useful way to test your library (and showcase it) is to write
one or more example apps.
Running monkey on the app ensures it doesn’t suffer from crashes
and ANRs
A useful Gradle plugin:

https://github.com/novoda/gradle-android-command-plugin
Step three: Test
Aar vs Jar
• Aar is supported by Gradle and Android Studio
• Aar is not supported by Ant and Eclipse
• Using local Aar files is not trivial
• Do you want to support Eclipse? Use Jar!
Step four: Publish
The Android Gradle plugin will generate an Aar file
How to generate a Jar instead?

The Aar actually contains our Jar already!
task generateJar(type: Copy) {
group 'Build'
description 'blah blah...'
dependsOn assemble
from 'build/intermediates/bundles/release/classes.jar'
into 'build/libs'
rename('classes.jar', 'awesome-library.jar')
}
Step four: Publish
Where to publish?
Step four: Publish
Bintray requires a source Jar
task androidSourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
}
Step four: Publish
Bintray also requires a Javadoc Jar
android.libraryVariants.all { variant ->
task("javadoc${variant.name.capitalize()}", type: Javadoc) {
description "Generates Javadoc for $variant.name."
group 'Docs'
source = variant.javaCompile.source
ext.androidJar = files(plugins
.findPlugin(“com.android.library")
.getBootClasspath())
classpath = files(variant.javaCompile.classpath.files) +
ext.androidJar
exclude '**/BuildConfig.java'
exclude '**/R.java'
}
}
Step four: Publish
Bintray also provides a Gradle plugin for the actual publishing
https://github.com/bintray/gradle-bintray-plugin
The configuration is not trivial, and in the beginning it might be
easier to just do the release manually on the binary website
Step four: Publish
Annotation processing is a functionality of javac used for scanning
and processing annotations at compile time
You can write your own annotation processor.
Problems that annotation processing is good at solving:
• Boilerplate removal
• Introspection removal
Advanced Topics: Annotation Processor
Popular Android libraries using annotation processing:
• Dagger
• Butter Knife
• Autovalue/Autoparcel
• Realm
Advanced Topics: Annotation Processor
Bad news!
The Android API does not support the 

javax.annotation.processing package
Advanced Topics: Annotation Processor
Create two new java sub-projects:
• annotations (used both by the library and the processor)
• annotations processor
Advanced Topics: Annotation Processor
The Jar task will need to be modified:
task androidJar(type: Jar) {
dependsOn assemble
group 'Build'
description ‘blah blah’
from zipTree(
'build/intermediates/bundles/release/classes.jar')
from zipTree(
'../annotations-processor/build/libs/processor.jar')
from zipTree(
'../annotations/build/libs/annotations.jar')
}
Advanced Topics: Annotation Processor
The javadoc tasks will also have to be modified:
android.libraryVariants.all { variant ->
task("javadoc${variant.name.capitalize()}", type: Javadoc) {
description "Generates Javadoc for $variant.name."
group 'Docs'
source = variant.javaCompile.source
source "../annotations/src/main/java"
ext.androidJar = files(plugins
.findPlugin(“com.android.library")
.getBootClasspath())
classpath = files(variant.javaCompile.classpath.files)
+ ext.androidJar
exclude '**/BuildConfig.java'
exclude '**/R.java'
}
}
Advanced Topics: Annotation Processor
NDK
Advanced Topics: Native code
WARNING  [Project:  :lib]  
Current  NDK  support  is  deprecated.  
Alternative  will  be  provided  in  the  future.
Advanced Topics: Native code
Advanced Topics: Native code
Solution 1
Ignore Google’s warning and keep using the Gradle NDK support.
It works* but there is one missing feature: no ldFlags
ndk  {  
      moduleName  "sanangeles"  
      cFlags  "-­‐DANDROID_NDK  -­‐DDISABLE_IMPORTGL"  
      ldLibs  "GLESv1_CM",  "dl",  "log"  
      stl  "stlport_static"  
}  
*for some definition of “works”
Advanced Topics: Native code
Solution 2
Gradle native plugin
Gotchas:
• Requires extra logic to handle standalone toolchains
• It might soon become obsolete
Advanced Topics: Native code
How to include the native libraries in the Jar file?
$ tree
.
!"" META-INF
#   $"" MANIFEST.MF
!"" com
#   $"" amazing-library
#   $"" AmazingLibrary.class
$"" lib
!"" armeabi
#   $"" amazing-library.so
!"" armeabi-v7a
#   $"" amazing-library.so
$"" x86
$"" amazing-library.so
Advanced Topics: Native code
How to generate the jar file in Gradle?
task  androidJar(type:  Jar,  dependsOn:  ['assemble'])  {  
      group  'Build'  
      description  ‘blah  blah'  
      from  zipTree('build/intermediates/bundles/release/classes.jar')  
      from(file('src/main/jniLibs'))  {  
            into  'lib'  
      }  
}  
Advanced Topics: Native code
• Embrace Gradle
• Explore Gradle plugins
• Automate your tests
• Bintray is the go-to solution for publishing
• Writing libraries rocks!
Takeaways
Questions?

More Related Content

PPTX
Reverse engineering android apps
PPT
Reverse Engineering Android Application
PDF
Introduction to the Android NDK
PDF
NDK Programming in Android
PDF
Android reverse engineering - Analyzing skype
PDF
How to reverse engineer Android applications
PDF
Learn How to Unit Test Your Android Application (with Robolectric)
PDF
Unit testing and Android
Reverse engineering android apps
Reverse Engineering Android Application
Introduction to the Android NDK
NDK Programming in Android
Android reverse engineering - Analyzing skype
How to reverse engineer Android applications
Learn How to Unit Test Your Android Application (with Robolectric)
Unit testing and Android

What's hot (17)

PDF
Introduction to the Android NDK
PDF
Android reverse engineering: understanding third-party applications. OWASP EU...
PPTX
PPT
Steelcon 2015 Reverse-Engineering Obfuscated Android Applications
PDF
Android Native Development Kit
PDF
Droidcon Greece '15 - Reverse Engineering in Android: Countermeasures and Tools
PPTX
Native development kit (ndk) introduction
PPSX
Intoduction to java
PDF
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
PDF
基於 Flow & Path 的 MVP 架構
PPT
Java basics
PDF
Android NDK: Entrando no Mundo Nativo
PDF
Testing on Android
PDF
Android talks #08 decompiling android applications
PPT
Introduction to Java Programming, Basic Structure, variables Data type, input...
PDF
(CISC 2013) Real-Time Record and Replay on Android for Malware Analysis
PPTX
Building next gen android library with gradle
Introduction to the Android NDK
Android reverse engineering: understanding third-party applications. OWASP EU...
Steelcon 2015 Reverse-Engineering Obfuscated Android Applications
Android Native Development Kit
Droidcon Greece '15 - Reverse Engineering in Android: Countermeasures and Tools
Native development kit (ndk) introduction
Intoduction to java
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
基於 Flow & Path 的 MVP 架構
Java basics
Android NDK: Entrando no Mundo Nativo
Testing on Android
Android talks #08 decompiling android applications
Introduction to Java Programming, Basic Structure, variables Data type, input...
(CISC 2013) Real-Time Record and Replay on Android for Malware Analysis
Building next gen android library with gradle
Ad

Similar to Writing Android Libraries (20)

PDF
[113] lessons from realm
PDF
Get started with AAR
PDF
Антон Руткевич, Сборка Android-библиотеки нового поколения с помощью Gradle
PDF
Where All Libraries & Data Required For Android App Development Are Present
PPT
Creating your own Android library and documenting it with Javadocs
PDF
Android Development 201
PPTX
PPTX
Android developer's toolbox
PDF
Using the Android Native Development Kit (NDK)
PPTX
Android ndk - Introduction
PPTX
Using the android ndk - DroidCon Paris 2014
PDF
Андрей Володин — Как подружиться с роботом
PPTX
Lecture android best practices
PPTX
It's always your fault. Poznań ADG 2016
PDF
Innovation Generation - The Mobile Meetup: Android Best Practices
PDF
Groovy on Android
PPTX
Getting started with the NDK
PPTX
It's always your fault
PPTX
Mobile Application Development- Configuration and Android Installation
PDF
To ∞ (~65K) and beyond! - Sebastiano Gottardo - Codemotion Milan 2016
[113] lessons from realm
Get started with AAR
Антон Руткевич, Сборка Android-библиотеки нового поколения с помощью Gradle
Where All Libraries & Data Required For Android App Development Are Present
Creating your own Android library and documenting it with Javadocs
Android Development 201
Android developer's toolbox
Using the Android Native Development Kit (NDK)
Android ndk - Introduction
Using the android ndk - DroidCon Paris 2014
Андрей Володин — Как подружиться с роботом
Lecture android best practices
It's always your fault. Poznań ADG 2016
Innovation Generation - The Mobile Meetup: Android Best Practices
Groovy on Android
Getting started with the NDK
It's always your fault
Mobile Application Development- Configuration and Android Installation
To ∞ (~65K) and beyond! - Sebastiano Gottardo - Codemotion Milan 2016
Ad

Recently uploaded (20)

PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
Odoo POS Development Services by CandidRoot Solutions
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PPTX
history of c programming in notes for students .pptx
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
medical staffing services at VALiNTRY
PPTX
ai tools demonstartion for schools and inter college
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPT
Introduction Database Management System for Course Database
2025 Textile ERP Trends: SAP, Odoo & Oracle
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Upgrade and Innovation Strategies for SAP ERP Customers
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Odoo POS Development Services by CandidRoot Solutions
ISO 45001 Occupational Health and Safety Management System
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
history of c programming in notes for students .pptx
PTS Company Brochure 2025 (1).pdf.......
medical staffing services at VALiNTRY
ai tools demonstartion for schools and inter college
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
Understanding Forklifts - TECH EHS Solution
VVF-Customer-Presentation2025-Ver1.9.pptx
Introduction Database Management System for Course Database

Writing Android Libraries

  • 1. Writing Android Libraries Emanuele Zattin @emanuelez April 9, 2015 DroidCon Italy
  • 2. Emanuele Zattin Born and raised in Padova, Italy Living in Copenhagen, Denmark since 2005 Automation enthusiast Jenkins CI contributor since 2010 Java developer at Realm About me
  • 3. Embedded on-device mobile database Easy — saves app makers months of time Cross-platform — iOS & Android for now. More are coming Fast — way faster than existing solutions About Realm
  • 5. Your code depends on Android idioms: • UI • Looper/Handler • Sensors • Native code • Many more! Why writing an Android library?
  • 6. How hard can it be right? Just fire Android Studio up and start a new project! Step one: Getting started!
  • 7. Android Studio supports the creation of: Step one: Getting started! Application Library New project ✓ ✗ New module ✓ ✓
  • 8. Option 1: Android Studio 1. Create a new application project 2. Add a library module 3. Remove the application module Step one: Getting started!
  • 9. Option 2: The Command Line $ android create lib-project -t 1 -k it.droidcon.awesomelib -p . -g -v 1.1.3 -t: target (Use android  list  targets to get a list of target ids) -k: package name -p: path to the project -g: make it a Gradle project (requires SDK >= 19) -v: version of the Android Gradle plugin to use Step one: Getting started!
  • 10. Step one: Getting started!
  • 11. Step two: Code, code, code!
  • 12. API Design It’s a huge subject that goes far beyond this presentation but here are some pointers and reference: Effective Java 2, Joshua Bloch How To Design A Good API and Why it Matters, Joshua Bloch Step two: Code, code, code!
  • 13. Characteristics of a good API (Joshua Bloch) • Easy to learn • Easy to use, even without documentation • Hard to misuse • Easy to read and maintain code that uses it • Sufficiently powerful to satisfy requirements • Easy to extend • Appropriate to audience Step two: Code, code, code!
  • 14. Testing is universally important but even more so for libraries Testing an Android library is just like testing an Android application Yes, JUnit 3 is not so good and lacks a particularly important feature when testing libraries: parametric tests Luckily there’s a solution: Burst Step three: Test
  • 15. Automate your tests! Jenkins is a wonderful tool for this. It offers more than one thousand plugins some of which specialised for Android development. Step three: Test
  • 16. Some must-have Jenkins plugins include: • Job Config History plugin • Git plugin • Gradle plugin • Android Emulator plugin • Jenkins comes with JUnit and Matrix job support out of the box Step three: Test
  • 17. Another useful way to test your library (and showcase it) is to write one or more example apps. Running monkey on the app ensures it doesn’t suffer from crashes and ANRs A useful Gradle plugin:
 https://github.com/novoda/gradle-android-command-plugin Step three: Test
  • 18. Aar vs Jar • Aar is supported by Gradle and Android Studio • Aar is not supported by Ant and Eclipse • Using local Aar files is not trivial • Do you want to support Eclipse? Use Jar! Step four: Publish
  • 19. The Android Gradle plugin will generate an Aar file How to generate a Jar instead?
 The Aar actually contains our Jar already! task generateJar(type: Copy) { group 'Build' description 'blah blah...' dependsOn assemble from 'build/intermediates/bundles/release/classes.jar' into 'build/libs' rename('classes.jar', 'awesome-library.jar') } Step four: Publish
  • 20. Where to publish? Step four: Publish
  • 21. Bintray requires a source Jar task androidSourcesJar(type: Jar) { from android.sourceSets.main.java.srcDirs } Step four: Publish
  • 22. Bintray also requires a Javadoc Jar android.libraryVariants.all { variant -> task("javadoc${variant.name.capitalize()}", type: Javadoc) { description "Generates Javadoc for $variant.name." group 'Docs' source = variant.javaCompile.source ext.androidJar = files(plugins .findPlugin(“com.android.library") .getBootClasspath()) classpath = files(variant.javaCompile.classpath.files) + ext.androidJar exclude '**/BuildConfig.java' exclude '**/R.java' } } Step four: Publish
  • 23. Bintray also provides a Gradle plugin for the actual publishing https://github.com/bintray/gradle-bintray-plugin The configuration is not trivial, and in the beginning it might be easier to just do the release manually on the binary website Step four: Publish
  • 24. Annotation processing is a functionality of javac used for scanning and processing annotations at compile time You can write your own annotation processor. Problems that annotation processing is good at solving: • Boilerplate removal • Introspection removal Advanced Topics: Annotation Processor
  • 25. Popular Android libraries using annotation processing: • Dagger • Butter Knife • Autovalue/Autoparcel • Realm Advanced Topics: Annotation Processor
  • 26. Bad news! The Android API does not support the 
 javax.annotation.processing package Advanced Topics: Annotation Processor
  • 27. Create two new java sub-projects: • annotations (used both by the library and the processor) • annotations processor Advanced Topics: Annotation Processor
  • 28. The Jar task will need to be modified: task androidJar(type: Jar) { dependsOn assemble group 'Build' description ‘blah blah’ from zipTree( 'build/intermediates/bundles/release/classes.jar') from zipTree( '../annotations-processor/build/libs/processor.jar') from zipTree( '../annotations/build/libs/annotations.jar') } Advanced Topics: Annotation Processor
  • 29. The javadoc tasks will also have to be modified: android.libraryVariants.all { variant -> task("javadoc${variant.name.capitalize()}", type: Javadoc) { description "Generates Javadoc for $variant.name." group 'Docs' source = variant.javaCompile.source source "../annotations/src/main/java" ext.androidJar = files(plugins .findPlugin(“com.android.library") .getBootClasspath()) classpath = files(variant.javaCompile.classpath.files) + ext.androidJar exclude '**/BuildConfig.java' exclude '**/R.java' } } Advanced Topics: Annotation Processor
  • 31. WARNING  [Project:  :lib]   Current  NDK  support  is  deprecated.   Alternative  will  be  provided  in  the  future. Advanced Topics: Native code
  • 33. Solution 1 Ignore Google’s warning and keep using the Gradle NDK support. It works* but there is one missing feature: no ldFlags ndk  {        moduleName  "sanangeles"        cFlags  "-­‐DANDROID_NDK  -­‐DDISABLE_IMPORTGL"        ldLibs  "GLESv1_CM",  "dl",  "log"        stl  "stlport_static"   }   *for some definition of “works” Advanced Topics: Native code
  • 34. Solution 2 Gradle native plugin Gotchas: • Requires extra logic to handle standalone toolchains • It might soon become obsolete Advanced Topics: Native code
  • 35. How to include the native libraries in the Jar file? $ tree . !"" META-INF #   $"" MANIFEST.MF !"" com #   $"" amazing-library #   $"" AmazingLibrary.class $"" lib !"" armeabi #   $"" amazing-library.so !"" armeabi-v7a #   $"" amazing-library.so $"" x86 $"" amazing-library.so Advanced Topics: Native code
  • 36. How to generate the jar file in Gradle? task  androidJar(type:  Jar,  dependsOn:  ['assemble'])  {        group  'Build'        description  ‘blah  blah'        from  zipTree('build/intermediates/bundles/release/classes.jar')        from(file('src/main/jniLibs'))  {              into  'lib'        }   }   Advanced Topics: Native code
  • 37. • Embrace Gradle • Explore Gradle plugins • Automate your tests • Bintray is the go-to solution for publishing • Writing libraries rocks! Takeaways