为什么intelliJ IDEA依赖范围是“提供”而不是“编译”?

我希望IntelliJ IDEA将我的库作为“编译”范围而不是“提供”范围。 这是我的gradle文件的一部分: apply plugin: 'java' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { // Logging compile 'ch.qos.logback:logback-classic:1.2.1' compile 'com.getsentry.raven:raven-logback:7.8.2' // BigQuery compile 'com.google.api-client:google-api-client:1.20.0' compile 'com.google.apis:google-api-services-bigquery:v2-rev227-1.20.0' // Configuration management compile 'commons-configuration:commons-configuration:1.10' //Json compile 'org.json:json:20160810' //Kafka compile "org.apache.kafka:kafka-clients:0.10.1.1" testCompile group: 'junit', name: 'junit', version: '4.12' testCompile 'org.assertj:assertj-core:3.0.0' testCompile 'org.mockito:mockito-all:1.10.19' } task wrapper(type: Wrapper) { gradleVersion […]

在Kotlin嵌套lambda调用

在Kotlin嵌套lambda调用时,我怎样才能明确地引用孩子和父母的元素? 例如: data class Course(var weekday: Int, var time: Int, var duration: Int) var list1 = mutableListOf<Course>() var list2 = mutableListOf<Course>() // populate list1 and list2 // exclude any element from list1 if it has the same time and weekday as any element from list2 list1.filter { list2.none{it.weekday == it.weekday && it.time == it.time} }

查询Android的联系人在Kotlin收购

我在尝试在Kotlin编写的Android应用程序中查询ContactsContract时遇到了一些麻烦。 Android工作室从未解析的引用(例如ContactsContract.Contacts._ID)中提供错误。 有谁知道正确的方式来查询这些Kotlin?

Kotlin null来自Java

当我将Java代码从一个库转换为Kotlin时,我会看到Java代码中是否存在空值检查,并想知道如何最好地将其转换。 我仍然需要在Kotlin的空检查,因为我不能控制,如果Java将使对象为空? 比方说,我有一个CameraDevice可以从Java返回null 我应该如何定义这个 private lateinit var mCameraDevice: CameraDevice 然后不需要空检查或像这样.. private var mCameraDevice: CameraDevice? = null 然后保持空的检查 if (mCameraDevice != null) { // do something } Kotlin的可玩性让我感到困惑,因为我觉得我不应该处理null,并且应该使用选项1,但是基本上我使用的每个库都是Java,所以我必须处理空值,并使用选项2

Kotlin相当于三元运算符

所以在java中,我们有三元运算符(?),它有时很容易通过if-else inlines来计算一些值。 例如: myAdapter.setAdapterItems( textToSearch.length == 0 ? noteList : noteList.sublist(0, length-5) ) 我知道kotlin的等价物是: myAdapter.setAdapterItems( if(textToSearch.length == 0) noteList else noteList.sublist(0, length-5) ) 但是我只是喜欢Java中的三元运算符,用于简短的表达条件以及将值传递给方法。 有没有Kotlin等价物?

Kotlin自定义属性数据绑定

我想在我的Kotlin项目中使用Android DataBinding库设置自定义属性,如下所示: 布局 <ImageView android:id="@+id/imgView” android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center" android:adjustViewBounds="true" app:imageUrl="@{segment.url}"/> 码 class Utils { companion object { @BindingAdapter("bind:imageUrl") @JvmStatic fun loadImage(view: ImageView, url:String) {Picasso.with(view.context).load(url).error(R.drawable.error).into(view)} } 我得到的运行时错误是: in中的BindingAdapter不是静态的,需要使用从DataBindingComponent中检索的对象。 如果您不使用采用DataBindingComponent的通胀方法,请使用DataBindingUtil.setDefaultComponent或使所有BindingAdapter方法都是静态的。 任何指针来解决它? 这只发生在自定义属性上。 其余的数据绑定工作正常

Spring Boot + Kotlin注释错误(s)

我有一个写在Kotlin的Spring Boot 2.0.0.M2 (带WebFlux)应用程序。 我习惯于为测试用例定义/声明“注释”,以避免一些样板配置; 就像是: import java.lang.annotation.ElementType import java.lang.annotation.Inherited import java.lang.annotation.Retention import java.lang.annotation.RetentionPolicy import java.lang.annotation.Target … @Inherited @Target(ElementType.TYPE) @AutoConfigureWebTestClient // TODO: FTW this really does?! @Retention(RetentionPolicy.RUNTIME) //@kotlin.annotation.Target(AnnotationTarget.TYPE) //@kotlin.annotation.Retention(AnnotationRetention.RUNTIME) @ActiveProfiles(profiles = arrayOf("default", "test")) @ContextConfiguration(classes = arrayOf(Application::class)) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) annotation class SpringWebFluxTest …然后在我的测试中,我使用它: @SpringWebFluxTest @RunWith(SpringRunner::class) class PersonWorkflowTest { private lateinit var client: WebTestClient … 问题是 […]

Kotlin函数语法

我正在做Kotlin Koans测试,以便熟悉Kotlin。 在某个测试中,我必须重写compareTo方法。 在第一种情况下,一切都按预期工作 data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) { operator fun compareTo(other: MyDate)= when { year != other.year -> year – other.year month != other.month -> month – other.month else -> dayOfMonth – other.dayOfMonth } } 现在在第二种情况下,我正在写compareTo有点不同,我得到了大量的编译错误。 data class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) […]

Kotlin类实例assertEqual

我是新来的java / kotlin。 我想在下面的课堂上坚持平等: class PlaceCommand(vararg args: String) : ICommand { var direction: Direction = Direction.valueOf(args[1].toUpperCase()) var x: Int = args[2].toInt() var y: Int = args[3].toInt() // … } 需要改变什么: class FactoryTest { @Test fun testFactorySuccess() { val args = arrayOf("place", "WEST", "1", "1") val a = PlaceCommand(*args) val b = Factory(args) as PlaceCommand Assert.assertTrue(axequals(bx)) […]

Kotlin Android视图绑定:findViewById vs Butterknife vs Kotlin Android扩展

我试图找出在Kotlin中执行Android视图绑定的最佳方法。 似乎有几个选项在那里: findViewById val button: Button by lazy { findViewById<Button>(R.id.button) } 牛油刀 https://github.com/JakeWharton/butterknife @BindView(R.id.button) lateinit var button: Button Kotlin Android扩展 https://kotlinlang.org/docs/tutorials/android-plugin.html import kotlinx.android.synthetic.main.activity_main.* 我非常熟悉Java领域的findViewById和Butterknife,但是Kotlin中每种视图绑定方法的优缺点是什么? Kotlin Android扩展与RecyclerView + ViewHolder模式兼容吗? 另外Kotlin Android Extensions如何通过include处理嵌套视图的视图绑定? 例如:对于使用activity_main.xml的Activity,如何访问View custom1 ? activity_main.xml中 <…> <include layout="@layout/custom" android:id="@+id/custom" /> </> custom.xml <…> <View android:id="@+id/custom1" … /> <View android:id="@+id/custom2" … /> </>