Skip to content

Commit 66aae94

Browse files
committed
Merge pull request Kotlin#9 from yanex/master
Kotlin with Dagger 2 example (use SNAPSHOT compiler&plugin version)
2 parents dc55e93 + 9842802 commit 66aae94

22 files changed

+616
-0
lines changed

gradle/kotlin-dagger/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea
5+
.DS_Store
6+
/build
7+
/captures
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
buildscript {
2+
ext.kotlin_version = '0.1-SNAPSHOT'
3+
repositories {
4+
jcenter()
5+
maven {
6+
url 'http://oss.sonatype.org/content/repositories/snapshots'
7+
}
8+
}
9+
dependencies {
10+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11+
classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
12+
}
13+
}
14+
15+
apply plugin: 'com.android.application'
16+
apply plugin: 'kotlin-android'
17+
18+
android {
19+
compileSdkVersion 22
20+
buildToolsVersion "22.0.1"
21+
22+
defaultConfig {
23+
applicationId "com.example.dagger.kotlin"
24+
minSdkVersion 15
25+
targetSdkVersion 22
26+
versionCode 1
27+
versionName "1.0"
28+
}
29+
buildTypes {
30+
release {
31+
minifyEnabled false
32+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
33+
}
34+
}
35+
sourceSets {
36+
main.java.srcDirs += 'src/main/kotlin'
37+
}
38+
}
39+
40+
dependencies {
41+
compile fileTree(dir: 'libs', include: ['*.jar'])
42+
compile 'com.android.support:appcompat-v7:22.1.1'
43+
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
44+
45+
compile 'com.google.dagger:dagger:2.0'
46+
kapt 'com.google.dagger:dagger-compiler:2.0'
47+
provided 'org.glassfish:javax.annotation:10.0-b28'
48+
}
49+
50+
repositories {
51+
jcenter()
52+
maven {
53+
url 'http://oss.sonatype.org/content/repositories/snapshots'
54+
}
55+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/yan/Library/Android/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
3+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
4+
package="com.example.dagger.kotlin">
5+
6+
<application
7+
android:allowBackup="true"
8+
android:label="app_name"
9+
android:name=".DemoApplication">
10+
<activity
11+
android:label="app_name"
12+
android:name=".ui.HomeActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN"/>
15+
<category android:name="android.intent.category.LAUNCHER"/>
16+
<category android:name="android.intent.category.DEFAULT"/>
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
</manifest>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (C) 2013 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example.dagger.kotlin
17+
18+
import android.content.Context
19+
import android.content.Context.LOCATION_SERVICE
20+
import android.location.LocationManager
21+
import dagger.Module
22+
import dagger.Provides
23+
import javax.inject.Singleton
24+
25+
/**
26+
* A module for Android-specific dependencies which require a [Context] or
27+
* [android.app.Application] to create.
28+
*/
29+
Module
30+
public class AndroidModule(private val application: BaseApplication) {
31+
32+
/**
33+
* Allow the application context to be injected but require that it be annotated with
34+
* [@Annotation][ForApplication] to explicitly differentiate it from an activity context.
35+
*/
36+
Provides Singleton ForApplication fun provideApplicationContext(): Context {
37+
return application
38+
}
39+
40+
Provides Singleton fun provideLocationManager(): LocationManager {
41+
return application.getSystemService(LOCATION_SERVICE) as LocationManager
42+
}
43+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.dagger.kotlin
2+
3+
import com.example.dagger.kotlin.ui.HomeActivity
4+
import dagger.Component
5+
import javax.inject.Singleton
6+
7+
Singleton
8+
Component(modules = arrayOf(javaClass<AndroidModule>()))
9+
public trait ApplicationComponent {
10+
public fun inject(application: BaseApplication)
11+
public fun inject(homeActivity: HomeActivity)
12+
public fun inject(demoActivity: DemoActivity)
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) 2013 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example.dagger.kotlin;
17+
18+
import android.app.Application;
19+
20+
public abstract class BaseApplication extends Application {
21+
22+
protected ApplicationComponent initDaggerComponent() {
23+
return DaggerApplicationComponent.builder()
24+
.androidModule(new AndroidModule(this))
25+
.build();
26+
}
27+
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2013 Square, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.example.dagger.kotlin
17+
18+
import android.app.Activity
19+
import android.os.Bundle
20+
21+
public abstract class DemoActivity : Activity() {
22+
override fun onCreate(savedInstanceState: Bundle?) {
23+
super.onCreate(savedInstanceState)
24+
// Perform injection so that when this call returns all dependencies will be available for use.
25+
(getApplication() as DemoApplication).component().inject(this)
26+
}
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.example.dagger.kotlin
2+
3+
import android.location.LocationManager
4+
import javax.inject.Inject
5+
6+
public class DemoApplication : BaseApplication() {
7+
8+
private var component: ApplicationComponent? = null
9+
10+
var locationManager: LocationManager? = null // for some reason.
11+
[Inject] set
12+
13+
override fun onCreate() {
14+
super.onCreate()
15+
component = initDaggerComponent()
16+
component().inject(this) // As of now, LocationManager should be injected into this.
17+
}
18+
19+
public fun component(): ApplicationComponent {
20+
return component!!
21+
}
22+
23+
}

0 commit comments

Comments
 (0)