Coder Social home page Coder Social logo

kotlin-dagger-example's Introduction

kotlin-dagger-example

This project demonstrate how to setup an Android Project with Kotlin and Dagger 2.

It's based on Dagger 2 example

##Known issues/limitations

  1. Kotlin compiler does not allow $ sign in class names. So you can't use Dagger's generated classes that use $. There is an issue reported KT-6444. Dagger 2 is also going to remove $ from their naming conventions, see issue
  2. Classes and interfaces that Dagger use for generating implementations must be kept in Java. If it's moved to Kotlin it won't be generated e.g. AndroidModule, ApplicationComponent
  3. Dagger's field injection is not working within Kotlin files.

This will not work:

Inject var locationManager: LocationManager? = null

Fortunately, setter injection works for Kotlin and syntax is simple:

var locationManager: LocationManager? = null
[Inject] set

How to use @Named property qualifier

If you are going to use multiple @Provides for the same type you would expect injecting property like this:

var typeface: Typeface? = null
[Inject Named("bold")] set

Unfortunatelly, it won't work. It will not even compile.

Proper way is

var typeface: Typeface? = null
[Inject] set([Named("bold")] value) {
    $typeface = value
}

Be careful where you call Dagger's inject(this) method. If you do it from the class constructor it won't work becasue after injection, property is initialised with null value. If you do it in Activity or Fragment, onCreate() will be just fine. If not, you have to create your own function and call it explicitly wherever you initialise your class. For some more datails see issue KT-6740

kotlin-dagger-example's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.