SlideShare une entreprise Scribd logo
1  sur  13
Télécharger pour lire hors ligne
How to write

an annotation processor
in Android
Ivan Kušt
• Generics

http://docs.oracle.com/javase/tutorial/java/generics/

• Reflection

http://docs.oracle.com/javase/tutorial/reflect/

• Annotations

http://docs.oracle.com/javase/1.5.0/docs/guide/apt/
mirror/overview-summary.html
How to write code faster?
Let’s look at our options
Generics Reflection Annotations
Speed Fast Slow
Fast (if using
compile time)
Ease of use Easy Medium Advanced
“Freedom" - generic types
- modify everything
at run-time
- generate code
during compile
• use generics for type agnostic structures when
possible (e.g. lists) - it’s easy and fast

• when that isn’t enough, use annotation processing -
generate code that is boring to write

• generated code isn’t “visible” - use reflection to access
it at run time from the code that you write
Approach
Let’s get our hands dirty
• actually only the keyboard gets dirty over time

• prerequisites:

- Android Studio (tested on version 0.4.0)

- enable annotation processing

(Open preference and navigate to Compiler → Annotation Processors.
Check "Enable annotation processing”)

- apt plugin https://bitbucket.org/hvisser/android-apt

• what is an annotation processor made of?

- definition of annotations using @interface

- a class that extends AbstractProcessor

- entry containing processor name in 

META-INF/services/javax.annotation.processing.Processor
Project structure
• API project 

- annotation definitions

- other non-generated code

• Compiler project

- annotation processor implementation

- annotation definitions (from API project) on
classpath

• Application project 

- android application project

- references to API and Compiler projects
API project
• android library project

• contains annotation definitions, for example:









Compiler project
• standard java library project

- this is required in order to see APT classes

• API project sources must be in classpath (build.gradle): 









Compiler project
• AbstractProcessor implementation

- override process(…) method



- return true if processing was successfull



- add following annotations to processor class:



@SupportedAnnotationTypes(“your.package.AnnotationName”)

@SupportedSourceVersion(SourceVersion.RELEASE_7)

• create file: 

src/main/resources/META-INF/services/javax.annotation.processing.Processor

• add fully qualified class name of your processor class to the
file (e.g. co.infinum.annotations.processor.TestProcessor)

Application project
• referencing API and Compiler projects in build.gradle:
Butterknife library
• UI dependency injection library

• View injection on Activities or
arbitrary objects

- no more writing findViewById(…)

• simplifying view holder pattern

• on click listener injection
View injection
class ExampleActivity extends Activity {	
@InjectView(R.id.title) TextView title;	
@InjectView(R.id.subtitle) TextView subtitle;	
@InjectView(R.id.footer) TextView footer;	
!
@Override public void onCreate(Bundle savedInstanceState) {	
super.onCreate(savedInstanceState);	
setContentView(R.layout.simple_activity);	
ButterKnife.inject(this);	
// TODO Use "injected" views...	
}	
}	
• use @InjectView on fields representing view elements

- fields can’t be private or protected

• call ButterKnife.inject(this) in onCreate()
Resources
• annotations example:

https://github.com/ikust/hello-annotations

• http://turbomanage.wordpress.com/2012/09/28/
annotation-processor-android-eclipse/

• https://bitbucket.org/hvisser/android-apt

• http://www.javacodegeeks.com/2012/11/java-
annotations-tutorial-with-custom-annotation.html

• http://blog.retep.org/2009/02/13/getting-class-
values-from-annotations-in-an-annotationprocessor/

• http://jakewharton.github.io/butterknife/

Contenu connexe

Tendances

Java EE revisits design patterns
Java EE revisits design patterns Java EE revisits design patterns
Java EE revisits design patterns Alex Theedom
 
Mobile Fest 2018. Алексей Лизенко. Make your project great again
Mobile Fest 2018. Алексей Лизенко. Make your project great againMobile Fest 2018. Алексей Лизенко. Make your project great again
Mobile Fest 2018. Алексей Лизенко. Make your project great againMobileFest2018
 
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
 
Microsoft Azure DocumentDB - Global Azure Bootcamp 2016
Microsoft Azure DocumentDB -  Global Azure Bootcamp 2016Microsoft Azure DocumentDB -  Global Azure Bootcamp 2016
Microsoft Azure DocumentDB - Global Azure Bootcamp 2016Sunny Sharma
 
Android talks #08 decompiling android applications
Android talks #08   decompiling android applicationsAndroid talks #08   decompiling android applications
Android talks #08 decompiling android applicationsInfinum
 
Zen and the Art of REST API documentation - MuCon London 2015
Zen and the Art of REST API documentation - MuCon London 2015Zen and the Art of REST API documentation - MuCon London 2015
Zen and the Art of REST API documentation - MuCon London 2015Steve Judd
 
Web a Quebec - JS Debugging
Web a Quebec - JS DebuggingWeb a Quebec - JS Debugging
Web a Quebec - JS DebuggingRami Sayar
 
WEPA - Webdriver Enhanced Platform for Automation - WEPATest
WEPA - Webdriver Enhanced Platform for Automation - WEPATestWEPA - Webdriver Enhanced Platform for Automation - WEPATest
WEPA - Webdriver Enhanced Platform for Automation - WEPATestFreddy Vega
 
JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation
JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next GenerationJAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation
JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generationjazoon13
 
Spider进化论
Spider进化论Spider进化论
Spider进化论cjhacker
 
Testing Alfresco extensions
Testing Alfresco extensionsTesting Alfresco extensions
Testing Alfresco extensionsITD Systems
 
vodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev toolsvodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev toolsvodQA
 
JavaScript unit testing with Jasmine
JavaScript unit testing with JasmineJavaScript unit testing with Jasmine
JavaScript unit testing with JasmineYuval Dagai
 

Tendances (20)

Java EE revisits design patterns
Java EE revisits design patterns Java EE revisits design patterns
Java EE revisits design patterns
 
Mobile Fest 2018. Алексей Лизенко. Make your project great again
Mobile Fest 2018. Алексей Лизенко. Make your project great againMobile Fest 2018. Алексей Лизенко. Make your project great again
Mobile Fest 2018. Алексей Лизенко. Make your project great again
 
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
 
Microsoft Azure DocumentDB - Global Azure Bootcamp 2016
Microsoft Azure DocumentDB -  Global Azure Bootcamp 2016Microsoft Azure DocumentDB -  Global Azure Bootcamp 2016
Microsoft Azure DocumentDB - Global Azure Bootcamp 2016
 
Android talks #08 decompiling android applications
Android talks #08   decompiling android applicationsAndroid talks #08   decompiling android applications
Android talks #08 decompiling android applications
 
Java
JavaJava
Java
 
Zen and the Art of REST API documentation - MuCon London 2015
Zen and the Art of REST API documentation - MuCon London 2015Zen and the Art of REST API documentation - MuCon London 2015
Zen and the Art of REST API documentation - MuCon London 2015
 
Spring.new hope.1.3
Spring.new hope.1.3Spring.new hope.1.3
Spring.new hope.1.3
 
Web a Quebec - JS Debugging
Web a Quebec - JS DebuggingWeb a Quebec - JS Debugging
Web a Quebec - JS Debugging
 
WEPA - Webdriver Enhanced Platform for Automation - WEPATest
WEPA - Webdriver Enhanced Platform for Automation - WEPATestWEPA - Webdriver Enhanced Platform for Automation - WEPATest
WEPA - Webdriver Enhanced Platform for Automation - WEPATest
 
Road Trip To Component
Road Trip To ComponentRoad Trip To Component
Road Trip To Component
 
JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation
JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next GenerationJAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation
JAZOON'13 - Sam Brannen - Spring Framework 4.0 - The Next Generation
 
Flywaydb
FlywaydbFlywaydb
Flywaydb
 
Spider进化论
Spider进化论Spider进化论
Spider进化论
 
QSpiders - Jdk Jvm Jre and Jit
QSpiders - Jdk Jvm Jre and JitQSpiders - Jdk Jvm Jre and Jit
QSpiders - Jdk Jvm Jre and Jit
 
Automation
AutomationAutomation
Automation
 
Testing Alfresco extensions
Testing Alfresco extensionsTesting Alfresco extensions
Testing Alfresco extensions
 
vodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev toolsvodQA Pune (2019) - Browser automation using dev tools
vodQA Pune (2019) - Browser automation using dev tools
 
Lucene 101
Lucene 101Lucene 101
Lucene 101
 
JavaScript unit testing with Jasmine
JavaScript unit testing with JasmineJavaScript unit testing with Jasmine
JavaScript unit testing with Jasmine
 

Similaire à Infinum Android Talks #02 - How to write an annotation processor in Android

API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)Tom Johnson
 
Practices and tools for building better APIs
Practices and tools for building better APIsPractices and tools for building better APIs
Practices and tools for building better APIsNLJUG
 
Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)Peter Hendriks
 
Practices and Tools for Building Better APIs
Practices and Tools for Building Better APIsPractices and Tools for Building Better APIs
Practices and Tools for Building Better APIsPeter Hendriks
 
How to build a JavaScript toolkit
How to build a JavaScript toolkitHow to build a JavaScript toolkit
How to build a JavaScript toolkitMichael Nelson
 
Gwt and JSR 269's Pluggable Annotation Processing API
Gwt and JSR 269's Pluggable Annotation Processing APIGwt and JSR 269's Pluggable Annotation Processing API
Gwt and JSR 269's Pluggable Annotation Processing APIArnaud Tournier
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - IntroductionWebStackAcademy
 
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...Jorge Hidalgo
 
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...Jorge Hidalgo
 
[2015/2016] Apache Cordova
[2015/2016] Apache Cordova[2015/2016] Apache Cordova
[2015/2016] Apache CordovaIvano Malavolta
 
APIs distribuidos con alta escalabilidad
APIs distribuidos con alta escalabilidadAPIs distribuidos con alta escalabilidad
APIs distribuidos con alta escalabilidadSoftware Guru
 
Modern Web-site Development Pipeline
Modern Web-site Development PipelineModern Web-site Development Pipeline
Modern Web-site Development PipelineGlobalLogic Ukraine
 
A User Interface for adding Machine Learning tools into GitHub
A User Interface for adding Machine Learning tools into GitHubA User Interface for adding Machine Learning tools into GitHub
A User Interface for adding Machine Learning tools into GitHubRumyana Rumenova
 

Similaire à Infinum Android Talks #02 - How to write an annotation processor in Android (20)

API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)API workshop: Introduction to APIs (TC Camp)
API workshop: Introduction to APIs (TC Camp)
 
Practices and tools for building better APIs
Practices and tools for building better APIsPractices and tools for building better APIs
Practices and tools for building better APIs
 
Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)Practices and tools for building better API (JFall 2013)
Practices and tools for building better API (JFall 2013)
 
iOS Application Exploitation
iOS Application ExploitationiOS Application Exploitation
iOS Application Exploitation
 
Practices and Tools for Building Better APIs
Practices and Tools for Building Better APIsPractices and Tools for Building Better APIs
Practices and Tools for Building Better APIs
 
How to build a JavaScript toolkit
How to build a JavaScript toolkitHow to build a JavaScript toolkit
How to build a JavaScript toolkit
 
Eclipse e4
Eclipse e4Eclipse e4
Eclipse e4
 
Creation&imitation
Creation&imitationCreation&imitation
Creation&imitation
 
Gwt and JSR 269's Pluggable Annotation Processing API
Gwt and JSR 269's Pluggable Annotation Processing APIGwt and JSR 269's Pluggable Annotation Processing API
Gwt and JSR 269's Pluggable Annotation Processing API
 
Apache Cordova 4.x
Apache Cordova 4.xApache Cordova 4.x
Apache Cordova 4.x
 
Angular - Chapter 1 - Introduction
 Angular - Chapter 1 - Introduction Angular - Chapter 1 - Introduction
Angular - Chapter 1 - Introduction
 
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
JavaOne 2017 CON3282 - Code Generation with Annotation Processors: State of t...
 
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
JavaOne 2014 - CON2013 - Code Generation in the Java Compiler: Annotation Pro...
 
[2015/2016] Apache Cordova
[2015/2016] Apache Cordova[2015/2016] Apache Cordova
[2015/2016] Apache Cordova
 
Ide
IdeIde
Ide
 
Apache Cordova
Apache CordovaApache Cordova
Apache Cordova
 
APIs distribuidos con alta escalabilidad
APIs distribuidos con alta escalabilidadAPIs distribuidos con alta escalabilidad
APIs distribuidos con alta escalabilidad
 
SGCE 2015 REST APIs
SGCE 2015 REST APIsSGCE 2015 REST APIs
SGCE 2015 REST APIs
 
Modern Web-site Development Pipeline
Modern Web-site Development PipelineModern Web-site Development Pipeline
Modern Web-site Development Pipeline
 
A User Interface for adding Machine Learning tools into GitHub
A User Interface for adding Machine Learning tools into GitHubA User Interface for adding Machine Learning tools into GitHub
A User Interface for adding Machine Learning tools into GitHub
 

Plus de Infinum

Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...
Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...
Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...Infinum
 
Infinum Android Talks #20 - DiffUtil
Infinum Android Talks #20 - DiffUtilInfinum Android Talks #20 - DiffUtil
Infinum Android Talks #20 - DiffUtilInfinum
 
Infinum Android Talks #20 - Benefits of using Kotlin
Infinum Android Talks #20 - Benefits of using KotlinInfinum Android Talks #20 - Benefits of using Kotlin
Infinum Android Talks #20 - Benefits of using KotlinInfinum
 
Infinum iOS Talks #4 - Making our VIPER more reactive
Infinum iOS Talks #4 - Making our VIPER more reactiveInfinum iOS Talks #4 - Making our VIPER more reactive
Infinum iOS Talks #4 - Making our VIPER more reactiveInfinum
 
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...Infinum
 
Infinum Android Talks #13 - Using ViewDragHelper
Infinum Android Talks #13 - Using ViewDragHelperInfinum Android Talks #13 - Using ViewDragHelper
Infinum Android Talks #13 - Using ViewDragHelperInfinum
 
Infinum Android Talks #14 - Log4j
Infinum Android Talks #14 - Log4jInfinum Android Talks #14 - Log4j
Infinum Android Talks #14 - Log4jInfinum
 
Infinum Android Talks #9 - Making your app location-aware
Infinum Android Talks #9 - Making your app location-awareInfinum Android Talks #9 - Making your app location-aware
Infinum Android Talks #9 - Making your app location-awareInfinum
 
Infinum Android Talks #14 - Gradle plugins
Infinum Android Talks #14 - Gradle pluginsInfinum Android Talks #14 - Gradle plugins
Infinum Android Talks #14 - Gradle pluginsInfinum
 
Infinum Android Talks #14 - Facebook for Android API
Infinum Android Talks #14 - Facebook for Android APIInfinum Android Talks #14 - Facebook for Android API
Infinum Android Talks #14 - Facebook for Android APIInfinum
 
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...Infinum
 
Infinum Android Talks #18 - Create fun lists by Ivan Marić
Infinum Android Talks #18 - Create fun lists by Ivan MarićInfinum Android Talks #18 - Create fun lists by Ivan Marić
Infinum Android Talks #18 - Create fun lists by Ivan MarićInfinum
 
Infinum Android Talks #18 - In-app billing by Ivan Marić
Infinum Android Talks #18 - In-app billing by Ivan MarićInfinum Android Talks #18 - In-app billing by Ivan Marić
Infinum Android Talks #18 - In-app billing by Ivan MarićInfinum
 
Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
Infinum Android Talks #18 - How to cache like a boss by Željko PlesacInfinum Android Talks #18 - How to cache like a boss by Željko Plesac
Infinum Android Talks #18 - How to cache like a boss by Željko PlesacInfinum
 
Infinum iOS Talks #2 - VIPER for everybody by Damjan Vujaklija
Infinum iOS Talks #2 - VIPER for everybody by Damjan VujaklijaInfinum iOS Talks #2 - VIPER for everybody by Damjan Vujaklija
Infinum iOS Talks #2 - VIPER for everybody by Damjan VujaklijaInfinum
 
Infinum iOS Talks #2 - Xamarin by Ivan Đikić
Infinum iOS Talks #2 - Xamarin by Ivan ĐikićInfinum iOS Talks #2 - Xamarin by Ivan Đikić
Infinum iOS Talks #2 - Xamarin by Ivan ĐikićInfinum
 
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho PolutaInfinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho PolutaInfinum
 
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan DikicInfinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan DikicInfinum
 
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran Burojevic
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran BurojevicInfinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran Burojevic
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran BurojevicInfinum
 
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - Testing your Android applications by Ivan KustInfinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - Testing your Android applications by Ivan KustInfinum
 

Plus de Infinum (20)

Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...
Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...
Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...
 
Infinum Android Talks #20 - DiffUtil
Infinum Android Talks #20 - DiffUtilInfinum Android Talks #20 - DiffUtil
Infinum Android Talks #20 - DiffUtil
 
Infinum Android Talks #20 - Benefits of using Kotlin
Infinum Android Talks #20 - Benefits of using KotlinInfinum Android Talks #20 - Benefits of using Kotlin
Infinum Android Talks #20 - Benefits of using Kotlin
 
Infinum iOS Talks #4 - Making our VIPER more reactive
Infinum iOS Talks #4 - Making our VIPER more reactiveInfinum iOS Talks #4 - Making our VIPER more reactive
Infinum iOS Talks #4 - Making our VIPER more reactive
 
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...
 
Infinum Android Talks #13 - Using ViewDragHelper
Infinum Android Talks #13 - Using ViewDragHelperInfinum Android Talks #13 - Using ViewDragHelper
Infinum Android Talks #13 - Using ViewDragHelper
 
Infinum Android Talks #14 - Log4j
Infinum Android Talks #14 - Log4jInfinum Android Talks #14 - Log4j
Infinum Android Talks #14 - Log4j
 
Infinum Android Talks #9 - Making your app location-aware
Infinum Android Talks #9 - Making your app location-awareInfinum Android Talks #9 - Making your app location-aware
Infinum Android Talks #9 - Making your app location-aware
 
Infinum Android Talks #14 - Gradle plugins
Infinum Android Talks #14 - Gradle pluginsInfinum Android Talks #14 - Gradle plugins
Infinum Android Talks #14 - Gradle plugins
 
Infinum Android Talks #14 - Facebook for Android API
Infinum Android Talks #14 - Facebook for Android APIInfinum Android Talks #14 - Facebook for Android API
Infinum Android Talks #14 - Facebook for Android API
 
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
 
Infinum Android Talks #18 - Create fun lists by Ivan Marić
Infinum Android Talks #18 - Create fun lists by Ivan MarićInfinum Android Talks #18 - Create fun lists by Ivan Marić
Infinum Android Talks #18 - Create fun lists by Ivan Marić
 
Infinum Android Talks #18 - In-app billing by Ivan Marić
Infinum Android Talks #18 - In-app billing by Ivan MarićInfinum Android Talks #18 - In-app billing by Ivan Marić
Infinum Android Talks #18 - In-app billing by Ivan Marić
 
Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
Infinum Android Talks #18 - How to cache like a boss by Željko PlesacInfinum Android Talks #18 - How to cache like a boss by Željko Plesac
Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
 
Infinum iOS Talks #2 - VIPER for everybody by Damjan Vujaklija
Infinum iOS Talks #2 - VIPER for everybody by Damjan VujaklijaInfinum iOS Talks #2 - VIPER for everybody by Damjan Vujaklija
Infinum iOS Talks #2 - VIPER for everybody by Damjan Vujaklija
 
Infinum iOS Talks #2 - Xamarin by Ivan Đikić
Infinum iOS Talks #2 - Xamarin by Ivan ĐikićInfinum iOS Talks #2 - Xamarin by Ivan Đikić
Infinum iOS Talks #2 - Xamarin by Ivan Đikić
 
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho PolutaInfinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
 
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan DikicInfinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
 
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran Burojevic
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran BurojevicInfinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran Burojevic
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran Burojevic
 
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - Testing your Android applications by Ivan KustInfinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
 

Dernier

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 

Dernier (20)

Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 

Infinum Android Talks #02 - How to write an annotation processor in Android

  • 1. How to write
 an annotation processor in Android Ivan Kušt
  • 2. • Generics
 http://docs.oracle.com/javase/tutorial/java/generics/
 • Reflection
 http://docs.oracle.com/javase/tutorial/reflect/
 • Annotations
 http://docs.oracle.com/javase/1.5.0/docs/guide/apt/ mirror/overview-summary.html How to write code faster?
  • 3. Let’s look at our options Generics Reflection Annotations Speed Fast Slow Fast (if using compile time) Ease of use Easy Medium Advanced “Freedom" - generic types - modify everything at run-time - generate code during compile
  • 4. • use generics for type agnostic structures when possible (e.g. lists) - it’s easy and fast
 • when that isn’t enough, use annotation processing - generate code that is boring to write
 • generated code isn’t “visible” - use reflection to access it at run time from the code that you write Approach
  • 5. Let’s get our hands dirty • actually only the keyboard gets dirty over time
 • prerequisites:
 - Android Studio (tested on version 0.4.0)
 - enable annotation processing
 (Open preference and navigate to Compiler → Annotation Processors. Check "Enable annotation processing”)
 - apt plugin https://bitbucket.org/hvisser/android-apt
 • what is an annotation processor made of?
 - definition of annotations using @interface
 - a class that extends AbstractProcessor
 - entry containing processor name in 
 META-INF/services/javax.annotation.processing.Processor
  • 6. Project structure • API project 
 - annotation definitions
 - other non-generated code
 • Compiler project
 - annotation processor implementation
 - annotation definitions (from API project) on classpath
 • Application project 
 - android application project
 - references to API and Compiler projects
  • 7. API project • android library project
 • contains annotation definitions, for example:
 
 
 
 

  • 8. Compiler project • standard java library project
 - this is required in order to see APT classes
 • API project sources must be in classpath (build.gradle): 
 
 
 
 

  • 9. Compiler project • AbstractProcessor implementation
 - override process(…) method
 
 - return true if processing was successfull
 
 - add following annotations to processor class:
 
 @SupportedAnnotationTypes(“your.package.AnnotationName”)
 @SupportedSourceVersion(SourceVersion.RELEASE_7)
 • create file: 
 src/main/resources/META-INF/services/javax.annotation.processing.Processor
 • add fully qualified class name of your processor class to the file (e.g. co.infinum.annotations.processor.TestProcessor)

  • 10. Application project • referencing API and Compiler projects in build.gradle:
  • 11. Butterknife library • UI dependency injection library
 • View injection on Activities or arbitrary objects
 - no more writing findViewById(…)
 • simplifying view holder pattern
 • on click listener injection
  • 12. View injection class ExampleActivity extends Activity { @InjectView(R.id.title) TextView title; @InjectView(R.id.subtitle) TextView subtitle; @InjectView(R.id.footer) TextView footer; ! @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.inject(this); // TODO Use "injected" views... } } • use @InjectView on fields representing view elements
 - fields can’t be private or protected
 • call ButterKnife.inject(this) in onCreate()
  • 13. Resources • annotations example:
 https://github.com/ikust/hello-annotations
 • http://turbomanage.wordpress.com/2012/09/28/ annotation-processor-android-eclipse/
 • https://bitbucket.org/hvisser/android-apt
 • http://www.javacodegeeks.com/2012/11/java- annotations-tutorial-with-custom-annotation.html
 • http://blog.retep.org/2009/02/13/getting-class- values-from-annotations-in-an-annotationprocessor/
 • http://jakewharton.github.io/butterknife/