Tag: 科特林

我们如何用Kotlin使用Dagger2?

我想用kotlin来使用Dagger2。 但是,然后我在互联网上使用kotlin的dagger2这个设置,它出现了一个问题。 //build.gradle apply plugin: ‘kotlin-kapt’ kapt { generateStubs = true } … dependencies { compile ‘com.google.dagger:dagger-android:2.11’ compile ‘com.google.dagger:dagger-android-support:2.11’ kapt ‘com.google.dagger:dagger-android-processor:2.11’ kapt ‘com.google.dagger:dagger-compiler:2.11’ } 这里有一个问题,我也使用龙目。 这是问题吗? e: java.lang.IllegalStateException: failed to analyze: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing at org.jetbrains.kotlin.analyzer.AnalysisResult.throwIfError(AnalysisResult.kt:57) at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:137) at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:158) at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:61) at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:107) at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.java:51) at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:92) at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:386) at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:96) at […]

React-Kotlin包装setState方法

在这里的反应文档: https://facebook.github.io/react/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous 它说下面的代码是不安全的,因为状态是异步更新的: this.setState({ counter: this.state.counter + this.props.increment, }); 而是通过像以前的状态和道具: this.setState(function(prevState, props) { return { counter: prevState.counter + props.increment }; }); 但是,位于这里的React-Kotlin包装: https://github.com/Kotlin/kotlin-fullstack-sample/tree/master/frontend/src/org/jetbrains/react 状态更改是作为状态的扩展函数传入的,它修改了状态对象上的variables: //Located in the ReactComponent class in ReactComponent.kt fun setState(builder: S.() -> Unit) { … } 如果我在Kotlin中像这样调用setState函数: setState { counter: state.counter + props.increment } 这不等于上面的不安全方法吗? 在React-Kotlin包装器中是不是需要这样执行? fun setState(builder: S.(prevState: S, props: P) […]

为什么Kotlin印刷声明不要求作出范围澄清?

这可能是一个愚蠢的问题,但我是Kotlin全新的。 在Java中,典型的打印语句是: System.out.println(“Goodbye cruel world!”); 但是,在Kotlin,我们只能写: println(“Kotlin is weird but good”) Kotlin如何在这里得到范围澄清? 这不是像println是Kotlin的语言结构(或者是?)。 我在这里错过了很明显的东西吗 是否有某种隐含的import语句在这里发生? 请注意, println只是一个例子。 Kotlin还有许多其他类似的例子。

如何用FindBugs避免kotlin文件

我将findbugs集成到一个混合的java / kotlin项目中。 但是,findbugs在生成的jar上运行时,会发现与通过kotlin文件生成的类有关的bug。 有没有办法避免检查这些类? 谢谢

科特林匕首不注射

我最近从java移动到kotlin,并尝试实施匕首2dependency injection。 我已经添加到我的gradle apply plugin: ‘kotlin-kapt’ implementation “com.google.dagger:dagger:2.11” kapt “com.google.dagger:dagger-compiler:2.11” compileOnly ‘javax.annotation:jsr250-api:1.0’ 这是我的模块 @Module class AppModule(val context : Context) { @Provides @Singleton fun provideContext() = context } 这是我的组件 @Singleton @Component(modules = arrayOf(AppModule::class)) interface AppComponent { fun inject(application: Application) } 这是我的应用程序 class MyApplication : Application() { @Inject lateinit var context : Context lateinit var appComponent : […]

Maven with Kotlin 1.2:一个需要的类缺少kotlin / reflect / KDeclarationContainer

我试图用Maven使用Kotlin,所以我按照文档 我在pom.xml文件中有这个配置 UTF-8 1.8 1.8 1.2.10 ${project.basedir}/src/main/kotlin ${project.basedir}/src/test/kotlin kotlin-maven-plugin org.jetbrains.kotlin ${kotlin.version} compile compile test-compile test-compile org.jetbrains.kotlin kotlin-stdlib-jdk8 ${kotlin.version} org.jetbrains.kotlin kotlin-reflect ${kotlin.version} 源文件夹是src/main/kotlin和src/test/kotlin 当我mvn clean install我得到这个错误 [ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.2.10:compile (compile) on project kotlin-starter: Execution compile of goal org.jetbrains.kotlin:kotlin-maven-plugin:1.2.10:compile failed: A required class was missing while executing org.jetbrains.kotlin:kotlin-maven-plugin:1.2.10:compile: kotlin/reflect/KDeclarationContainer [ERROR] —————————————————– [ERROR] realm = […]

Kotlin语法混乱:fun Foo.bar()=(…)

我很困惑下面的代码行应该做什么: fun MyContext.req() = request as LaunchRequest LaunchRequest是一个Java类,而MyContext是一个Kotlin数据类。 我试过查找使用这种语法的Kotlin代码的例子,但是还没有find任何东西。 MyContext没有req()函数,那么这是为MyContext定义一个新的函数,它返回一个types为LaunchRequest “request”variables?

Kotlin Javascript -library-files无效的参数

遵循这里的指令: https ://kotlinlang.org/docs/tutorials/command-line-library-js.html但编译时得到无效参数:-library-files 。 我不确定我做错了什么。 任何帮助赞赏。 编译器从https://github.com/JetBrains/kotlin/releases/tag/v1.1获得 kotlin-compiler-1.1.zip kotlinc-js -output binom.js -library-files sample-library.jar binom.kt Invalid argument: -library-files Usage: kotlinc-js where possible options include: -output Output file path -no-stdlib Don’t use bundled Kotlin stdlib -libraries Paths to Kotlin libraries with .meta.js and .kjsm files, separated by system file separator -source-map Generate source map -meta-info Generate .meta.js […]

将Java Collection.toArray转换为kotlin

请帮我把这个java代码转换成kotlin,我试了很多次,但是不行 ObjectStream bin = ObjectStreamUtils.createObjectStream(categoryStreams.toArray(new ObjectStream[0]));

Gradle项目同步失败,Kotlin

我正在使用Beta Kotlin和Android-Studio频道。 更新到最新版本后,我得到了一些gradle同步错误。 以下是我从事件日志复制的日志: Error:Could not find org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.0-rc-39. Searched in the following locations: file:/home/yourpc/Android/android/gradle/m2repository/org/jetbrains/kotlin/kotlin-gradle-plugin/1.2.0-rc-39/kotlin-gradle-plugin-1.2.0-rc-39.pom file:/home/yourpc/Android/android/gradle/m2repository/org/jetbrains/kotlin/kotlin-gradle-plugin/1.2.0-rc-39/kotlin-gradle-plugin-1.2.0-rc-39.jar https://dl.google.com/dl/android/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.2.0-rc-39/kotlin-gradle-plugin-1.2.0-rc-39.pom https://dl.google.com/dl/android/maven2/org/jetbrains/kotlin/kotlin-gradle-plugin/1.2.0-rc-39/kotlin-gradle-plugin-1.2.0-rc-39.jar https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-gradle-plugin/1.2.0-rc-39/kotlin-gradle-plugin-1.2.0-rc-39.pom https://jcenter.bintray.com/org/jetbrains/kotlin/kotlin-gradle-plugin/1.2.0-rc-39/kotlin-gradle-plugin-1.2.0-rc-39.jar Required by: project : 下面是build.gradle // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { ext.kotlin_version = ‘1.2.0-rc-39’ repositories { google() jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:3.1.0-alpha04’ classpath “org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version” // […]