27
Copyright © 2011 CommonsWare, LLC Android Library Projects

Android Library Projects

Embed Size (px)

DESCRIPTION

From 2011's AnDevCO

Citation preview

Page 1: Android Library Projects

Copyright © 2011 CommonsWare, LLC

Android Library Projects

Page 2: Android Library Projects

Copyright © 2011 CommonsWare, LLC

Overview

● Usage Patterns● Basic Setup● Recent Tools Changes● Inside CWAC● Design Considerations● What We Need● Q&A

Page 3: Android Library Projects

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

Page 4: Android Library Projects

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

Page 5: Android Library Projects

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

Page 6: Android Library Projects

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

Page 7: Android Library Projects

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

Page 8: Android Library Projects

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

Page 9: Android Library Projects

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

Page 10: Android Library Projects

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

Page 11: Android Library Projects

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

Page 12: Android Library Projects

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

Page 13: Android Library Projects

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

Page 14: Android Library Projects

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

Page 15: Android Library Projects

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

Page 16: Android Library Projects

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

Page 17: Android Library Projects

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

Page 18: Android Library Projects

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

Page 19: Android Library Projects

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

Page 20: Android Library Projects

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

Page 21: Android Library Projects

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.

Page 22: Android Library Projects

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

Page 23: Android Library Projects

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

Page 24: Android Library Projects

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

Page 25: Android Library Projects

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

Page 26: Android Library Projects

Copyright © 2011 CommonsWare, LLC

What We Need

● Good Catalog of Reusable Components– Widgets– Application templates– Other libraries

● Stabilization of Tools● More Experience More Patterns→

Page 27: Android Library Projects

Copyright © 2011 CommonsWare, LLC

Q & A