This document summarizes lessons learned from developing the Realm Android library. It discusses challenges such as setting up an Android library project, API design, testing, distribution methods, and issues like annotation processing, bytecode weaving, and native code support. Key points covered are how to start a library project, the importance of testing libraries extensively, and distribution options like Bintray.
10. 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
11. REUSABILITY
A consequence of modularity. Now you can use that unit again in several
places in your projects or accross several different ones
12. 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!
18. 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
19. SOLUTION 1
From Android Studio:
1. Create a new Application project
2. Add a new Library module
3. Remove the Application module
20. 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
23. ▸ 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
24. 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
38. HOW TO PRODUCE A SOURCE JAR
task androidSourcesJar(type: Jar) {
from android.sourceSets.main.java.srcDirs
}
39. 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'
}
}
40. 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
44. ! 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
45. SOME VERY cool libraries USE IT!
▸ Dagger
▸ Butter Knife
▸ AutoValue/AutoParcel
▸ Realm
50. ! 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
52. ! 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
53. TOOLS THAT ALLOW TO DO BYTECODE WEAVING
▸ Javassist
▸ ASM
▸ AspectJ
58. 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'
59. 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"
}
}
}
60. FIND OUT MORE ABOUT IT
http://tools.android.com/tech-docs/new-build-system/gradle-
experimental