Skip to content
This repository has been archived by the owner on Jan 22, 2024. It is now read-only.

Commit

Permalink
Use stubs in Dagger example
Browse files Browse the repository at this point in the history
  • Loading branch information
yanex committed Oct 28, 2015
1 parent 34d96e6 commit 71f042b
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 33 deletions.
23 changes: 9 additions & 14 deletions gradle/android-dagger/app/build.gradle
@@ -1,10 +1,8 @@
buildscript {
ext.kotlin_version = '0.1-SNAPSHOT'
ext.kotlin_version = '1.0.0-beta-1038'

repositories {
jcenter()
maven {
url 'http://oss.sonatype.org/content/repositories/snapshots'
}
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
Expand All @@ -16,13 +14,13 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
applicationId "com.example.dagger.kotlin"
minSdkVersion 15
targetSdkVersion 22
minSdkVersion 14
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
Expand All @@ -39,17 +37,14 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.android.support:appcompat-v7:23.1.0'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

compile 'com.google.dagger:dagger:2.0.1'
kapt 'com.google.dagger:dagger-compiler:2.0'
provided 'org.glassfish:javax.annotation:10.0-b28'
}

repositories {
jcenter()
maven {
url 'http://oss.sonatype.org/content/repositories/snapshots'
}
kapt {
generateStubs = true
}
Expand Up @@ -13,16 +13,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.dagger.kotlin;
package com.example.dagger.kotlin

import android.app.Application;
import android.app.Application

public abstract class BaseApplication extends Application {
public abstract class BaseApplication : Application() {

protected ApplicationComponent initDaggerComponent() {
return DaggerApplicationComponent.builder()
.androidModule(new AndroidModule(this))
.build();
}
protected fun initDaggerComponent(): ApplicationComponent {
return DaggerApplicationComponent.builder().androidModule(AndroidModule(this)).build()
}

}
Expand Up @@ -22,6 +22,6 @@ public abstract class DemoActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Perform injection so that when this call returns all dependencies will be available for use.
(application as DemoApplication).component().inject(this)
(application as DemoApplication).component.inject(this)
}
}
@@ -1,15 +1,14 @@
package com.example.dagger.kotlin

public class DemoApplication : BaseApplication() {

private lateinit var component: ApplicationComponent
lateinit var component: ApplicationComponent
protected set

override fun onCreate() {
super.onCreate()
component = initDaggerComponent()
component().inject(this) // As of now, LocationManager should be injected into this.
val component = initDaggerComponent()
component.inject(this) // As of now, LocationManager should be injected into this.
this.component = component
}

public fun component(): ApplicationComponent = component

}
Expand Up @@ -18,5 +18,4 @@ package com.example.dagger.kotlin
import javax.inject.Qualifier

@Qualifier
@Retention(AnnotationRetention.RUNTIME)
annotation public class ForApplication
Expand Up @@ -24,12 +24,13 @@ import kotlinx.android.synthetic.activity_main.locationInfo
import javax.inject.Inject

public class HomeActivity : DemoActivity() {
@Inject lateinit var locationManager: LocationManager
@Inject
lateinit var locationManager: LocationManager

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
(application as DemoApplication).component().inject(this)
(application as DemoApplication).component.inject(this)

// TODO do something with the injected dependencies here!
locationInfo.text = "Injected LocationManager:\n$locationManager"
Expand Down
2 changes: 1 addition & 1 deletion gradle/android-dagger/build.gradle
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
classpath 'com.android.tools.build:gradle:1.3.0'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down

0 comments on commit 71f042b

Please sign in to comment.