{"id":326753,"date":"2021-07-20T21:00:08","date_gmt":"2021-07-20T21:00:08","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=326753"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=326753","title":{"rendered":"Hilt+Retrofit+Coroutine"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f Hilt \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0441 Retrofit, \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0431\u0434 \u0431\u0443\u0434\u0443\u0442 \u0430\u0441\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Coroutines.<\/p>\n<ul>\n<li>\n<p>Hilt &#8212; \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0434\u043b\u044f Android, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432 \u0434\u043b\u044f \u0440\u0443\u0447\u043d\u043e\u0433\u043e \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0432 \u0432\u0430\u0448 \u043f\u0440\u043e\u0435\u043a\u0442. <a href=\"https:\/\/developer.android.com\/training\/dependency-injection\/hilt-android\" rel=\"noopener noreferrer nofollow\">https:\/\/developer.android.com\/training\/dependency-injection\/hilt-android<\/a><\/p>\n<\/li>\n<li>\n<p>Retrofit &#8212; \u0442\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 HTTP-\u043a\u043b\u0438\u0435\u043d\u0442 \u0434\u043b\u044f Android \u0438 Java. \u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043d\u0435 \u043e\u0441\u043e\u0431\u043e \u043d\u0430\u043f\u0440\u044f\u0433\u044f\u0441\u044c \u0434\u0435\u043b\u0430\u0442\u044c  GET, POST, PUT \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b.<\/p>\n<\/li>\n<li>\n<p>Coroutines &#8212; \u0448\u0430\u0431\u043b\u043e\u043d \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 Android \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e.<\/p>\n<\/li>\n<\/ul>\n<h2>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439<\/h2>\n<p>\u0412 top-level build file, \u0442\u043e \u0435\u0441\u0442\u044c build.gradle \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u0441\u0430\u043c\u043e\u0439 \u043f\u0430\u043f\u043a\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0430 \u043d\u0435 \u0432 app, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"kotlin\">buildscript {     ...     ext.hilt_version = '2.35'     dependencies {         ...         classpath \"com.google.dagger:hilt-android-gradle-plugin:$hilt_version\"     } }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0443\u0436\u0435 \u0432 build.gradle, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0430\u043f\u043e\u0447\u043a\u0435 app, Navigation \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043e\u0440\u0438\u0435\u043d\u0442\u0430\u0446\u0438\u0438 <a href=\"https:\/\/developer.android.com\/guide\/navigation\/navigation-getting-started\" rel=\"noopener noreferrer nofollow\">https:\/\/developer.android.com\/guide\/navigation\/navigation-getting-started<\/a>:<\/p>\n<pre><code class=\"kotlin\">plugins {     id(\"kotlin-kapt\")     id(\"dagger.hilt.android.plugin\") }  android {     ... }  dependencies {     \/\/Hilt     implementation(\"com.google.dagger:hilt-android:$hilt_version\")     kapt(\"com.google.dagger:hilt-android-compiler:$hilt_version\")          \/\/Retrofit     implementation 'com.squareup.retrofit2:retrofit:2.9.0'     implementation 'com.squareup.retrofit2:converter-gson:2.9.0'      \timplementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'   \t   \t\/\/Navigation     implementation(\"androidx.navigation:navigation-fragment-ktx:2.3.5\")     implementation(\"androidx.navigation:navigation-ui-ktx:2.3.5\") }<\/code><\/pre>\n<h2>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u0442\u0430\u0432\u0438\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438<\/h2>\n<p>\u0412 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439 : &#171;\u0412\u0441\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0435 Hilt, \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 Application, \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0439 @HiltAndroidApp &#171;.<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043a\u043b\u0430\u0441\u0441 MyApplication, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c \u043e\u0442 Application:<\/p>\n<pre><code class=\"kotlin\">@HiltAndroidApp class MyApplication : Application() { }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0432 AndroidManifest, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u0441\u0431\u043e\u0440\u043a\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430\u0448 \u043a\u043b\u0430\u0441\u0441:<\/p>\n<pre><code class=\"xml\">android:name=\".MyApplication\"<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e @AndroidEntryPoint \u043a MainFragment \u0438 MainActivity. \u042d\u0442\u0430 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e \u0433\u043e\u0432\u043e\u0440\u0438\u0442 Hilt, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043b \u043a\u043b\u0430\u0441\u0441\u044b Component <a href=\"https:\/\/developer.android.com\/training\/dependency-injection\/hilt-android#generated-components\" rel=\"noopener noreferrer nofollow\">https:\/\/developer.android.com\/training\/dependency-injection\/hilt-android#generated-components<\/a> . \u041a\u0430\u0436\u0434\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0441\u0432\u043e\u0435\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, ActivityComponent \u0437\u0430 Activity, FragmentComponent \u0437\u0430 Fragment. \u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u0430 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0438 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043a Fragment, Activity \u0438 \u0442.\u0434.. \u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u0443\u043a\u0430\u0437\u0430\u043b\u0438 \u044d\u0442\u0443 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443 \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430, \u0435\u0451 \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0443 Activity, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u043d \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d. <a href=\"https:\/\/developer.android.com\/training\/dependency-injection\/hilt-android#component-hierarchy\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/a>.<\/p>\n<pre><code class=\"kotlin\">@AndroidEntryPoint class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)     } }<\/code><\/pre>\n<pre><code class=\"kotlin\">@AndroidEntryPoint class MainFragment : Fragment() {      override fun onCreateView(         inflater: LayoutInflater, container: ViewGroup?,         savedInstanceState: Bundle?     ): View? {         \/\/ Inflate the layout for this fragment         return inflater.inflate(R.layout.fragment_main, container, false)     } }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 <a href=\"https:\/\/medium.com\/nuances-of-programming\/mvvm-\u043d\u0430-android-\u0441-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438-\u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b-\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430-koin-e2e77b77950e\" rel=\"noopener noreferrer nofollow\">\u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043e\u0439 MVVM<\/a> \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c ViewModel, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0430\u0448\u0438 LiveData, \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0438\u0437 MainRepository:<\/p>\n<pre><code class=\"kotlin\">@Singleton class MainRepository @Inject constructor() {   ... }<\/code><\/pre>\n<p>\u0410\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f @Singleton \u0433\u043e\u0432\u043e\u0440\u0438\u0442 Hilt, \u0447\u0442\u043e \u043d\u0430\u0448 MainRepository \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d \u043a SIngletonComponent, \u0442\u043e \u0435\u0441\u0442\u044c \u043a Application. \u041f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0433\u0434\u0435-\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f MainRepository, \u043e\u043d \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u0441\u0430\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430, \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0440\u0430\u0437\u044b \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u043e\u0442 \u0436\u0435 \u0441\u0430\u043c\u044b\u0439, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0432 1 \u0440\u0430\u0437 MainRepository. \u041d\u043e \u0447\u0442\u043e\u0431\u044b Hilt \u0437\u043d\u0430\u043b, \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439, \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 : @Inject constructor(&#8230;.). \u0415\u0441\u043b\u0438 \u043c\u044b \u0434\u0430\u043b\u0435\u0435 \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u0433\u0434\u0435-\u0442\u043e \u0432 \u043a\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c MainRepository, \u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a: @Inject lateinit var repository: MainRepository.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0431\u044a\u044f\u0432\u0438\u043c ViewModel:<\/p>\n<pre><code class=\"kotlin\">@HiltViewModel class MainViewModel @Inject constructor(val repository: MainRepository) : ViewModel(){ \t... }<\/code><\/pre>\n<p>\u0412 Hilt \u0434\u043b\u044f ViewModel \u0435\u0441\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f @HiltViewModel , ViewModel \u0442\u0430\u043a\u0436\u0435 \u043a\u0430\u043a \u0438 \u0440\u0430\u043d\u044c\u0448\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u044b \u043a \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u0430\u043c Fragment \u0438\u043b\u0438 Activity, \u0441\u043c\u043e\u0442\u0440\u044f \u0433\u0434\u0435 \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u0435\u0451 \u0441\u043e\u0437\u0434\u0430\u0442\u044c. \u0422\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c @Inject, \u043f\u043e \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c \u0432\u044b\u0448\u0435, \u0430 \u0442\u043a Hilt \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u0442, \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c repository, \u0432\u0441\u0451 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c(\u043f\u0440\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0438 @Inject , Hilt \u0434\u043e\u043b\u0436\u0435\u043d \u0437\u043d\u0430\u0442\u044c, \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 constructor). \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0430\u0448\u0443 ViewModel:<\/p>\n<pre><code class=\"kotlin\">private val viewModel by viewModels&lt;MainViewModel&gt;()<\/code><\/pre>\n<p>\u0412\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0440\u0435\u0437\u043e\u043d\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441, \u0437\u0430\u0447\u0435\u043c \u0432\u0441\u0435 \u044d\u0442\u0438 \u0437\u0430\u043c\u043e\u0440\u043e\u0447\u043a\u0438 \u0441 Hilt, \u0435\u0441\u043b\u0438 \u043c\u044b \u0438 \u0434\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0433\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c ViewModel \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e by viewModels. \u0412\u0441\u0451 \u0434\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0442\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0443 \u043d\u0430\u0448\u0435\u0439 ViewModel \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0447\u0442\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 MainViewModel \u043d\u0435 \u0442\u0430\u043a. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u0438 \u0445\u043e\u0442\u0438\u043c \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c.<\/p>\n<h2>Retrofit<\/h2>\n<p>\u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 Retrofit \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430, \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0443\u0434\u0430\u043b\u0451\u043d\u043d\u043e\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445(\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f Firebase), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u0433\u043e\u043c &lt;application \u0432 AndroidManifest.xml \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u0447\u043a\u0443:<\/p>\n<pre><code class=\"xml\">&lt;uses-permission android:name=\"android.permission.INTERNET\"\/&gt;<\/code><\/pre>\n<p>C\u043e\u0437\u0434\u0430\u0434\u0438\u043c data class, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0443\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435:<\/p>\n<pre><code class=\"kotlin\">data class User(     val age : Int,     val name : String )<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0451\u043c interface, \u043d\u0443\u0436\u043d\u044b\u0439 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f Retrofit, \u0433\u0434\u0435 \u043f\u0440\u043e\u043f\u0438\u0448\u0435\u043c \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u044b\u0435 \u043d\u0430\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<pre><code class=\"kotlin\">interface MainService {      @GET(\"User\/{userId}.json\")     suspend fun getUser(@Path(\"userId\") userId : Int) : Response&lt;User&gt; }<\/code><\/pre>\n<p>\u041f\u043e\u043a\u0430 \u0443 \u043d\u0430\u0441 1 \u0437\u0430\u043f\u0440\u043e\u0441 GET. \u0410\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f @Path \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 userId \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0432 \u0441\u0435\u0433\u043c\u0435\u043d\u0442 URL {userId}.<\/p>\n<pre><code class=\"kotlin\">class MainRemoteData @Inject constructor(private val mainService : MainService) {          suspend fun getUser(userId : Int) = mainService.getUser(userId) }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0442\u0443\u0442 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u0442\u0432\u0435\u0442\u0430: \u043a\u043e\u0434\u0430 \u043e\u0442\u0432\u0435\u0442\u0430, \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435 \u0438 \u0442.\u0434..<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0443 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043d\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 Hilt \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0437\u043d\u0430\u0442\u044c \u043a\u0430\u043a \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c. \u0422\u0430\u043a\u0436\u0435 Retrofit \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435, \u0437\u043d\u0430\u0447\u0438\u0442 \u043e \u043d\u0435\u0439 Hilt \u0442\u043e\u0436\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442, \u043a\u0430\u043a \u0436\u0435 \u0442\u043e\u0433\u0434\u0430 \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u0442\u044c, \u0447\u0442\u043e\u0431\u044b Hilt \u0437\u043d\u0430\u043b \u043a\u0430\u043a \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u0442\u043e\u0433\u043e \u0438\u043b\u0438 \u0438\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430? \u041d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0451\u0442 Module, \u043e\u043d \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043d\u0430\u0431\u043e\u0440 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043d\u0430\u043c \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<pre><code class=\"kotlin\">@Module @InstallIn(SingletonComponent::class) object AppModule {      @Provides     fun providesBaseUrl() : String = \"https:\/\/example-hilt-retrofit-default-rtdb.firebaseio.com\/\"      @Provides     @Singleton     fun provideRetrofit(BASE_URL : String) : Retrofit = Retrofit.Builder()         .addConverterFactory(GsonConverterFactory.create())         .baseUrl(BASE_URL)         .build()      @Provides     @Singleton     fun provideMainService(retrofit : Retrofit) : MainService = retrofit.create(MainService::class.java)      @Provides     @Singleton     fun provideMainRemoteData(mainService : MainService) : MainRemoteData = MainRemoteData(mainService) }<\/code><\/pre>\n<p>\u0410\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f @InstallIn \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0447\u0435\u0433\u043e \u0431\u0443\u0434\u0443\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u043d\u0430\u0448\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0438\u0445 \u0432 \u043b\u044e\u0431\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c SingletonComponent. \u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e @Provides, \u0447\u0442\u043e\u0431\u044b Hilt \u0437\u043d\u0430\u043b \u043a\u0430\u043a \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u0442\u0438\u043f\u043e\u0432 Retrofit \u0438 \u0442.\u0434.. \u0410\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f @Singleton , \u043a\u0430\u043a \u0431\u044b\u043b\u043e \u0441\u043a\u0430\u0437\u0430\u043d\u043e \u0440\u0430\u043d\u0435\u0435, \u0433\u043e\u0432\u043e\u0440\u0438\u0442 Hilt, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043d\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440, \u0430 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u043b \u0443\u0436\u0435 \u0438\u043c\u0435\u044e\u0449\u0438\u0439\u0441\u044f(\u0435\u0441\u043b\u0438 \u043d\u0438 \u0440\u0430\u0437\u0443 \u0435\u0449\u0451 \u043d\u0435 \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d, \u0442\u043e \u043e\u043d \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0441\u0442).<\/p>\n<p>\u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043c, \u043a\u0430\u043a \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 String, \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0433\u0434\u0435-\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 \u043f\u043e \u0442\u0438\u043f\u0443 \u0442\u0430\u043a\u043e\u0433\u043e:<\/p>\n<pre><code class=\"kotlin\">@Singleton class Test @Inject constructor(private val str : String) {      fun print() {         println(str)     } }<\/code><\/pre>\n<p>\u0422\u043e \u043f\u0440\u0438 \u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e:<\/p>\n<pre><code class=\"kotlin\">@Inject lateinit var test : Test<\/code><\/pre>\n<p>\u0418 \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 print(), \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e BASE_URL. <\/p>\n<h2>\u041f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u043e ViewModel<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e \u0441\u0443\u0442\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u043e ViewModel, \u0430 \u043f\u043e\u0442\u043e\u043c \u0438\u0437 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u0434\u043e\u0441\u0442\u0430\u0442\u044c LiveData \u0438 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043d\u0435\u0439.<\/p>\n<p>\u041f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439:<\/p>\n<pre><code class=\"kotlin\">@Singleton class MainRepository @Inject constructor(     private val remoteData : MainRemoteData ) {      suspend fun getUser(userId : Int) = remoteData.getUser(userId) }<\/code><\/pre>\n<p>\u041f\u043e\u0442\u043e\u043c \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0438\u0445 \u0432 LiveData \u0432\u043e ViewModel:<\/p>\n<pre><code class=\"kotlin\">@HiltViewModel class MainViewModel @Inject constructor(val repository: MainRepository) : ViewModel(){      fun getUser(userId : Int) : LiveData&lt;User&gt; {         return liveData {             val data = repository.getUser(userId)             data.body()?.let { emit(it) }         }     } }<\/code><\/pre>\n<p>\u0412\u044b\u0437\u043e\u0432 emit () \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0430 \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 LiveData \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u0422\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u043a\u0430 \u043d\u0430\u0448\u0430 suspend fun \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c\u0441\u044f.<\/p>\n<h2>UI<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u0437 Firebase. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432 MainFragment \u0434\u043e\u0431\u0430\u0432\u0438\u043c 2 Textview:<\/p>\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt; &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"     xmlns:tools=\"http:\/\/schemas.android.com\/tools\"     android:layout_width=\"match_parent\"     android:layout_height=\"match_parent\"     xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"     tools:context=\".MainFragment\"&gt;      &lt;!-- TODO: Update blank fragment layout --&gt;      &lt;TextView         android:id=\"@+id\/name_field\"         android:layout_width=\"0dp\"         android:layout_height=\"wrap_content\"         android:text=\"name\"         android:textSize=\"50sp\"          android:layout_marginTop=\"24dp\"         android:layout_marginLeft=\"24dp\"         android:layout_marginRight=\"24dp\"          app:layout_constraintLeft_toLeftOf=\"parent\"         app:layout_constraintRight_toRightOf=\"parent\" \/&gt;      &lt;TextView         android:id=\"@+id\/age_field\"         android:layout_width=\"0dp\"         android:layout_height=\"wrap_content\"         android:textSize=\"50sp\"         android:text=\"age\"          android:layout_marginTop=\"24dp\"         android:layout_marginLeft=\"24dp\"         android:layout_marginRight=\"24dp\"          app:layout_constraintLeft_toLeftOf=\"parent\"         app:layout_constraintRight_toRightOf=\"parent\"         app:layout_constraintTop_toBottomOf=\"@+id\/name_field\" \/&gt;  &lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;<\/code><\/pre>\n<p>\u0418 \u0432 MainFragment \u0431\u0443\u0434\u0435\u043c \u0443\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u0438 \u0438\u0437 ViewModel:<\/p>\n<pre><code class=\"kotlin\">@AndroidEntryPoint class MainFragment : Fragment() {      private val viewModel by viewModels&lt;MainViewModel&gt;()      @SuppressLint(\"SetTextI18n\")     override fun onCreateView(         inflater: LayoutInflater, container: ViewGroup?,         savedInstanceState: Bundle?     ): View {         val constraintLayout = inflater.inflate(R.layout.fragment_main, container, false)                 as ConstraintLayout         val name : TextView = constraintLayout.findViewById(R.id.name_field)         val age : TextView = constraintLayout.findViewById(R.id.age_field)         viewModel.getUser(0).observe(viewLifecycleOwner, {user -&gt;             name.text = \"name : \" + user.name             age.text = \"age : \" + user.age.toString()         })         return constraintLayout     } }<\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442(\u043a\u043e\u043d\u0435\u0447\u043d\u043e \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043a\u0440\u0430\u0441\u0438\u0432\u044b\u0439, \u043d\u043e \u043c\u044b \u043a \u044d\u0442\u043e\u043c\u0443 \u0438 \u043d\u0435 \u0441\u0442\u0440\u0435\u043c\u0438\u043b\u0438\u0441\u044c):<\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/18c\/16e\/8a4\/18c16e8a45df4375bd2938c85093ca83.png\" width=\"1080\" height=\"1920\"><figcaption><\/figcaption><\/figure>\n<p>\u0412\u043e\u0442 \u0438 \u0432\u0441\u0451, \u043f\u0440\u043e\u0441\u0442\u043e\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f Hilt+retrofit \u0433\u043e\u0442\u043e\u0432! <a href=\"https:\/\/github.com\/DbnStr\/Retrofit-Hilt_Example\" rel=\"noopener noreferrer nofollow\">\u041f\u043e\u043b\u043d\u044b\u0439 \u043a\u043e\u0434 \u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/a>.<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/568792\/\"> https:\/\/habr.com\/ru\/post\/568792\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u044b\u0439 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f Hilt \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e \u0441 Retrofit, \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0431\u0434 \u0431\u0443\u0434\u0443\u0442 \u0430\u0441\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e Coroutines.<\/p>\n<ul>\n<li>\n<p>Hilt &#8212; \u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0434\u043b\u044f Android, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0441\u043e\u043a\u0440\u0430\u0449\u0430\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u043e\u0432 \u0434\u043b\u044f \u0440\u0443\u0447\u043d\u043e\u0433\u043e \u0432\u043d\u0435\u0434\u0440\u0435\u043d\u0438\u044f \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u0432 \u0432\u0430\u0448 \u043f\u0440\u043e\u0435\u043a\u0442. <a href=\"https:\/\/developer.android.com\/training\/dependency-injection\/hilt-android\" rel=\"noopener noreferrer nofollow\">https:\/\/developer.android.com\/training\/dependency-injection\/hilt-android<\/a><\/p>\n<\/li>\n<li>\n<p>Retrofit &#8212; \u0442\u0438\u043f\u043e\u0431\u0435\u0437\u043e\u043f\u0430\u0441\u043d\u044b\u0439 HTTP-\u043a\u043b\u0438\u0435\u043d\u0442 \u0434\u043b\u044f Android \u0438 Java. \u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u043d\u0435 \u043e\u0441\u043e\u0431\u043e \u043d\u0430\u043f\u0440\u044f\u0433\u044f\u0441\u044c \u0434\u0435\u043b\u0430\u0442\u044c  GET, POST, PUT \u0438 \u0434\u0440\u0443\u0433\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b.<\/p>\n<\/li>\n<li>\n<p>Coroutines &#8212; \u0448\u0430\u0431\u043b\u043e\u043d \u043f\u0440\u043e\u0435\u043a\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u0440\u0430\u043b\u043b\u0435\u043b\u0438\u0437\u043c\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432 Android \u0434\u043b\u044f \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u0438\u044f \u043a\u043e\u0434\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e.<\/p>\n<\/li>\n<\/ul>\n<h2>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439<\/h2>\n<p>\u0412 top-level build file, \u0442\u043e \u0435\u0441\u0442\u044c build.gradle \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u044c\u0441\u044f \u0432 \u0441\u0430\u043c\u043e\u0439 \u043f\u0430\u043f\u043a\u0435 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0430 \u043d\u0435 \u0432 app, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:<\/p>\n<pre><code class=\"kotlin\">buildscript {     ...     ext.hilt_version = '2.35'     dependencies {         ...         classpath \"com.google.dagger:hilt-android-gradle-plugin:$hilt_version\"     } }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0443\u0436\u0435 \u0432 build.gradle, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 \u043f\u0430\u043f\u043e\u0447\u043a\u0435 app, Navigation \u0434\u043b\u044f \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u0430 \u043e\u0440\u0438\u0435\u043d\u0442\u0430\u0446\u0438\u0438 <a href=\"https:\/\/developer.android.com\/guide\/navigation\/navigation-getting-started\" rel=\"noopener noreferrer nofollow\">https:\/\/developer.android.com\/guide\/navigation\/navigation-getting-started<\/a>:<\/p>\n<pre><code class=\"kotlin\">plugins {     id(\"kotlin-kapt\")     id(\"dagger.hilt.android.plugin\") }  android {     ... }  dependencies {     \/\/Hilt     implementation(\"com.google.dagger:hilt-android:$hilt_version\")     kapt(\"com.google.dagger:hilt-android-compiler:$hilt_version\")          \/\/Retrofit     implementation 'com.squareup.retrofit2:retrofit:2.9.0'     implementation 'com.squareup.retrofit2:converter-gson:2.9.0'      \timplementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.2.0'   \t   \t\/\/Navigation     implementation(\"androidx.navigation:navigation-fragment-ktx:2.3.5\")     implementation(\"androidx.navigation:navigation-ui-ktx:2.3.5\") }<\/code><\/pre>\n<h2>\u0422\u0435\u043f\u0435\u0440\u044c \u0440\u0430\u0441\u0441\u0442\u0430\u0432\u0438\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u0438<\/h2>\n<p>\u0412 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0435\u0439 : &#171;\u0412\u0441\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0438\u0435 Hilt, \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 Application, \u043f\u043e\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u0439 @HiltAndroidApp &#171;.<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0441\u043e\u0437\u0434\u0430\u0451\u043c \u043a\u043b\u0430\u0441\u0441 MyApplication, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0430\u0441\u043b\u0435\u0434\u0443\u0435\u043c \u043e\u0442 Application:<\/p>\n<pre><code class=\"kotlin\">@HiltAndroidApp class MyApplication : Application() { }<\/code><\/pre>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0435\u043c \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u044d\u0442\u043e\u0442 \u043a\u043b\u0430\u0441\u0441 \u0432 AndroidManifest, \u0447\u0442\u043e\u0431\u044b \u043f\u0440\u0438 \u0441\u0431\u043e\u0440\u043a\u0435 \u0432\u0441\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e \u043d\u0430\u0448 \u043a\u043b\u0430\u0441\u0441:<\/p>\n<pre><code class=\"xml\">android:name=\".MyApplication\"<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e @AndroidEntryPoint \u043a MainFragment \u0438 MainActivity. \u042d\u0442\u0430 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e \u0433\u043e\u0432\u043e\u0440\u0438\u0442 Hilt, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043b \u043a\u043b\u0430\u0441\u0441\u044b Component <a href=\"https:\/\/developer.android.com\/training\/dependency-injection\/hilt-android#generated-components\" rel=\"noopener noreferrer nofollow\">https:\/\/developer.android.com\/training\/dependency-injection\/hilt-android#generated-components<\/a> . \u041a\u0430\u0436\u0434\u044b\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0441\u0432\u043e\u0435\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430, ActivityComponent \u0437\u0430 Activity, FragmentComponent \u0437\u0430 Fragment. \u0418\u043c\u0435\u043d\u043d\u043e \u044d\u0442\u0430 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u0438 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043a Fragment, Activity \u0438 \u0442.\u0434.. \u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u043b\u0438 \u0432\u044b \u0443\u043a\u0430\u0437\u0430\u043b\u0438 \u044d\u0442\u0443 \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443 \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430, \u0435\u0451 \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0443 Activity, \u043a \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u043e\u043d \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d. <a href=\"https:\/\/developer.android.com\/training\/dependency-injection\/hilt-android#component-hierarchy\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0440\u043e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435<\/a>.<\/p>\n<pre><code class=\"kotlin\">@AndroidEntryPoint class MainActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)         setContentView(R.layout.activity_main)     } }<\/code><\/pre>\n<pre><code class=\"kotlin\">@AndroidEntryPoint class MainFragment : Fragment() {      override fun onCreateView(         inflater: LayoutInflater, container: ViewGroup?,         savedInstanceState: Bundle?     ): View? {         \/\/ Inflate the layout for this fragment         return inflater.inflate(R.layout.fragment_main, container, false)     } }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0432 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0438\u0438 \u0441 <a href=\"https:\/\/medium.com\/nuances-of-programming\/mvvm-\u043d\u0430-android-\u0441-\u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430\u043c\u0438-\u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u044b-\u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430-koin-e2e77b77950e\" rel=\"noopener noreferrer nofollow\"> rel=&#187;noopener noreferrer nofollow&#187;>\u0430\u0440\u0445\u0438\u0442\u0435\u043a\u0442\u0443\u0440\u043e\u0439 MVVM<\/a> \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u043e\u0437\u0434\u0430\u0442\u044c ViewModel, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u044c \u043d\u0430\u0448\u0438 LiveData, \u0434\u0430\u043d\u043d\u044b\u0435 \u0432 \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0438\u0437 MainRepository:<\/p>\n<pre><code class=\"kotlin\">@Singleton class MainRepository @Inject constructor() {   ... }<\/code><\/pre>\n<p>\u0410\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f @Singleton \u0433\u043e\u0432\u043e\u0440\u0438\u0442 Hilt, \u0447\u0442\u043e \u043d\u0430\u0448 MainRepository \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d \u043a SIngletonComponent, \u0442\u043e \u0435\u0441\u0442\u044c \u043a Application. \u041f\u0435\u0440\u0432\u044b\u0439 \u0440\u0430\u0437, \u043a\u043e\u0433\u0434\u0430 \u0433\u0434\u0435-\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c\u0441\u044f MainRepository, \u043e\u043d \u0441\u043e\u0437\u0434\u0430\u0441\u0442 \u0441\u0430\u043c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043a\u043b\u0430\u0441\u0441\u0430, \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u0440\u0430\u0437\u044b \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0432\u0430\u0442\u044c\u0441\u044f \u0442\u043e\u0442 \u0436\u0435 \u0441\u0430\u043c\u044b\u0439, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u0432 1 \u0440\u0430\u0437 MainRepository. \u041d\u043e \u0447\u0442\u043e\u0431\u044b Hilt \u0437\u043d\u0430\u043b, \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439, \u043d\u0443\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a\u043e\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 : @Inject constructor(&#8230;.). \u0415\u0441\u043b\u0438 \u043c\u044b \u0434\u0430\u043b\u0435\u0435 \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u0433\u0434\u0435-\u0442\u043e \u0432 \u043a\u043e\u0434\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c MainRepository, \u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0442\u0430\u043a: @Inject lateinit var repository: MainRepository.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043e\u0431\u044a\u044f\u0432\u0438\u043c ViewModel:<\/p>\n<pre><code class=\"kotlin\">@HiltViewModel class MainViewModel @Inject constructor(val repository: MainRepository) : ViewModel(){ \t... }<\/code><\/pre>\n<p>\u0412 Hilt \u0434\u043b\u044f ViewModel \u0435\u0441\u0442\u044c \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f @HiltViewModel , ViewModel \u0442\u0430\u043a\u0436\u0435 \u043a\u0430\u043a \u0438 \u0440\u0430\u043d\u044c\u0448\u0435 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0438\u0432\u044f\u0437\u0430\u043d\u044b \u043a \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u044b\u043c \u0446\u0438\u043a\u043b\u0430\u043c Fragment \u0438\u043b\u0438 Activity, \u0441\u043c\u043e\u0442\u0440\u044f \u0433\u0434\u0435 \u0432\u044b \u0437\u0430\u0445\u043e\u0442\u0438\u0442\u0435 \u0435\u0451 \u0441\u043e\u0437\u0434\u0430\u0442\u044c. \u0422\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c @Inject, \u043f\u043e \u043f\u0440\u0438\u0447\u0438\u043d\u0430\u043c \u043e\u043f\u0438\u0441\u0430\u043d\u043d\u044b\u043c \u0432\u044b\u0448\u0435, \u0430 \u0442\u043a Hilt \u0443\u0436\u0435 \u0437\u043d\u0430\u0435\u0442, \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c repository, \u0432\u0441\u0451 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c(\u043f\u0440\u0438 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u0438 @Inject , Hilt \u0434\u043e\u043b\u0436\u0435\u043d \u0437\u043d\u0430\u0442\u044c, \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0430\u0445\u043e\u0434\u044f\u0442\u0441\u044f \u0432 constructor). \u0422\u0435\u043f\u0435\u0440\u044c \u0432\u043e \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0430\u0448\u0443 ViewModel:<\/p>\n<pre><code class=\"kotlin\">private val viewModel by viewModels&lt;MainViewModel&gt;()<\/code><\/pre>\n<p>\u0412\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u0440\u0435\u0437\u043e\u043d\u043d\u044b\u0439 \u0432\u043e\u043f\u0440\u043e\u0441, \u0437\u0430\u0447\u0435\u043c \u0432\u0441\u0435 \u044d\u0442\u0438 \u0437\u0430\u043c\u043e\u0440\u043e\u0447\u043a\u0438 \u0441 Hilt, \u0435\u0441\u043b\u0438 \u043c\u044b \u0438 \u0434\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0433\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c ViewModel \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e by viewModels. \u0412\u0441\u0451 \u0434\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0442\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0443 \u043d\u0430\u0448\u0435\u0439 ViewModel \u043f\u0443\u0441\u0442\u043e\u0439 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440, \u0447\u0442\u043e \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0441 MainViewModel \u043d\u0435 \u0442\u0430\u043a. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0448\u0430\u0431\u043b\u043e\u043d\u043d\u043e\u0433\u043e \u043a\u043e\u0434\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u0438 \u0445\u043e\u0442\u0438\u043c \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c.<\/p>\n<h2>Retrofit<\/h2>\n<p>\u041f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 Retrofit \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0440\u0430\u0437\u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043d\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u043d\u0442\u0435\u0440\u043d\u0435\u0442\u0430, \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0443\u0434\u0430\u043b\u0451\u043d\u043d\u043e\u0439 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445(\u0432 \u0434\u0430\u043d\u043d\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f Firebase), \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0435\u0440\u0435\u0434 \u0442\u0435\u0433\u043e\u043c &lt;application \u0432 AndroidManifest.xml \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0443\u044e \u0441\u0442\u0440\u043e\u0447\u043a\u0443:<\/p>\n<pre><code class=\"xml\">&lt;uses-permission android:name=\"android.permission.INTERNET\"\/&gt;<\/code><\/pre>\n<p>C\u043e\u0437\u0434\u0430\u0434\u0438\u043c data class, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0443\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435:<\/p>\n<pre><code class=\"kotlin\">data class User(     val age : Int,     val name : String )<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0437\u0434\u0430\u0451\u043c interface, \u043d\u0443\u0436\u043d\u044b\u0439 \u0434\u043b\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f Retrofit, \u0433\u0434\u0435 \u043f\u0440\u043e\u043f\u0438\u0448\u0435\u043c \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u044b\u0435 \u043d\u0430\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u044b:<\/p>\n<pre><code class=\"kotlin\">interface MainService {      @GET(\"User\/{userId}.json\")     suspend fun getUser(@Path(\"userId\") userId : Int) : Response&lt;User&gt; }<\/code><\/pre>\n<p>\u041f\u043e\u043a\u0430 \u0443 \u043d\u0430\u0441 1 \u0437\u0430\u043f\u0440\u043e\u0441 GET. \u0410\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f @Path \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 userId \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c\u0441\u044f \u0432 \u0441\u0435\u0433\u043c\u0435\u043d\u0442 URL {userId}.<\/p>\n<pre><code class=\"kotlin\">class MainRemoteData @Inject constructor(private val mainService : MainService) {          suspend fun getUser(userId : Int) = mainService.getUser(userId) }<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0442\u0443\u0442 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043b\u043e\u0433\u0438\u043a\u0443 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u043e\u0442\u0432\u0435\u0442\u0430: \u043a\u043e\u0434\u0430 \u043e\u0442\u0432\u0435\u0442\u0430, \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u044f \u043e\u0431 \u043e\u0448\u0438\u0431\u043a\u0435 \u0438 \u0442.\u0434..<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0443 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043d\u0435\u0442 \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u0430, \u0430 \u0437\u043d\u0430\u0447\u0438\u0442 Hilt \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u0437\u043d\u0430\u0442\u044c \u043a\u0430\u043a \u0435\u0433\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c. \u0422\u0430\u043a\u0436\u0435 Retrofit \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0435, \u0437\u043d\u0430\u0447\u0438\u0442 \u043e \u043d\u0435\u0439 Hilt \u0442\u043e\u0436\u0435 \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0437\u043d\u0430\u0435\u0442, \u043a\u0430\u043a \u0436\u0435 \u0442\u043e\u0433\u0434\u0430 \u043f\u043e\u0441\u0442\u0443\u043f\u0438\u0442\u044c, \u0447\u0442\u043e\u0431\u044b Hilt \u0437\u043d\u0430\u043b \u043a\u0430\u043a \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u0442\u043e\u0433\u043e \u0438\u043b\u0438 \u0438\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430? \u041d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0451\u0442 Module, \u043e\u043d \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u043e\u0439 \u043d\u0430\u0431\u043e\u0440 \u043c\u0435\u0442\u043e\u0434\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 \u043a\u0430\u043a \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043d\u0430\u043c \u043a\u043b\u0430\u0441\u0441\u0430:<\/p>\n<pre><code class=\"kotlin\">@Module @InstallIn(SingletonComponent::class) object AppModule {      @Provides     fun providesBaseUrl() : String = \"https:\/\/example-hilt-retrofit-default-rtdb.firebaseio.com\/\"      @Provides     @Singleton     fun provideRetrofit(BASE_URL : String) : Retrofit = Retrofit.Builder()         .addConverterFactory(GsonConverterFactory.create())         .baseUrl(BASE_URL)         .build()      @Provides     @Singleton     fun provideMainService(retrofit : Retrofit) : MainService = retrofit.create(MainService::class.java)      @Provides     @Singleton     fun provideMainRemoteData(mainService : MainService) : MainRemoteData = MainRemoteData(mainService) }<\/code><\/pre>\n<p>\u0410\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f @InstallIn \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u0432 \u043a\u043e\u043d\u0442\u0435\u043a\u0441\u0442\u0435 \u0447\u0435\u0433\u043e \u0431\u0443\u0434\u0443\u0442 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u043d\u0430\u0448\u0438 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0438\u0445 \u0432 \u043b\u044e\u0431\u043e\u043c \u043c\u0435\u0441\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438 \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c SingletonComponent. \u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u0443\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u043c \u0430\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044e @Provides, \u0447\u0442\u043e\u0431\u044b Hilt \u0437\u043d\u0430\u043b \u043a\u0430\u043a \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440\u044b \u0442\u0438\u043f\u043e\u0432 Retrofit \u0438 \u0442.\u0434.. \u0410\u043d\u043d\u043e\u0442\u0430\u0446\u0438\u044f @Singleton , \u043a\u0430\u043a \u0431\u044b\u043b\u043e \u0441\u043a\u0430\u0437\u0430\u043d\u043e \u0440\u0430\u043d\u0435\u0435, \u0433\u043e\u0432\u043e\u0440\u0438\u0442 Hilt, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u043d\u0435 \u043f\u0435\u0440\u0435\u0441\u043e\u0437\u0434\u0430\u0432\u0430\u043b \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043d\u043e\u0432\u044b\u0439 \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440, \u0430 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u043b \u0443\u0436\u0435 \u0438\u043c\u0435\u044e\u0449\u0438\u0439\u0441\u044f(\u0435\u0441\u043b\u0438 \u043d\u0438 \u0440\u0430\u0437\u0443 \u0435\u0449\u0451 \u043d\u0435 \u0431\u044b\u043b \u0441\u043e\u0437\u0434\u0430\u043d, \u0442\u043e \u043e\u043d \u0435\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0441\u0442).<\/p>\n<p>\u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u0433\u043e\u0432\u043e\u0440\u0438\u043c, \u043a\u0430\u043a \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u044d\u043a\u0437\u0435\u043c\u043f\u043b\u044f\u0440 String, \u044d\u0442\u043e \u0437\u043d\u0430\u0447\u0438\u0442, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043c\u044b \u0433\u0434\u0435-\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441 \u043f\u043e \u0442\u0438\u043f\u0443 \u0442\u0430\u043a\u043e\u0433\u043e:<\/p>\n<pre><code class=\"kotlin\">@Singleton class Test @Inject constructor(private val str : String) {      fun print() {         println(str)     } }<\/code><\/pre>\n<p>\u0422\u043e \u043f\u0440\u0438 \u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e:<\/p>\n<pre><code class=\"kotlin\">@Inject lateinit var test : Test<\/code><\/pre>\n<p>\u0418 \u0432\u044b\u0437\u043e\u0432\u0435 \u043c\u0435\u0442\u043e\u0434\u0430 print(), \u0431\u0443\u0434\u0435\u0442 \u043f\u0435\u0447\u0430\u0442\u0430\u0442\u044c\u0441\u044f \u0438\u043c\u0435\u043d\u043d\u043e BASE_URL. <\/p>\n<h2>\u041f\u0435\u0440\u0435\u0434\u0430\u0451\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u043e ViewModel<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043e \u0441\u0443\u0442\u0438 \u043e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0430, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u043d\u0430\u0448\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0432\u043e ViewModel, \u0430 \u043f\u043e\u0442\u043e\u043c \u0438\u0437 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u0430 \u0434\u043e\u0441\u0442\u0430\u0442\u044c LiveData \u0438 \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u043d\u0435\u0439.<\/p>\n<p>\u041f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439:<\/p>\n<pre><code class=\"kotlin\">@Singleton class MainRepository @Inject constructor(     private val remoteData : MainRemoteData ) {      suspend fun getUser(userId : Int) = remoteData.getUser(userId) }<\/code><\/pre>\n<p>\u041f\u043e\u0442\u043e\u043c \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0438\u0445 \u0432 LiveData \u0432\u043e ViewModel:<\/p>\n<pre><code class=\"kotlin\">@HiltViewModel class MainViewModel @Inject constructor(val repository: MainRepository) : ViewModel(){      fun getUser(userId : Int) : LiveData&lt;User&gt; {         return liveData {             val data = repository.getUser(userId)             data.body()?.let { emit(it) }         }     } }<\/code><\/pre>\n<p>\u0412\u044b\u0437\u043e\u0432 emit () \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0430 \u0434\u043e \u0442\u0435\u0445 \u043f\u043e\u0440, \u043f\u043e\u043a\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 LiveData \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u043e \u0432 \u043e\u0441\u043d\u043e\u0432\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u0422\u043e \u0435\u0441\u0442\u044c \u043f\u043e\u043a\u0430 \u043d\u0430\u0448\u0430 suspend fun \u043d\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c\u0441\u044f.<\/p>\n<h2>UI<\/h2>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u043a\u043e\u043d\u0435\u0446 \u043e\u0442\u043e\u0431\u0440\u0430\u0437\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0438\u0437 Firebase. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432 MainFragment \u0434\u043e\u0431\u0430\u0432\u0438\u043c 2 Textview:<\/p>\n<pre><code class=\"xml\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt; &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"     xmlns:tools=\"http:\/\/schemas.android.com\/tools\"     android:layout_width=\"match_parent\"     android:layout_height=\"match_parent\"     xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"     tools:context=\".MainFragment\"&gt;      &lt;!-- TODO: Update blank fragment layout --&gt;      &lt;TextView         android:id=\"@+id\/name_field\"         android:layout_width=\"0dp\"         android:layout_height=\"wrap_content\"         android:text=\"name\"         android:textSize=\"50sp\"          android:layout_marginTop=\"24dp\"         android:layout_marginLeft=\"24dp\"         android:layout_marginRight=\"24dp\"          app:layout_constraintLeft_toLeftOf=\"parent\"<\/code><\/pre>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-326753","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/326753","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=326753"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/326753\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=326753"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=326753"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=326753"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}