Upload
commonsware
View
5.140
Download
0
Tags:
Embed Size (px)
DESCRIPTION
From 2011's AnDevCO
Citation preview
Copyright © 2011 CommonsWare, LLC
Android Library Projects
Copyright © 2011 CommonsWare, LLC
Overview
● Usage Patterns● Basic Setup● Recent Tools Changes● Inside CWAC● Design Considerations● What We Need● Q&A
Copyright © 2011 CommonsWare, LLC
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
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
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
Basic Setup
● Creating an Android Library Project: Eclipse– Create a regular Android
project– In Project Properties
choose “Is Library” inthe Android section
– Pat yourself on the back
Copyright © 2011 CommonsWare, LLC
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
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
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
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
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
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
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
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
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
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
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
Case Studies from CWAC
● Library– No resources, so Android library
project with custom JAR-creatingAnt tasks
● Host– Either use the JAR as-is or add reference to the
library project
Copyright © 2011 CommonsWare, LLC
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
Case Studies from CWAC
● Library– Contains resources
● References them via dynamicID lookup
– Distributed only as library project● Host
– References library project, uses widget
Copyright © 2011 CommonsWare, LLC
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
Design Considerations
● Interfaces– Libraries cannot reference
classes in host project● Well, OK, reflection, but that's 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
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
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
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
What We Need
● Good Catalog of Reusable Components– Widgets– Application templates– Other libraries
● Stabilization of Tools● More Experience More Patterns→
Copyright © 2011 CommonsWare, LLC
Q & A