Slices: What They're Not Telling You
Mark Murphy, CommonsWare
https://developer.android.com/guide/slices/
Slices are UI templates that can display rich, dynamic, and interactive content from your app from within the Google Search app and later in other places like the Google Assistant.
Slices will start appearing soon for users, but you can start building today.
You can customize your Slice with color, text, imagery, video, and more to match your app's design.
(That's) What I'm Talkin' 'bout
- What are slices, really?
- How do we create and publish them?
- How do we host and render them?
- Why would we bother?
A Problem As Old As Time
The Android Solution You're Used To
The Slice Scenario
A Slice of... Stuff
-
What Provider Provides
- Structured data (e.g., weather forecast)
- Metadata for suggested visual organization
-
What Host Does With It
- Whatever it wants
- Probably uses library-supplied rendering classes
Pieces of a Slice
-
androidx.slice:slice-builders-ktx
dependency
- Kotlin slice-building DSL
- Everything involving slices requires
androidx
!
-
SliceProvider
Subclass
- Plus corresponding manifest entry
- Key method:
onBindSlice()
Trying Your Slice
- Install the Slice Viewer app
-
Run Your Slice in the Viewer from Studio
- Set up a
slice-content
run configuration
- Tie it to your provider's authority
Slices... Live!
- Your slices are identified by a
Uri
- When a slice changes, use
notifyChange()
- Slice hosts will automagically find out about the change and fetch a fresh slice
-
Scenarios
- New slice with asynchronous data load
- Underlying data changes for existing slices
- In response to user input from the slice
Slices... LiveData
!
Rendering a Slice
- Add a
SliceView
to your UI
- Configure the
SliceView
(e.g., mode
)
- Get a
SliceLiveData
using fromUri()
- Use
observe()
to tie the SliceView
to the SliceLiveData
- That's it!
Custom Slicing
- Get a
SliceLiveData
using fromUri()
observe()
with your own Observer
-
Consume the
Slice
Objects
- Tree of
SliceItem
objects
SliceMetadata
to help decode a Slice
- Poorly documented
- Use case:
SliceProvider
testing
Problem: Discovery
- The host needs the
Uri
to the provider
-
Possible Solutions
<intent-filter>
<meta-data>
- Any already-established IPC mechanisms
- Key: agreed-upon rules between M hosts and N providers
What Are They Good For?
E-Commerce
- E-commerce app publishes slices
Uri
contains referral ID and search keyword
- Hosts render the slice, make $$$
-
Benefits
- No ad SDK requirement: just a
Uri
- Richer than image ad banners
- Less costly than
WebView
What Are They Good For?
App Widgets 2.0
- Launcher developers agree to specification for
Uri
discovery
- App publish slices
- Launchers add to "widgets" catalog
Slice
to RemoteViews
bridge to support incompatible home screens, with same content
What Are They Good For?
Knowledge App Integration
- Wikipedia, etc. publish slices with search terms in
Uri
structure
- Apps can embed those slices in popups for term definitions, etc.
- Once again: no heavyweight SDK, no
WebView
What Are They Good For?
OMG I'm Running Out Of Time
- App suite integration
ACTION_SEND
alternative
- Specialized slice hosts (screenreaders, "portals", etc.)
- "Turnkey" slice providers (RSS/Atom, Swagger, etc.)
- Developer tools (Sonar plugin, testing library)
Compatibility Concerns
- Slice spec and implementation need to be versioned
-
Slice Host Must Be Flexible
- Graceful degradation for older-spec slices
- Defensive programming for newer-spec slices
- Not implemented in
1.0.0-alpha3
slice rendering code, apparently
- Possible differences between library, platform implementations
Questions?
https://commonsware.com/presos/androidSummit2018
https://tinyurl.com/cw-summit2018