findViewById in order to reduce an already bloated activity : View someView = (View) findViewById(R.id.someView) View someView; // ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); someView = (View) findViewById(R.id.someView); // } @BindView(R.id.someView1) View view1; @BindView(R.id.someView2) View view2; @BindView(R.id.someView3) View view3; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); // } @BindView annotation @BindView we tell which view we need, and then the main thing is not to forget about ButterKnife.bind(this); (You need to do this in activation, for other places like holder or fragment, they do it a little differently. See here . compile 'com.jakewharton:butterknife:8.5.1' apt 'com.jakewharton:butterknife-compiler:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' jack compile 'com.google.code.gson:gson:2.7' compile 'com.squareup.retrofit2:retrofit:2.1.0' compile 'com.squareup.retrofit2:converter-gson:2.1.0' // rxJava compile 'io.reactivex.rxjava2:rxjava:2.0.1' compile 'io.reactivex.rxjava2:rxandroid:2.0.1' compile 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' 
public interface IWeatherProvider{ @GET("/premium/v1/weather.ashx") Observable<Model> getWeather(@QueryMap Map<String, String> map); } Retrofit retrofit = Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .baseUrl(Constants.WEATHER_BASE_URL) .build(); ... retrofit.create(IWeatherProvider); HashMap<String, String> mapJson = new HashMap<>(); mapJson.put("key", Constants.WEATHER_API_KEY); mapJson.put("q", latitude + "," + longitude); mapJson.put("num_of_days", "14"); mapJson.put("date", "today"); mapJson.put("format", "json"); iweatherProvider.getWeather(mapJson); new CompositeDisposable().add(weatherProvider .observeOn(Schedulers.newThread()) .subscribeOn(Schedulers.io()) .subscribe(onNext, onError)); iweatherProvider.getWeather(mapJson).enqueue(callback); @Component(modules = {WeatherInfoTaskModule.class}) public interface RetrofitComponent { void inject(MainActivity activity); } @Module public class WeatherInfoTaskModule { @Provides Gson provideGson() { Log.d(TAG, "gson"); return new GsonBuilder().create(); } @Provides IWeatherProvider provideWeather(Retrofit retrofit) { Log.d(TAG, "weather"); return retrofit.create(IWeatherProvider.class); } @Provides Retrofit provideRetrofit(Gson gson) { Log.d(TAG, "retrofit"); return new Retrofit.Builder() .addConverterFactory(GsonConverterFactory.create(gson)) .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .baseUrl(Constants.WEATHER_BASE_URL) .build(); } } @Inject IWeatherProvider mWeatherProvider; ). Then create your Application class: public class App extends Application { private static RetrofitComponent component; @Override public void onCreate() { super.onCreate(); component = DaggerRetrofitComponent.create(); } public RetrofitComponent getComponent() { return component; } } @Inject IWeatherProvider mWeatherProvider; ... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ((App) getApplicationContext()).getComponent().inject(this); } buildscript { repositories { mavenCentral() } dependencies { classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } } apply plugin: 'com.neenbedankt.android-apt' apply plugin: 'android-apt' dependencies{ compile 'com.google.dagger:dagger:2.7' apt 'com.google.dagger:dagger-compiler:2.7' } classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' ... defaultConfig { ... multiDexEnabled true ... } ... compile 'com.android.support:multidex:1.0.1' android:name="android.support.multidex.MultiDexApplication" App class from MultiDexApplicationMultiDex.install(this);App classSource: https://habr.com/ru/post/336956/
All Articles