Source link:

Scoper - Lightweight Dagger 2 Component Management

Quick Start

Setup will depend on how you want to build your scopes. The following instructions will assume you have scopes as follows:

Application Scope->Activity Scope->View scope...

Each subsequent scope is a subscope built on top of the previous scope.

Create an Application subclass. In it you will initialize the Application Scope's component.

public class MyApplication extends Application {

 public static final String SCOPE_TAG = "application_scope";

@Override public void onCreate() {


 Scoper.cacheComponent(SCOPE_TAG, DaggerApplicationComponent.builder()

.applicationModule(new ApplicationModule(this))



Next, in each Activity, you should override the attachBaseContext(Context newBase) method and return an instance of ScoperContext as shown. Doing this in a base Activity class can simplify the process. The Activity should also implement the Scoped interface. This has you supply a scope name that will be used to map the component in the cache. The Type Parameter for the Scoped interface is the component class for that scope.

public abstract class BaseActivity<T> extends AppCompatActivity implements Scoped<T> {

 protected T component;

 @Override protected void onCreate(Bundle savedInstanceState) {


component = Scoper.createComponent(this, initComponent());


 @Override protected void attachBaseContext(Context newBase) {

super.attachBaseContext(new ScoperContext(newBase, getScopeName()));


 @Override public String getScopeName() {

return getClass().getName();


 protected abstract T initComponent();

 @Override protected void onDestroy() {


if (isFinishing()) {



final class MainActivity extends BaseActivity<MainComponent> {

  @Override protected void onCreate(Bundle savedInstanceState) {




 @Override protected MainComponent initComponent() {

return Scoper.<ApplicationComponent>withParent(MyApplication.SCOPE_TAG)

 .createChild(this, appComponent -> MainModule()));


This setup should be the same whether you use Fragments/Plain Views/Conductor/etc. for the building blocks of your UI.

How you do the next level of scopes will vary depending on your choice of UI management. Demos are provided to show possible solutions (more on the way).

**Note: In the above example we are using the overloaded method for Scoper#withParent that takes in a String for the tag. Normally, you would want to use the overloaded method that takes a Context (ideally all of your downstream Contexts are ScoperContext instances). Overriding #attachBaseContext in an Application subclass can break some libraries, though, so in the case of getting a reference to the Application component, it is recommended to get it via its scope tag.

This is just one potential way to set up the boilerplate for scoping your components. Scoper has methods like cacheComponent(String scopeName), getComponentForTag(String scopeName), destroyScope(String scopeName) that allow you to manually manage your components without having to implement the Scoped interface or use ScoperContext.

Caching Behavior

The default behavior is to cache components across config changes. This means that each time your class calls createComponent(context, component), if there is an existing component for that scope, the existing instance will be returned rather than the provided new instance.

This behavior can be overridden by calling Scoper.replaceExisting(true).


Add to your Gradle dependencies (Check badge at top for version information):

buildscript {

  repositories {

  dependencies {

  compile 'com.brandongogetap:scoper:x.y.z' 


Copyright 2016 Brandon Gogetap  Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License. You may obtain a copy of the License at  Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 


Smooth activity switch animation.

PoppyView is a library which implements view on the bottom which come and go relative to the user scroll.

Just add the library to your application as a library project. And don't forget that your project must use NineOldAndroids.

A small library replicating the new dialogs in Android L.

Just as there was Retroweaver et al. for running Java 5 code with generics on Java 1.4, Retrolambda lets you run Java 8 code with lambda expressions on Java 7 or lower. It does this by transforming your Java 8 compiled bytecode so that it can run on a Java 7 runtime. After the transformation they are just a bunch of normal .class files, without adding any runtime dependencies.

ViewServer is a simple class you can use in your Android application to use the HierarchyViewer inspection tool.

ViewServer requires the Android SDK r12 or higher.

Extended Javassist for Android. DEX (ODEX) file can be made from ".class" file that is generated by Javassist.


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes