Posts

Showing posts with the label Android Architecture Components

AndroidViewModel Vs ViewModel

Answer : AndroidViewModel provides Application context If you need to use context inside your Viewmodel you should use AndroidViewModel (AVM), because it contains the application context. To retrieve the context call getApplication() , otherwise use the regular ViewModel (VM). AndroidViewModel has application context . We all know having static context instance is evil as it can cause memory leaks!! However, having static Application instance is not as bad as you might think because there is only one Application instance in the running application. Therefore, using and having Application instance in a specific class is not a problem in general. But, if an Application instance references them, it is a problem because of the reference cycle problem. See Also about Application Instance AndroidViewModel Problematic for unit tests AVM provides application context which is problematic for unit testing. Unit tests should not deal with any of the Android lifecycle, such as con...

Android Architecture Components: Gradle Sync Error For Dependency Version

Answer : As @RedBassett mentions Support libraries depends on this lightweight import (runtime library) as explained at android developers documentation. This is, android.arch.lifecycle:runtime:1.0.0 is spreading up in the dependency tree as a result of an internal api (transitive) import so in my case I only had to include extensions library as "api" instead of "implementation" so that it will override its version to the highest (1.1.1). In conclusion, change implementation "android.arch.lifecycle:extensions:1.1.1" to api "android.arch.lifecycle:extensions:1.1.1" In your main build.gradle file allprojects { ... configurations { all { resolutionStrategy { force "android.arch.lifecycle:runtime:1.1.1" } } } } This will enforce version 1.1.1 Apparently support-v4 was causing the conflict. In the case of this question, the Gradle dependency task wasn...