%in tembisa+277-882-255-28 abortion pills for sale in tembisa
Android architecture
1. Android
Architecture
Presentation by:
Bui Trong An
Trip Application
Email: trongan93@gmail.com
Skype: trongan93
Reference:
Technical Uber http://t.uber.com
Google Dagger2 - https://google.github.io/dagger
Dependency Injection
2. Audience Prerequisites
You know how to write a basic Android App
Nice to have:
You’re familiar with ButterKnife
You’ve written a fairly large or growing Andorid App
You’ve configured and used networking stacks,
imaging libraries, persistent data...
3. Wikipedia
Dependency injection is a software design pattern in which
one or more dependencies (or services) are injected, or
passed by reference, into a dependent object (or client)
and are made part of the client's state. The pattern
separates the creation of a client's dependencies from its
own behavior, which allows program designs to be loosely
coupled and to follow the dependency inversion and single
responsibility principles.
4. Goal of this presentation
To explain the reasons and concepts behind
Dependency Injection, specifically Dagger 2 in
in the context of Android.
To understand the big picture so you’ll know
where to look for the answers.
5. TL;DR
Separation of Configuration & Usage
Maintainability, testability, mockability, low
coupling, easy singletons, less boilerplate*
6. Configuration vs Usage
SharedPreferences
Image Loader
(Picasso)
Network Client
(OkHttp)
API Interface
(Retrofit)
Http Disk Cache
Image Disk Cache
SessionManager
(Auth tokens)
Activities
Fragments
Views
Objects
7. Extremely brief history of Dagger
Dagger 1 - Square
Object Graph
Code Generation
Some Reflection
Dagger 2 - Google
Components
Code Generation
No Reflection
10. TextView mTitleText;
TextView mBodyText;
TextView mIconText;
// in constructor
mTitleText =
(TextView) findViewById(R.id.title)
mBodyText =
(TextView) findViewById(R.id.body)
mFooterText =
(ImageView) findViewById(R.id.icon)
Butterknife Analogy
@InjectView(R.id.title)
TextView mTitleText;
@InjectView(R.id.body)
TextView mBodyText;
@InjectView(R.id.icon)
ImageView mIcon;
// in constructor
Butterknife.inject(this, viewGroup)
Take all fields with annotation, look in the ViewGroup and
find the view by id, cast to expected type, set field.
12. Configuration vs Usage
SharedPreferences
Image Loader
(Picasso)
Network Client
(OkHttp)
API Interface
(Retrofit)
Http Disk Cache
Image Disk Cache
SessionManager
(Auth tokens)
Activities
Fragments
Views
Objects
@Inject
13. Dagger Usage
Component Object
“find object
by type”
@Inject
Take all fields with annotation, look in the Component
and find the object by type (or create it), and set the field.
FKA ObjectGraph
14. Dagger Usage
@Inject Picasso mPicasso;
@Inject SessionManager mSessionManager;
@Inject Toolbar mToolbar;
@Inject UberApi mUberApi;
// in onCreate()
getComponent().inject(this);
Take all fields with annotation, look in the Component
and find the object by type (or create it), and set the field.
15. Where do we get the Component?
public class MyApp extends Application {
private MyAppComponent component;
@Override public void onCreate() {
component = ???
}
public MyAppComponent getComponent() {
return component;
}
}
<application
android:name=".MyApp"
…>
<activity … />
</application>
AndroidManifest.xml MyApp.java
16. public MyComponent getComponent() { return component; }
Where do we get the Component?
MyApp.java
((MyApp) this.getApplicationContext()).getComponent().inject(this);
MyActivity.java
18. public static MyComponent getComponent(Context context) {
return ((MyApp) context.getApplicationContext()).component;
}
Where do we get the Component?
MyApp.java
MyApp.getComponent(this).inject(this);
MyActivity.java
19. public static MyComponent getComponent(Context context) {
return ((MyApp) context.getApplicationContext()).component;
}
Where do we get the Component?
MyApp.java
MyApp.getComponent(this).inject(this);
MyActivity.java
MyApp.getComponent(getActivity()).inject(this);
MyFragment.java
24. Defining a Component
@Component
public interface MyAppComponent {
void inject(MyActivity myActivity);
}
MyAppComponent.java
Actual Implementation is generated by Dagger 2.
Just an interface:
25. Depending on a Module
@Component(
modules = {
MyAppModule.class,
}
)
public interface MyAppComponent {
void inject(MyActivity myActivity);
}
MyAppComponent.java
26. Defining a Module
@Module public class MyAppModule {
private final MyApp app;
public MyAppModule(MyApp app) {
this.app = app;
}
@Provides MyApp provideMyApp() {
return app;
}
}
MyAppModule.java
27. Defining a Module
@Module public class MyAppModule {
private final MyApp app;
public MyAppModule(MyApp app) {
this.app = app;
}
@Provides @Singleton MyApp provideMyApp() {
return app;
}
}
MyAppModule.java
28. Defining a Module
@Module public class MyAppModule {
private final MyApp app;
public MyAppModule(MyApp app) {
this.app = app;
}
@Provides @PerApp MyApp provideMyApp() {
return app;
}
}
MyAppModule.java
30. Bringing it all together
public class MyApp extends Application {
private MyComponent component;
@Override public void onCreate() {
component = ???
}
public MyComponent getComponent() { … }
}
MyApp.java
31. Bringing it all together
public class MyApp extends Application {
private MyComponent component;
@Override public void onCreate() {
component = DaggerMyAppComponent.builder()
.myAppModule(new MyAppModule(this))
.build();
}
public MyComponent getComponent() { … }
}
MyApp.java
Just @Inject and don’t think about it.
Does Picasso depends on Okhttp and a disk cache? I don’t care.
Does SessionManager depend on SharedPrefs or a CookieStore? I don’t care.
Does Toolbar need inflating? Doesn’t matter.
Does UberApi depend on Okhttp? Or a disk cache? Is logging enabled in debug builds? I don’t worry about that here.
This is a lot of boilerplate in the activity (and every activity), let’s try to move it into the Application
We can create a static getter method for the App
But we don’t really care about the app, we want the component, so let’s just get that.
This can be repeated for Activities and other objects if you’d like.
But we don’t really care about the app, we want the component, so let’s just get that.
This can be repeated for Activities and other objects if you’d like.
Activity Components can have activity specific objects like Toolbars, Shared UI, etc. Things you would want to inject into Fragments, Views, etc. that you want to only live within the Activity lifecycle.
Define the classes you want to inject
Other thing you want to do is define modules
DaggerMyAppComponent.builder() is generated by dagger.
DaggerMyAppComponent.builder() is generated by dagger.