DEV Community

LiHan
LiHan

Posted on • Edited 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)

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

AWS GenAI Live!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️