Kotlin中的generateStubs配置是什么? [英] What is generateStubs configuration in Kotlin?

查看:1352
本文介绍了Kotlin中的generateStubs配置是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Kotlin的generateStubs是什么?这是我在build.gradle中的配置.

我在此处的公共文档中找不到它: http://kotlinlang.org/docs/reference/kapt.html

kapt {
  generateStubs = true
}

如果我在项目中使用JavaKotlin(1.2),还需要添加吗?

解决方案

编辑:

如果我在项目中使用Java和Kotlin(1.2),则仍然需要 添加

,在版本 1.0.4 中引入了注释处理API的新实验性实现.现在,无需在build.gradle中配置此generateStubs.

如果要启用以下内容,请在build.gradle中添加以下内容:

apply plugin: 'kotlin-kapt' 

您还必须从build.gradle

中删除generateStubs配置

但是作为您对generateStubs上的详细信息的疑问,我保留了原来的帖子.



使用:

kaptgeneratestubs = true结合使用,以便使用诸如dagger 2或dbflow之类的库,这将使编译器能够生成Java和Kotlin之间的互操作性所需的桩类.除非启用generateStubs = true,否则必须使用Java代码来引用"bootstrap"(传递给javac的自定义注释处理器,该注释处理器将加载注释数据并调用其他注释处理器.).生成的sources.pulling 来自

注意:生成的代码始终位于Java中,而不位于Kotlin中.


何时包括:

生成存根需要相对较多的工作,因为必须解析所有声明,有时知道返回类型需要对表达式进行分析(=符号之后的函数或属性初始化器的实体).因此,在kapt中使用存根会稍微降低构建速度.这就是为什么存根默认情况下处于关闭状态的原因,并且要启用它们,您需要在build.gradle文件中写入以下内容:

kapt {
  generateStubs = true
}


工作原理:

存根是编译器生成的中间类,它允许从Kotlin引用生成的"源,否则编译器将无法引用缺少的源.

生成的源代码是在"build/generated/source/kapt/main"中创建的,因为它位于" build "下,通常从IntelliJ的项目源代码中排除,因此该源代码根将标记在构建脚本本身中.

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


示例:

> 带有Kotlin(1.1.50)注释处理器支持Gradle构建的Dagger2-示例

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

I can not find it in public document here : http://kotlinlang.org/docs/reference/kapt.html

kapt {
  generateStubs = true
}

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

解决方案

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

这篇关于Kotlin中的generateStubs配置是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆