[113] lessons from realm

NAVER D2
NAVER D2NAVER D2
DEVELOPING ANDROID LIBRARIES:
LESSONS FROM REALM
EMANUELE ZATTIN (@EMANUELEZ)
DEVIEW, SEPTEMBER 14, 2015
THANK YOU NAVER!
LET me KNOW YOU
WHAT IS REALM?
REALM IS A
MOBILE
DATABASE
REALM IS A
ZERO-COPY
OBJECT STORE
REALM FOR ANDROID WAS
FIRST RELEASED ONE YEAR
AGO HERE AT DEVIEW
SOME OF OUR USERS*
IN KOREA
*
Find out more at http://realm.io/kr/users
WHY WOULD YOU WRITE A
LIBRARY?
MODULARITY
The ability to split your code base into several units with well defined
interface. This allows to focus on smaller problems, producing more
testable code
REUSABILITY
A consequence of modularity. Now you can use that unit again in several
places in your projects or accross several different ones
SHAREABILITY
A consequence or reusability. You found a nice way to solve a particular
issue. Why now helping other developers? Get your name out there!
WHY WOULD YOU WRITE AN
ANDROID LIBRARY?
▸ UI
▸ Looper/Handler
▸ Sensors
▸ Native code
▸ Many more!
WHY WOULD REALM WRITE
AN ANDROID LIBRARY?
▸ UI
▸ Looper/Handler
▸ Sensors
▸ Native code
▸ Many more!
CHALLENGE 1HOW TO START AN ANDROID
LIBRARY PROJECT?
THE PROBLEM
ANDROID STUDIO ALLOWS YOU TO:
▸ Create a new Application project
▸ Create a new Library project
▸ Add a new Application module
▸ Add a new Library module
SOLUTION 1
From Android Studio:
1. Create a new Application project
2. Add a new Library module
3. Remove the Application module
SOLUTION 2
From the command line:
android create lib-project -t 1 -k kr.deview.awesomelib -p . -g -v 1.3.0
-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
CHALLENGE 2API DESIGN
SOLUTION
DEFINE WHAT A GOOD API IS
▸ 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
STAND ON THE SHOUDERS OF GIANTS
▸ Effective Java 2 by Joshua Bloch
▸ How To Design A Good API and Why it Matters by Joshua Bloch
CHALLENGE 3TESTING
TESTING IS EVEN MORE IMPORTANT FOR
LIBRARIES THAN FOR APPLICATIONS
BECAUSE YOU HAVE NO IDEA HOW YOUR
CUSTOMERS ARE GOING TO USE IT.
SOLUTION 1
PREFER JUNIT41
OVER JUNIT3
1
Because parametric tests will save your life!
SOLUTION 2
AUTOMATE ALL THE THINGS!
LET JENKINS BECOME YOUR
BEST FRIEND
USEFUL JENKINS PLUGINS
▸ Job Config History
▸ Git
▸ Android Emulator
▸ Matrix Job2
▸ Junit2
2
It comes preloaded with Jenkins
SOLUTION 3
WRITE SAMPLE APPS!
▸ Additional integration tests
▸ Showcase how to use your library
▸ Validate your core principles
CHALLENGE 4JAR OR AAR?
DO YOU WANT TO SUPPORT ECLIPSE?
SHOW ME YOUR HANDS ONCE AGAIN
SOLUTION
USE AAR
CHALLENGE 5WHERE TO PUBLISH?
SOLUTION
BINTRAY
HOW TO PRODUCE A SOURCE JAR
task androidSourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
}
HOW TO PRODUCE 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(project.android.getBootClasspath())
classpath = files(variant.javaCompile.classpath.files) +
ext.androidJar
exclude '**/BuildConfig.java'
exclude '**/R.java'
}
}
Bintray also provides a Gradle plugin for the actual publishing
The configuration is not trivial, and in the beginning it might be easier
to just do the release manually on the binary website
CHALLENGE 6INTROSPECTION IS SLOW
Sometimes libraries need to be smart behind the scenes to save the
user from writing boilerplate code
SOLUTION
ANNOTATION PROCESSING
! PROS:
▸ It allows you to write new Java files
▸ It happens at compilation time
! CONS
▸ It does not allow to modify existing code
▸ It's not very easy to use
SOME VERY cool libraries USE IT!
▸ Dagger
▸ Butter Knife
▸ AutoValue/AutoParcel
▸ Realm
BAD NEWS
Android does not include the package
javax.annotation.processing
WORKAROUND
Create two Java sub-projects:
1. annotations (used both by the library and the processor)
2. annotations processor
YOU NEED TO INCLUDE THE ANNOTATIONS IN THE JAVADOC
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" // <-- Remember this!
ext.androidJar = files(project.android.getBootClasspath())
classpath = files(variant.javaCompile.classpath.files)
+ ext.androidJar
exclude '**/BuildConfig.java'
exclude '**/R.java'
}
}
CHALLENGE 7
SOMETIMES ANNOTATION
PROCESSING IS NOT
POWERFUL ENOUGH
! PROS:
▸ It allows you to write new Java files
▸ It happens at compilation time
! CONS
▸ It does not allow to modify existing code
▸ It's not very easy to use
SOLUTION
BYTECODE WEAVING
! PROS:
▸ It allows to modify existing code ❤
▸ It's easier to use compared to Annotation Processing
! CONS
▸ You really need to know what you are doing
▸ It might look weird in the debugger
TOOLS THAT ALLOW TO DO BYTECODE WEAVING
▸ Javassist
▸ ASM
▸ AspectJ
THANK YOU STEPHANE NICOLAS!
https://github.com/stephanenicolas/injects
CHALLENGE 8NATIVE CODE
THE BAD NEWS
THE ANDROID GRADLE PLUGIN
DOES NOT SUPPORT NDK
ANYMORE
THE GOOD NEWS
GOOGLE IS DEVELOPING AN
EXPERIMENTAL NEW PLUGIN
THAT SUPPORTS NDK
HOW TO START USING IT
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle-experimental:0.2.0'
}
}
apply plugin: 'com.android.model.application'
THE DSL IS SLIGHTLY DIFFERENT
model { // <-- This!
android {
compileSdkVersion = 22 // Now it's a property, not a method
buildToolsVersion = "22.0.1" // Same here and the rest of the example
defaultConfig.with { // Use the with method
applicationId = "com.example.user.myapplication"
minSdkVersion.apiLevel = 15 // Use the apiLevel property
targetSdkVersion.apiLevel = 22 // Same here
versionCode = 1
versionName = "1.0"
}
}
}
FIND OUT MORE ABOUT IT
http://tools.android.com/tech-docs/new-build-system/gradle-
experimental
ONE ANNOYING LIMITATION
NO SUPPORT FOR CREATING
AND DEPENDING ON STATIC
LIBRARIES
THANK YOU!
REALM 부스에
많이 방문해주세요!
오늘만 열어요
1 of 63

More Related Content

Viewers also liked(20)

[122] line on apple watch[122] line on apple watch
[122] line on apple watch
NAVER D211.7K views
[134] immersive sound vr[134] immersive sound vr
[134] immersive sound vr
NAVER D212.2K views
[123] quality without qa[123] quality without qa
[123] quality without qa
NAVER D273.5K views
[142] how riot works[142] how riot works
[142] how riot works
NAVER D213K views
[153] apache reef[153] apache reef
[153] apache reef
NAVER D212.2K views
[141] react everywhere[141] react everywhere
[141] react everywhere
NAVER D213.9K views
[132] rust[132] rust
[132] rust
NAVER D212.7K views
[164] pinpoint[164] pinpoint
[164] pinpoint
NAVER D214.6K views

Similar to [113] lessons from realm

JavaJava
Javasasi saseenthiran
272 views21 slides

Similar to [113] lessons from realm(20)

Writing Android LibrariesWriting Android Libraries
Writing Android Libraries
emanuelez298 views
Elements of Java Language Elements of Java Language
Elements of Java Language
Hitesh-Java541 views
JavaJava
Java
sasi saseenthiran272 views
Java 2 computer science.pptxJava 2 computer science.pptx
Java 2 computer science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL39 views
The new java developers kit bagThe new java developers kit bag
The new java developers kit bag
Jamie Coleman17 views
Front end-modernizationFront end-modernization
Front end-modernization
ColdFusionConference587 views
Front end-modernizationFront end-modernization
Front end-modernization
devObjective368 views
Java presentationJava presentation
Java presentation
Karan Sareen2.8K views
Java course-in-mumbaiJava course-in-mumbai
Java course-in-mumbai
Unmesh Baile257 views
Basics of JAVA programmingBasics of JAVA programming
Basics of JAVA programming
Elizabeth Thomas8.4K views
Iz PackIz Pack
Iz Pack
Inria736 views
JBCN_Testing_With_ContainersJBCN_Testing_With_Containers
JBCN_Testing_With_Containers
Grace Jansen119 views
Core Java SlidesCore Java Slides
Core Java Slides
Vinit Vyas22.9K views
10 interesting things about java10 interesting things about java
10 interesting things about java
kanchanmahajan2332 views

More from NAVER D2(20)

[235]Wikipedia-scale Q&A[235]Wikipedia-scale Q&A
[235]Wikipedia-scale Q&A
NAVER D21.5K views
[213] Fashion Visual Search[213] Fashion Visual Search
[213] Fashion Visual Search
NAVER D21.5K views

[113] lessons from realm