Skip to content
Kotlin Dependency Injection
Kotlin HTML Java Ruby
Branch: develop
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
doc Update documentation Aug 9, 2019
gradle
winter-android-integration-test
winter-android
winter-androidx-lifecycle
winter-compiler-test Remove unused kapt winterPrintSources argument Jul 27, 2019
winter-compiler
winter-junit4
winter-rxjava2 Set source and target compatibility to 1.8 Jul 27, 2019
winter
.gitignore
.travis.yml
CHANGELOG.md
LICENSE
README.md
build.gradle
deploy_website.rb
detekt-config.yml
generate_inject_extensions.rb
gradle.properties
gradlew
gradlew.bat
inject_extension.erb
settings.gradle

README.md

Winter

Kotlin 1.3.40 Maven Central Travis MIT License GitHub issues

Kotlin Dependency Injection

Winter is a fast and intuitive dependency injection library for Kotlin on Android and the JVM.

It offers an idiomatic Kotlin API as well as optional JSR-330 support with annotation processor.

Winter documentation

Installation

dependencies {
  // Core
  implementation 'io.jentz.winter:winter:0.3.0'
  // Android support
  implementation 'io.jentz.winter:winter-android:0.3.0'
  // Android X lifecycle extensions
  implementation 'io.jentz.winter:winter-androidx-lifecycle:0.3.0'
  // RxJava 2 disposable plugin
  implementation 'io.jentz.winter:winter-rxjava2:0.3.0'
  // JUnit 4 test support
  testImplementation 'io.jentz.winter:winter-junit4:0.3.0'  
  // Optional JSR-330 support
  implementation 'javax.inject:javax.inject:1'
  kapt 'io.jentz.winter:winter-compiler:0.3.0'
}

// The optional JSR-330 support requires also a kapt configuration block like
kapt {
  arguments {
    // Tell the Winter compiler under which package name it should generate the component
    arg("winterGeneratedComponentPackage", "my.project.root.package.name")
    // Optional: The custom scope annotation that is used as root scope instead of javax.inject.Singleton
    arg("winterRootScopeAnnotation", "my.project.root.package.name.ApplicationScope")
  }
}

Quickstart Android

Given is an Android application with an application component, a presentation subcomponent that survives orientation changes and an activity subcomponent.

class MyApplication : Application() {
  override fun onCreate() {
    // define application component
    Winter.component {
      singleton<HttpClient> { HttpClientImpl() }
      singleton<GitHubApi> { GitHubApiImpl(instance()) }
      
        // define presentation subcomponent
        subcomponent("presentation") {
          singleton<ReposListViewModel> { RepolistViewModel(instance<GitHubApi>()) }
          
          // define activity subcomponent
          subcomponent("activity") {
            singleton { Glide.with(instance()) }
          }
        }
    }

    /// Configure Injection to use the Android presentation scope adapter   
    Injection.useAndroidPresentationScopeAdapter()
    // Create application graph by providing the application instance
    Injection.createGraph(this)
  }
}

class MyActivity : Activity() {
  private val injector = Injector()
  private val viewModel: RepoListViewModel by injector.instance()
  private val glide: RequestManager by injector.instance()

  override fun onCreate(savedInstanceState: Bundle?) {
    Injection.createGraphAndInject(this, injector)
    super.onCreate(savedInstanceState)
  }

  override fun onDestroy() {
    Injection.disposeGraph(this)
    super.onDestroy()
  }

}

Read more

Winter is used in production since end of 2017. The API is considered mostly stable but there will be no guarantee before version 1.x.

Winter supports only Android and JVM right now but support for native (iOS) and JavaScript is planed.

Winter documentation

License

Copyright 2017 Alexander Jentz

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
You can’t perform that action at this time.