Your SlideShare is downloading. ×
Android Library Projects
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Android Library Projects

5,119
views

Published on

From 2011's AnDevCO

From 2011's AnDevCO

Published in: Technology, Education

0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,119
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
82
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Android Library Projects Copyright © 2011 CommonsWare, LLC
  • 2. Overview● Usage Patterns● Basic Setup● Recent Tools Changes● Inside CWAC● Design Considerations● What We Need● Q&A Copyright © 2011 CommonsWare, LLC
  • 3. Library Usage Patterns● Free/Paid – Common logic in library – Separate hosting projects for free, paid ● Both reference library, use common logic● Related Patterns – Multiple APK support – Multiple brands/content Copyright © 2011 CommonsWare, LLC
  • 4. Library Usage Patterns● Code Generation – Library project containing common logic – Code generated hosting projects referencing the library ● Example: “make an app from your RSS feed” services Copyright © 2011 CommonsWare, LLC
  • 5. Library Usage Patterns● Reusable Component/Framework – Library is the component or framework – Sample project(s) to demonstrate usage – Third parties add the library to reuse – Example: CWAC ● CommonsWare Android Components Copyright © 2011 CommonsWare, LLC
  • 6. Basic Setup● Creating an Android Library Project: Eclipse – Create a regular Android project – In Project Properties choose “Is Library” in the Android section – Pat yourself on the back Copyright © 2011 CommonsWare, LLC
  • 7. Basic Setup● Creating an Android Library Project: Shell – android create lib-project ● -t = build target ● -k = package ● -p = path for generating files Copyright © 2011 CommonsWare, LLC
  • 8. Basic Setup● Consuming a Library Project: Eclipse – In Project Properties... – ...in Android area... – ...in Library group box... – …click Add and choose a library Copyright © 2011 CommonsWare, LLC
  • 9. Basic Setup● Consuming a Library Project: Shell – Official: android update lib-project ● -t = build target ● -p = path to hosting project ● --library = path to library project – Unofficial: add the line to project.properties ● android.library.reference.1=path/to/library Copyright © 2011 CommonsWare, LLC
  • 10. Recent Tools Changes● Classic Distribution: Source Code – Distribute entire library project to anyone reusing it – Net ● OK for internal use ● Sorta OK for open source projects (inconvenient) ● Not OK for commercial components Copyright © 2011 CommonsWare, LLC
  • 11. Recent Tools Changes● Long-Term Distribution: JAR – JAR containing code and resources – Drop JAR in project, build tools automagically detect that this is an Android library project JAR, integrate the resources Copyright © 2011 CommonsWare, LLC
  • 12. Recent Tools Changes● State of the Union: Halfway There – R14/R15 tools generate JARs from library projects for use by hosting projects – Not designed for redistribution ● Coming in a future tools release Copyright © 2011 CommonsWare, LLC
  • 13. Recent Tools Changes● Packaging Resources – Ideally, library refers to resources via R constants ● E.g., R.layout.main – Problem: values generated by hosting project ● Library project would have its own temporary values ● Those values would be inlined in bytecode ● Net: wrong resources Copyright © 2011 CommonsWare, LLC
  • 14. Recent Tools Changes● Packaging Resources – R14 change: R values not final – Impact: cannot use in switch() statement ● Java requires switch() value to be a constant – Migration ● Manual conversion to if-else ● <Ctrl>-<1> fixup option in Eclipse Copyright © 2011 CommonsWare, LLC
  • 15. Distribution Options● Today – Without resources: Plain Ol JAR, OK? – With resources: ZIP ● Can distribute a JAR plus resources, but need to do runtime lookups of R values● Tomorrow – JAR for all Copyright © 2011 CommonsWare, LLC
  • 16. Distribution Options● Binary Library Projects: Workaround – Create standard library project, get working ● Runtime lookup of R values – Create distribution library project ● Generate JAR from library project source code ● Copy original library project to separate tree ● Nuke everything under src/ ● Copy JAR into libs/ ● ZIP the result Copyright © 2011 CommonsWare, LLC
  • 17. Case Studies from CWAC● WakefulIntentService – You want to do work purely in the background ● E.g., AlarmManager – You have a tough time keeping the device awake – WakefulIntentService wraps up WakeLock management in easy IntentService replacement Copyright © 2011 CommonsWare, LLC
  • 18. Case Studies from CWAC● Library – No resources, so Android library project with custom JAR-creating Ant tasks● Host – Either use the JAR as-is or add reference to the library project Copyright © 2011 CommonsWare, LLC
  • 19. Case Studies from CWAC● TouchListView – Drag-and-drop reordering of rows – Code culled from AOSP Music app (circa 2009 or so) – Drop-in replacement for ListView ● Custom attributes to control drag-and-drop behavior Copyright © 2011 CommonsWare, LLC
  • 20. Case Studies from CWAC● Library – Contains resources ● References them via dynamic ID lookup – Distributed only as library project● Host – References library project, uses widget Copyright © 2011 CommonsWare, LLC
  • 21. Design Considerations● Manifest – Library manifest contents not presently copied into host project – Net: document to reusers that they need to add stuff ● Permissions ● Components ● Features, SDK versions, etc. Copyright © 2011 CommonsWare, LLC
  • 22. Design Considerations● Interfaces – Libraries cannot reference classes in host project ● Well, OK, reflection, but thats cheatin – Core Pattern ● Define interface in library ● Host implements interface, supplies to library ● Library calls methods on the interface ● Use with strategy, factory patterns Copyright © 2011 CommonsWare, LLC
  • 23. Design Considerations● Replaceable Resources – Host project may need to replace your resources ● Color schemes ● i18n / l10n ● Gaps in your implementation (screen sizes, accessibility) – Host project can replace your resources Copyright © 2011 CommonsWare, LLC
  • 24. Design Considerations● Replaceable Resources – Consider your shipped resources to be defaults – Choose unique names unlikely to collide with host project, other libraries ● E.g., R.layout.mycomponent_main Copyright © 2011 CommonsWare, LLC
  • 25. Design Considerations● Libraries Referencing Libraries – Possible – Previously troublesome and fragile ● Might be improved once true libraries-as-JARs emerges (R16?) – Avoid where possible until patterns stabilize Copyright © 2011 CommonsWare, LLC
  • 26. What We Need● Good Catalog of Reusable Components – Widgets – Application templates – Other libraries● Stabilization of Tools● More Experience → More Patterns Copyright © 2011 CommonsWare, LLC
  • 27. Q&ACopyright © 2011 CommonsWare, LLC