From the documentation, I read that there are two ways to support MultiDex in devices below API 21:
- Having the Application class extend
MultiDexApplication
, and - Using
MultiDex.install(this)
in the Application class'sonAttachBaseContext(Context base)
function in case the Application extends something else.
Are they basically the same with extending MultiDexApplication
calling MultiDex.install(this)
in the onAttachBaseContext()
by default, or are there differences between both methods?
Like you mentioned it's a two "different" approach to solve a "single" problem
The problem with the first approach (i.e extending MultiDexApplication) might be not possible in some cases. like you want to extend your application with some other base class from some library. Since multiple Inheritance is not supported in Java, Android provided the other way to solve the issue and that is "MultiDex.install(this)"
they are two ways to enable multidex for your app and they are completely same
if you want to create a class for application just for enable multidex you can just put MultiDexApplication for application name in AndroidManifest and don't need to do more because in MultiDexApplication overrided attachBaseContext() , look :
and if you have application class end it extends just Application you also can change extends to MultiDexApplication instead of ovverride attachBaseContext() method , otherwise you have application class that it doesn't extends from Application, so you have to override attachBaseContext() and your custom Application class
In some projects you may do not have possibility to do Application class extends MultiDexApplication (for example if your Application already extends some other class). In that case you steel can use MultiDex.install(this) in your Application class.