What is generateStubs configuration in Kotlin?

后端 未结 1 431
别跟我提以往
别跟我提以往 2021-02-07 05:41

What is generateStubs for Kotlin? Here is my configuration in build.gradle.

I can not find it in public document here : http://kotlinlang.org/

相关标签:
1条回答
  • 2021-02-07 06:11

    EDIT:

    If I'm using Java and Kotlin(1.2) in my project, it is still needed to add

    No, With the version 1.0.4 introduces a new experimental implementation of the annotation processing API. Now there is no need to configure this generateStubs in build.gradle.

    Add the following to your build.gradle if you want to enable it:

    apply plugin: 'kotlin-kapt' 
    

    You will also have to remove the generateStubs config from build.gradle

    But as your question about the Details on generateStubs I keep my old Post as it is.



    Use :

    Using kapt with: generatestubs = true, in order to use libraries like dagger 2 or dbflow,This will enable the compiler to generate stub classes required for interoperability between Java and Kotlin. Unless generateStubs = true is enabled, "bootstrap" (A custom annotation processor, which is passed to javac, loads the annotation data and calls other annotation processors.) Java code is required to reference generated sources.pulled that from

    Note : Generated codes are always in Java not in Kotlin.


    When to Include:

    Generating stubs requires relatively much work, because all declarations must be resolved, and sometimes knowing return types requires analysis of expression (bodies of functions or property initializers after the = sign). So, using stubs in kapt slows your build down somewhat. That’s why stubs are off by default, and to enable them you need to write the following in your build.gradle file:

    kapt {
      generateStubs = true
    }
    

    How this works:

    Stubs, compiler generated intermediate classes, allows "generated" sources to be referenced from Kotlin otherwise the compiler will not be able to reference the missing sources.

    Generated source is created in "build/generated/source/kapt/main", as this is under "build", normally excluded from IntelliJ's project sources, this source root will be marked in the build script itself.

    sourceSets {
      main.java.srcDirs += [file("$buildDir/generated/source/kapt/main")]
    }
    

    Example :

    Dagger2-example with Kotlin (1.1.50) annotation processor support Gradle build

    0 讨论(0)
提交回复
热议问题