Modern Android Projects
Dependency injection
Events
Testing
About me
Java EE - 8 years
Android - 2 years
Did some Java desktop, Javascript, PHP, Databases, iOS along the way
Currently Development Manager of Spredfast Mobile (iOS and Android)
Pet projects (github.com/denisk20)

Agenda
- Dependency Injection
- Dagger
- Events
- Otto event bus
- Testing
- Unit tests
- Espresso
Demo project
Dependency Injection
#1
What is D.I. ?
Dependency injection means giving an object its instance variables. Really. That's it.
James Shore, 22 March 2006
class WeatherActivity {
WeatherManager weatherManager;
@Override
protected void onCreate() {
weatherManager = new WeatherManager();
WeatherNetClient netClient = new WeatherNetClient();
netClient.setRequestQueue(new RequestQueue());
weatherManager.setNetClient(netClient);
}
public void refreshWeather() {
weatherManager.fetchWeather();
}
}
public class WeatherActivity {
@Inject WeatherManager weatherManager;
protected onCreate() {
ObjectGraph.create(new WeatherActivityModule())
.inject(this);
}
public void refreshWeather() {
weatherManager.fetchWeather();
}
}@Module
class WeatherActivityModule {
@Provides RequestQueue provideRequestQueue() {
return new RequestQueue();
}
@Provides WeatherNetClient provideWeatherNetClient(RequestQueue queue) {
return new WeatherNetClient(queue);
}
@Provides WeatherManager provideWeatherManager(WeatherNetClient weatherNetClient) {
return new WeatherManager(weatherNetClient);
}
}Google says:
Avoid dependency injection frameworks
Dagger
- Uses code generation (almost no reflection)
- Optimized for Android
Show me how you do it!
Android Dependency Injection summary
Is good when right tools are used
But wait, there's more...
Dagger 2
by Google
@Component(modules = DripCoffeeModule.class)
@Singleton
interface CoffeeShop {
CoffeeMaker maker();
}
- Faster
- Generates code that looks hand-written
- Uses zero reflection => no ProGuard configs
- Module inheritance and many more...
Dagger http://square.github.io/dagger/
(intro talk by Jesse Wilson, Square
http://www.infoq.com/presentations/Dagger)
Dagger 2 http://google.github.io/dagger/
(intro talk by Gregory Kick, Google
Some links
Events
#2
- Better code structuring
- Asynchronous calls
- Help to avoid inner classes
Events are great because:
class WeatherActivity {
WeatherManager weatherManager;
/**
* Updates the weather for the first city from the list
*/
void updateWeather() {
weatherManager.fetchCities(new CitiesCallback() {
@Override
public void citiesUpdated(List<City> cities) {
weatherManager.updateWeatherForCity(cities.get(0), new WeatherCallback() {
@Override
public void weatherUpdated(Weather weather) {
//do something with the weather here
}
});
}
});
}class WeatherActivity {
WeatherManager weatherManager;
public void updateCities() {
weatherManager.fetchCities();
}
/**
* Is called whenever cities list is updated by ANYONE
*/
@Subscribe
public void citiesUpdated(CitiesUpdatedEvent event) {
weatherManager.updateWeatherForCity(event.getCities().get(0));
}
/**
* Is called whenever ANY city is updated
*/
@Subscribe
public void cityUpdated(CityUpdatedEvent event) {
City city = event.getCity();
Weather weather = event.getWeather();
//do something
}
}class WeatherActivity {
WeatherManager weatherManager;
TextView wind;
void updateWeather() {
weatherManager.updateWeather(new WeatherUpdatedCallback() {
@Override
public void weatherUpdated(Weather weather) {
wind.setText(weather.getWind());
}
});
}
}class WeatherActivity {
WeatherManager weatherManager; //should be application-scoped
TextView wind;
void updateWeather() {
weatherManager.updateWeather();
}
@Subscribe
void weatherUpdated(WeatherEvent event) {
wind.setText(event.getWeather().getWind());
}
}
Otto Event Bus to the resque!

by Square
- Dead simple
- Fast
- (as any Square library) Optimized for Android
Bus bus = new Bus();
bus.register(this);
bus.post(new MyEvent(myData));
@Subscribe
public void onMyEvent(MyEvent event) {
//do something with event
}
bus.unregister(this);Show me how you do it!
Summary of Events
- Make code shorter
- Make code less error-prone
- Eliminate inner classes
- Great for complex code
- Great for async code
Some links
Otto
GreenRobot EventBus
(claims to be like 2 times as fast as Otto)
Testing
#3
Testing + Dependency Injection =

Dependency injection makes tests
- possible
- effective
- fun
Jumping straight to the code...
by Google
- superfast
- easy to use
- extensible
- robust
Alternatives:
- Robotium
- Appium

Espresso demo
Some links
Thank you!
Questions?
Modern Android Projects
By Denis Kniazhev
Modern Android Projects
Dependency Injection, Events, Testing
- 826