小编lol*_*f64的帖子

使用Gradle和Kotlin构建一个可自我执行的jar

我已经编写了一个简单的kotlin源文件以便开始使用,还有一个gradle脚本文件.但我无法弄清楚如何将主要功能添加到清单中,以便jar可以自行执行.

这是我的build.gradle脚本:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.9.66'
    }
}
apply plugin: "kotlin"
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.jetbrains.kotlin:kotlin-stdlib:0.9.66'
}

jar {
    manifest {
        attributes 'Main-Class': 'com.loloof64.kotlin.exps.ExpsPackage'
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的com.loloof64.kotlin.exps.Multideclarations.kt

package com.loloof64.kotlin.exps

class Person(val firstName: String, val lastName: String) {
    fun component1(): String {
        return firstName
    }
    fun component2(): String {
        return lastName
    }
}

fun main(args: Array < String > ) {
    val(first, last) = Person("Laurent", "Bernabé")
    println("First name : $first - Last …
Run Code Online (Sandbox Code Playgroud)

gradle kotlin

43
推荐指数
3
解决办法
2万
查看次数

字符串数组文字?我该如何编码呢?

虽然这可能是一个愚蠢的问题,但我无法弄清楚如何声明一个数组文字分组一些字符串文字.

例如,假设我想要java数组["January", "February", "March"].我怎样才能将其翻译成最新的kotlin版本(today, 12.0.0)

我试过了什么?

stringArray("January", "February", "March")
Run Code Online (Sandbox Code Playgroud)

arrays kotlin

41
推荐指数
2
解决办法
2万
查看次数

Kotlin:安全的lambdas(没有内存泄漏)?

在阅读了关于内存泄漏的这篇文章之后,我想知道在Kotlin Android项目中使用lambdas是否安全.确实,lambda语法让我更容易编程,但内存泄漏怎么样?

作为问题的一个例子,我从我的一个项目中获取了一段代码,在那里我构建了一个AlertDialog.此代码位于项目的MainActivity类中.

fun deleteItemOnConfirmation(id: Long) : Unit {
        val item = explorerAdapter.getItemAt(id.toInt())
        val stringId = if (item.isDirectory) R.string.about_to_delete_folder else R.string.about_to_delete_file

        val dialog = AlertDialog.Builder(this).
                setMessage(String.format(getString(stringId), item.name)).setPositiveButton(
                R.string.ok, {dialog: DialogInterface, id: Int ->
                        val success = if (item.isDirectory) ExplorerFileManager.deleteFolderRecursively(item.name)
                        else ExplorerFileManager.deleteFile(item.name)
                        if (success) {
                            explorerAdapter.deleteItem(item)
                            explorerRecyclerView.invalidate()
                        }
                        else Toast.makeText(this@MainActivity, R.string.file_deletion_error, Toast.LENGTH_SHORT).show()
                    }).setNegativeButton(
                R.string.cancel, {dialog: DialogInterface, id: Int ->
                    dialog.cancel()
        })

        dialog.show()
}
Run Code Online (Sandbox Code Playgroud)

我的问题很简单:为正负按钮设置的两个lambdas可以导致内存泄漏吗?(我的意思是,kotlin lambdas只是转换为Java匿名函数吗?)

编辑:也许我在这个Jetbrains话题中得到了答案.

lambda android memory-leaks kotlin

28
推荐指数
3
解决办法
7810
查看次数

为什么我从这段代码中得到MalformedInputException?

我是Scala的新手,我想写一些自己的源代码让我变得更好.我编写了一个简单的对象(带有一个主条目),以模拟当前目录的所有文件上的"grep"调用.(我从Eclipse Indigo和Debian Squeeze启动程序):

package com.gmail.bernabe.laurent.scala.tests

import java.io.File

import scala.io.Source

object DealWithFiles {

  def main(args:Array[String]){
    for (result <- grepFilesHere(".*aur.*"))
      println(result)
  }

  private def grepFilesHere(pattern:String):Array[String] = {
    val filesHere = new File(".").listFiles

    def linesOfFile(file:File) =
      Source.fromFile(file).getLines.toList

    for (file <- filesHere;
        if file.isFile
    )
      yield linesOfFile(file)(0)
  }

}
Run Code Online (Sandbox Code Playgroud)

但我得到一个java.nio.charset.MalformedInputException,我无法解决:

Exception in thread "main" java.nio.charset.MalformedInputException: Input length = 1
at java.nio.charset.CoderResult.throwException(CoderResult.java:260)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:319)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at scala.io.BufferedSource$BufferedLineIterator.hasNext(BufferedSource.scala:67)
at scala.collection.Iterator$class.foreach(Iterator.scala:772)
at scala.io.BufferedSource$BufferedLineIterator.foreach(BufferedSource.scala:43)
at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48)
at scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:130)
at …
Run Code Online (Sandbox Code Playgroud)

scala

21
推荐指数
1
解决办法
3万
查看次数

在kotlin的懒惰名单?

如何以简单的方式在Kotlin中实现懒惰列表?(例如,整数懒惰列表).我一直在寻找官方文档,我一直在谷歌搜索没有一致的结果.也许我发现的最好的教程是这一个:在这里输入链接描述,但我想知道是否有更多的"kotlin本地方式"来做这件事,或者我必须自己用我刚给出的链接实现它.

我在Kotlin的官方博客上找到了以下内容,但我无法获得一个项目,例如整数[3]

var i = 0
integers = iterate{i++}

integers[3] // does not work
integers drop 3 // works
Run Code Online (Sandbox Code Playgroud)

kotlin

17
推荐指数
1
解决办法
4482
查看次数

针对Android的kotlin抑制警告已弃用

在我的Kotlin Android项目中,我使用的是从api 23开始不推荐使用的函数,这是最新的.所以我需要一种方法来禁用那些已弃用的警告.有一个简单的方法吗?

android warnings kotlin

17
推荐指数
2
解决办法
6642
查看次数

无法使用AndroidAnnotations库在AndroidStudio中制作项目

我已经看过几个关于在Android Studio中使用AndroidAnnotations框架编译Android应用程序的讨论和博客,特别是这个,但没有一个帮助我开始.

我正在使用Android Studio 0.8.9,我指向下载的Gradle 2.1二进制文件.我正在使用Ubuntu 14.04.

gradle编译过程说我在给定位置缺少AndroidManifest.xml的副本(请参阅下面的输出)虽然我在使用文件浏览器导航到此文件夹时找到了它.

另请注意,为了删除警告,我更换了

variant.processResources.manifestFile
Run Code Online (Sandbox Code Playgroud)

通过

variant.outputs.processResources.manifestFile
Run Code Online (Sandbox Code Playgroud)

在gradle脚本中.

这是我的gradle构建脚本:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.13.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.3'
    }
}

apply plugin: 'android'

repositories {
    mavenCentral()
}

configurations {
    apt
}

dependencies {
    compile fileTree(include: '*.jar', dir: 'libs')
    compile files('libs/androidsvg-1.2.1.jar')
    compile 'com.android.support:appcompat-v7:20.0.0'
    compile 'org.androidannotations:androidannotations-api:3.0'
    apt 'org.androidannotations:androidannotations:3.0'
}

android {
    compileSdkVersion 20
    buildToolsVersion "20.0.0"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 20
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs …
Run Code Online (Sandbox Code Playgroud)

android gradle android-annotations android-studio

16
推荐指数
1
解决办法
1万
查看次数

基于canvas的Angular2组件:如何在里面绘制?

我写了一个基于画布的简单组件,我正在使用随附类(TypeScript代码)中的Input()属性进行调整.我想要做的是在伴侣类中绘制canvas元素,其代码如下:实现它的最简单方法是什么?(请参阅代码中的注释:我想在构造函数的画布中绘制一个蓝色矩形).

import {Component, View, Input} from 'angular2/core';

@Component({
    selector: 'chess-diagram',
})
@View({
    template: `<canvas class='chess-diag'
     [attr.width]='_size'
     [attr.height]='_size'></canvas>`,
})
export class ChessDiagram {
    private _size: number;

    constructor(){
        this._size = 150;
        // Here I would like to draw a blue rectangle inside the canvas.
    }

    get size(){
        return this._size;
    }

    @Input () set size(newValue: number){
        this._size = Math.floor(newValue);
    }
}
Run Code Online (Sandbox Code Playgroud)

javascript typescript angular

12
推荐指数
1
解决办法
2万
查看次数

View.resolveSizeAndState()的第三个参数的效用是什么?

我去了官方doc页面android google官方文档,但似乎他们犯了一个严重错误:我们没有关于该方法的第三个参数的信息.所以我只是想知道是否有人已经知道如何定义第三个int参数.

android

10
推荐指数
1
解决办法
2290
查看次数

为什么Comparator.comparing不能与String :: toLowerCase方法引用一起使用?

我试图通过反向顺序(忽略大小写)对字符串数组进行排序,而不进行修改,只打印它.所以我使用的是Java8流.但我无法做到这一点.

这是我的尝试:

package experimentations.chapter02;

import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.Collectors;

public class StringStream {

    public static void main(String[] args) {
        sortStrings();
    }

    public static void sortStrings(){
        String[] stringsArray = "The quick brown fox has a dirty ladder".split("\\s+");
        System.out.println(
                Arrays.stream(stringsArray)
                .sorted(Comparator.comparing(String::toLowerCase).reversed())
                .collect(Collectors.toList())
        );
    }

}
Run Code Online (Sandbox Code Playgroud)

这里的问题是String::toLowerCase静态方法不接受Comparator.comparing.

同时,我设法对数组进行排序,但修改它:

public static void sortStrings(){
        String[] stringsArray = "The quick brown fox has a dirty ladder".split("\\s+");
        System.out.println(
                Arrays.stream(stringsArray)
                .map(String::toLowerCase)
                .sorted(Comparator.reverseOrder())
                .collect(Collectors.toList())
        );
}
Run Code Online (Sandbox Code Playgroud)

那么,最简​​单的解决方法是什么?

java comparator java-8 java-stream method-reference

10
推荐指数
2
解决办法
1万
查看次数