I'm using Dagger2 to inject my dependencies in all of my applications.
Some days ago I started getting Crash Reports for one of the applications from Samsung Android 7.0 (only these) devices.
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2924)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2985)
..
Caused by: java.lang.ClassCastException:
at de.package.name.MyApplication.get(MyApplication.java:43)
at de.package.name.ui.base.BaseActivity.onCreate(BaseActivity.java:53)
at de.package.name.ui.startup.StartupActivity.onCreate(StartupActivity.java:26)
at android.app.Activity.performCreate(Activity.java:6912)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2877)
MyApplication class:
public class MyApplication extends MultiDexApplication {
private AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
setupAppComponent();
}
private void setupAppComponent() {
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
.userApiModule(new UserApiModule())
.build();
appComponent.inject(this);
}
public static MyApplication get(Context context) {
return (MyApplication) context.getApplicationContext();
}
}
Relevant part of the BaseActivity class:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyApplication.get(this).getAppComponent().inject(this);
}
And finally, the StartupActivity Part:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupComponent(MyApplication.get(this).getAppComponent());
setContentView(R.layout.activity_startup);
startupPresenter.bindView(this);
}
public void setupComponent(AppComponent appComponent) {
startupComponent = DaggerStartupComponent.builder()
.appComponent(appComponent)
.startupModule(new StartupModule())
.build();
startupComponent.inject(this);
}
I already updated Dagger to the most recent version (2.11 for now). But I don't have any ideas about this issue. Also, I can't reproduce it on my Samsung S8 7.0 device.
So if you have any ideas, please let me know!
Cheers
edit: If anyone runs into this problem. Take a look here: RuntimeException with Dagger 2 on Android 7.0 and Samsung devices This might be your solution.