Created
March 27, 2018 10:15
-
-
Save seanghay/cd7577b6049a6af4142ed3ce39f87a67 to your computer and use it in GitHub Desktop.
Setting Up Dagger 2, Retrofit, Gson, RxJava2, AndroidInjector
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
abstract class ActivityBuilder { | |
@ContributesAndroidInjector(modules = [MainActivityModule::class]) | |
abstract fun bindMainActivity(): MainActivity | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.dell.todoapps"> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<application | |
android:name=".application.TodoApplication" | |
android:allowBackup="true" | |
android:icon="@mipmap/ic_launcher" | |
android:label="@string/app_name" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:supportsRtl="true" | |
android:theme="@style/AppTheme"> | |
<activity android:name=".ui.MainActivity.MainActivity"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<activity android:name=".ui.SecondActivity.SecondActivity"></activity> | |
</application> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply plugin: 'com.android.application' | |
apply plugin: 'kotlin-android' | |
apply plugin: 'kotlin-kapt' | |
apply plugin: 'kotlin-android-extensions' | |
android { | |
compileSdkVersion 27 | |
defaultConfig { | |
applicationId "com.example.dell.todoapps" | |
minSdkVersion 21 | |
targetSdkVersion 27 | |
versionCode 1 | |
versionName "1.0" | |
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" | |
javaCompileOptions { | |
annotationProcessorOptions { | |
arguments = ["room.schemaLocation": "$projectDir/schemas".toString()] | |
} | |
} | |
} | |
buildTypes { | |
release { | |
minifyEnabled false | |
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' | |
} | |
} | |
} | |
kapt { | |
generateStubs = true | |
} | |
dependencies { | |
implementation fileTree(dir: 'libs', include: ['*.jar']) | |
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" | |
implementation 'com.android.support:appcompat-v7:27.1.0' | |
implementation 'com.android.support.constraint:constraint-layout:1.0.2' | |
implementation "io.reactivex.rxjava2:rxjava:$rootProject.rxjavaVersion" | |
implementation "io.reactivex.rxjava2:rxandroid:$rootProject.rxandroidVersion" | |
implementation "io.reactivex.rxjava2:rxkotlin:$rootProject.rxkotlinVersion" | |
implementation "com.squareup.retrofit2:retrofit:$rootProject.retrofitVersion" | |
implementation "com.squareup.retrofit2:converter-gson:$rootProject.retrofitVersion" | |
implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0' | |
implementation "com.squareup.okhttp3:okhttp:$rootProject.okhttpVersion" | |
implementation "com.squareup.okhttp3:logging-interceptor:$rootProject.okhttpVersion" | |
implementation 'com.google.code.gson:gson:2.8.2' | |
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" | |
implementation "com.google.dagger:dagger:$rootProject.daggerVersion" | |
implementation "com.google.dagger:dagger-android:$rootProject.daggerVersion" | |
implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion" | |
implementation 'com.google.dagger:dagger-android:2.13' | |
implementation 'com.google.dagger:dagger-android-support:2.13' | |
kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion" | |
kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion" | |
compileOnly 'org.glassfish:javax.annotation:10.0-b28' | |
annotationProcessor 'com.google.dagger:dagger-android-processor:2.13' | |
annotationProcessor 'com.google.dagger:dagger-compiler:2.13' | |
testImplementation 'junit:junit:4.12' | |
androidTestImplementation 'com.android.support.test:runner:1.0.1' | |
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1' | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Singleton | |
@Component(modules = [AndroidInjectionModule::class, AppModule::class, ActivityBuilder::class]) | |
interface AppComponent { | |
@Component.Builder | |
interface Builder { | |
@BindsInstance | |
fun application(app: Application): Builder | |
fun build(): AppComponent | |
} | |
fun inject(app: TodoApplication) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
class AppModule { | |
@Provides | |
@Singleton | |
fun provideGson(): Gson { | |
return GsonBuilder() | |
.create() | |
} | |
@Provides | |
@Singleton | |
fun provideOkHttpClient(application: Application): OkHttpClient { | |
val interceptor = HttpLoggingInterceptor() | |
interceptor.level = HttpLoggingInterceptor.Level.BASIC | |
return OkHttpClient.Builder() | |
.addInterceptor(interceptor) | |
.build() | |
} | |
@Provides | |
@Singleton | |
fun provideApiService(gson: Gson, okHttpClient: OkHttpClient): ApiService { | |
return Retrofit.Builder() | |
.baseUrl("https://jsonplaceholder.typicode.com") | |
.addConverterFactory(GsonConverterFactory.create(gson)) | |
.addCallAdapterFactory(RxJava2CallAdapterFactory.create()) | |
.client(okHttpClient) | |
.build().create(ApiService::class.java) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
ext.kotlin_version = '1.2.30' | |
repositories { | |
google() | |
jcenter() | |
} | |
dependencies { | |
classpath 'com.android.tools.build:gradle:3.1.0' | |
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" | |
} | |
} | |
allprojects { | |
repositories { | |
google() | |
jcenter() | |
} | |
} | |
task clean(type: Delete) { | |
delete rootProject.buildDir | |
} | |
ext { | |
architectureComponentsVersion = "1.0.0" | |
daggerVersion = "2.13" | |
retrofitVersion = "2.3.0" | |
okhttpVersion = "3.9.0" | |
rxjavaVersion = "2.1.3" | |
rxandroidVersion = "2.0.1" | |
rxkotlinVersion = "2.1.0" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class DaggerActivity: AppCompatActivity(), HasFragmentInjector { | |
@Inject | |
lateinit var fragmentInjector: DispatchingAndroidInjector<Fragment> | |
override fun fragmentInjector(): AndroidInjector<Fragment> = fragmentInjector | |
override fun onCreate(savedInstanceState: Bundle?) { | |
AndroidInjection.inject(this) | |
super.onCreate(savedInstanceState) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : DaggerActivity() { | |
@Inject | |
lateinit var apiService: ApiService | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
button.setOnClickListener { | |
startActivity(Intent(this, SecondActivity::class.java)) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Module | |
class MainActivityModule { | |
@Provides | |
fun provideSomething(): Something { | |
return Something() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Post(var userId: Int, | |
var id: Int, | |
var title: String, | |
var body: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Something { | |
fun getString() = "Hello World" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TodoApplication: Application(), HasActivityInjector { | |
@Inject | |
lateinit var injector: DispatchingAndroidInjector<Activity> | |
override fun onCreate() { | |
super.onCreate() | |
DaggerAppComponent.builder() | |
.application(this) | |
.build() | |
.inject(this) | |
} | |
override fun activityInjector(): AndroidInjector<Activity> = injector | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment