DEV Community

LiHan
LiHan

Posted on • Updated on

[Android] UserListApp - Dependency Injection - Part4

Create Application

@HiltAndroidApp
class UserApplication  : Application()
Enter fullscreen mode Exit fullscreen mode

Add Name ... on AndroidManifest

  <application
        android:name=".UserApplication"
    ....
Enter fullscreen mode Exit fullscreen mode

Add User Internet Permission

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

Enter fullscreen mode Exit fullscreen mode

Create UserModule.kt

這個檔案是為了提前產生物件.比如說:UserApi , UserRepostioryImpl

@Module
@InstallIn(SingletonComponent::class)
object UserModule {

    @Provides
    @Singleton
    fun providesUserApi() : UserApi {
        return Retrofit.Builder()
           .baseUrl(UserApi.BASE_URL)
           .addConverterFactory(GsonConverterFactory.create())
           .build().create()
    }

    @Provides
    @Singleton
    fun providesUserRepository(
        userApi : UserApi
    ) : UserRepository {
        return UserRepositoryImpl(userApi)
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)