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

What's hot

Introduction to the Android NDK
Introduction to the Android NDKIntroduction to the Android NDK
Introduction to the Android NDK
Sebastian Mauer
 
Android reverse engineering: understanding third-party applications. OWASP EU...
Android reverse engineering: understanding third-party applications. OWASP EU...Android reverse engineering: understanding third-party applications. OWASP EU...
Android reverse engineering: understanding third-party applications. OWASP EU...
Internet Security Auditors
 
Steelcon 2015 Reverse-Engineering Obfuscated Android Applications
Steelcon 2015 Reverse-Engineering Obfuscated Android ApplicationsSteelcon 2015 Reverse-Engineering Obfuscated Android Applications
Steelcon 2015 Reverse-Engineering Obfuscated Android Applications
Tom Keetch
 
Android Native Development Kit
Android Native Development KitAndroid Native Development Kit
Android Native Development Kit
Peter R. Egli
 
Droidcon Greece '15 - Reverse Engineering in Android: Countermeasures and Tools
Droidcon Greece '15 - Reverse Engineering in Android: Countermeasures and ToolsDroidcon Greece '15 - Reverse Engineering in Android: Countermeasures and Tools
Droidcon Greece '15 - Reverse Engineering in Android: Countermeasures and Tools
Dario Incalza
 
Native development kit (ndk) introduction
Native development kit (ndk)  introductionNative development kit (ndk)  introduction
Native development kit (ndk) introduction
Rakesh Jha
 
Intoduction to java
Intoduction to javaIntoduction to java
Intoduction to java
jalinder123
 
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
ProbeDroid - Crafting Your Own Dynamic Instrument Tool on Android for App Beh...
ZongXian Shen
 
基於 Flow & Path 的 MVP 架構
基於 Flow & Path 的 MVP 架構基於 Flow & Path 的 MVP 架構
基於 Flow & Path 的 MVP 架構
玄武 Wu
 
Java basics
Java basicsJava basics
Java basics
suraj pandey
 
Android NDK: Entrando no Mundo Nativo
Android NDK: Entrando no Mundo NativoAndroid NDK: Entrando no Mundo Nativo
Android NDK: Entrando no Mundo Nativo
Eduardo Carrara de Araujo
 
Testing on Android
Testing on AndroidTesting on Android
Testing on Android
Ari Lacenski
 
Android talks #08 decompiling android applications
Android talks #08   decompiling android applicationsAndroid talks #08   decompiling android applications
Android talks #08 decompiling android applications
Infinum
 
Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...Introduction to Java Programming, Basic Structure, variables Data type, input...
Introduction to Java Programming, Basic Structure, variables Data type, input...
Mr. Akaash
 
(CISC 2013) Real-Time Record and Replay on Android for Malware Analysis
(CISC 2013) Real-Time Record and Replay on Android for Malware Analysis(CISC 2013) Real-Time Record and Replay on Android for Malware Analysis
(CISC 2013) Real-Time Record and Replay on Android for Malware Analysis
ZongXian Shen
 
Building next gen android library with gradle
Building next gen android library with gradleBuilding next gen android library with gradle
Building next gen android library with gradleAnton Rutkevich
 

What's hot (17)

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

Similar to Writing Android Libraries

[113] lessons from realm
[113] lessons from realm[113] lessons from realm
[113] lessons from realm
NAVER D2
 
Android 101 - Introduction to Android Development
Android 101 - Introduction to Android DevelopmentAndroid 101 - Introduction to Android Development
Android 101 - Introduction to Android Development
Andy Scherzinger
 
Introduction to JIB and Google Cloud Run
Introduction to JIB and Google Cloud RunIntroduction to JIB and Google Cloud Run
Introduction to JIB and Google Cloud Run
Saiyam Pathak
 
Session 02 - Elements of Java Language
Session 02 - Elements of Java LanguageSession 02 - Elements of Java Language
Session 02 - Elements of Java Language
PawanMM
 
Elements of Java Language
Elements of Java Language Elements of Java Language
Elements of Java Language
Hitesh-Java
 
Gradle
GradleGradle
Android - Getting started with Android
Android - Getting started with Android Android - Getting started with Android
Android - Getting started with Android
Vibrant Technologies & Computers
 
Eclipse Demo Camp Bangalore 2009 - JSDT
Eclipse Demo Camp Bangalore 2009 - JSDTEclipse Demo Camp Bangalore 2009 - JSDT
Eclipse Demo Camp Bangalore 2009 - JSDTdeepakazad
 
Next Step, Android Studio!
Next Step, Android Studio!Next Step, Android Studio!
Next Step, Android Studio!
Édipo Souza
 
Introduction to Android- A session by Sagar Das
Introduction to Android-  A session by Sagar DasIntroduction to Android-  A session by Sagar Das
Introduction to Android- A session by Sagar Das
dscfetju
 
Android Internal Library Management
Android Internal Library ManagementAndroid Internal Library Management
Android Internal Library Management
Kelly Shuster
 
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
Eric Smalling
 
Java in a world of containers
Java in a world of containersJava in a world of containers
Java in a world of containers
Docker, Inc.
 
Java in a World of Containers - DockerCon 2018
Java in a World of Containers - DockerCon 2018Java in a World of Containers - DockerCon 2018
Java in a World of Containers - DockerCon 2018
Arun Gupta
 
JavaFX - Sketch Board to Production
JavaFX - Sketch Board to ProductionJavaFX - Sketch Board to Production
JavaFX - Sketch Board to Production
Yoav Aharoni
 
Building Top-Notch Androids SDKs
Building Top-Notch Androids SDKsBuilding Top-Notch Androids SDKs
Building Top-Notch Androids SDKs
relayr
 
[Ultracode Munich #4] Short introduction to the new Android build system incl...
[Ultracode Munich #4] Short introduction to the new Android build system incl...[Ultracode Munich #4] Short introduction to the new Android build system incl...
[Ultracode Munich #4] Short introduction to the new Android build system incl...
BeMyApp
 
Java basic
Java basicJava basic
Java basic
Arati Gadgil
 
Core Android
Core AndroidCore Android
Core Android
Dominik Helleberg
 

Similar to Writing Android Libraries (20)

[113] lessons from realm
[113] lessons from realm[113] lessons from realm
[113] lessons from realm
 
Android 101 - Introduction to Android Development
Android 101 - Introduction to Android DevelopmentAndroid 101 - Introduction to Android Development
Android 101 - Introduction to Android Development
 
Introduction to JIB and Google Cloud Run
Introduction to JIB and Google Cloud RunIntroduction to JIB and Google Cloud Run
Introduction to JIB and Google Cloud Run
 
Session 02 - Elements of Java Language
Session 02 - Elements of Java LanguageSession 02 - Elements of Java Language
Session 02 - Elements of Java Language
 
Elements of Java Language
Elements of Java Language Elements of Java Language
Elements of Java Language
 
Gradle
GradleGradle
Gradle
 
Android - Getting started with Android
Android - Getting started with Android Android - Getting started with Android
Android - Getting started with Android
 
Eclipse Demo Camp Bangalore 2009 - JSDT
Eclipse Demo Camp Bangalore 2009 - JSDTEclipse Demo Camp Bangalore 2009 - JSDT
Eclipse Demo Camp Bangalore 2009 - JSDT
 
Next Step, Android Studio!
Next Step, Android Studio!Next Step, Android Studio!
Next Step, Android Studio!
 
Introduction to Android- A session by Sagar Das
Introduction to Android-  A session by Sagar DasIntroduction to Android-  A session by Sagar Das
Introduction to Android- A session by Sagar Das
 
Android Internal Library Management
Android Internal Library ManagementAndroid Internal Library Management
Android Internal Library Management
 
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
IBM Index 2018 Conference Workshop: Modernizing Traditional Java App's with D...
 
Java in a world of containers
Java in a world of containersJava in a world of containers
Java in a world of containers
 
Java in a World of Containers - DockerCon 2018
Java in a World of Containers - DockerCon 2018Java in a World of Containers - DockerCon 2018
Java in a World of Containers - DockerCon 2018
 
JavaFX - Sketch Board to Production
JavaFX - Sketch Board to ProductionJavaFX - Sketch Board to Production
JavaFX - Sketch Board to Production
 
Building Top-Notch Androids SDKs
Building Top-Notch Androids SDKsBuilding Top-Notch Androids SDKs
Building Top-Notch Androids SDKs
 
[Ultracode Munich #4] Short introduction to the new Android build system incl...
[Ultracode Munich #4] Short introduction to the new Android build system incl...[Ultracode Munich #4] Short introduction to the new Android build system incl...
[Ultracode Munich #4] Short introduction to the new Android build system incl...
 
Java basic
Java basicJava basic
Java basic
 
Android session-1-sajib
Android session-1-sajibAndroid session-1-sajib
Android session-1-sajib
 
Core Android
Core AndroidCore Android
Core Android
 

Recently uploaded

Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 

Recently uploaded (20)

Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 

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