如何在kotlin程序中添加库?

时间:2017-07-19 06:56:26

标签: kotlin libraries

我想在intellij想法中将库(如koma,kUnits)添加到我的kotlin程序中,但是没有任何方法可以添加它们的依赖项。我无法在程序中找到gradle文件

2 个答案:

答案 0 :(得分:1)

@ Aman Gautam's answer中提到的链接已断开,因此这是替代答案。只需运行代码片段,然后查看网页:

<div id="content">
<h1 class="title">Kotlin with Gradle</h1>
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">Why Kotlin and Gradle</a></li>
<li><a href="#sec-2">Get Started</a></li>
<li><a href="#sec-3">Building with Gradle</a>
<ul>
<li><a href="#sec-3-1">Build</a></li>
<li><a href="#sec-3-2">Gradle Run</a></li>
<li><a href="#sec-3-3">Passing Arguments</a></li>
</ul>
</li>
<li><a href="#sec-4">Using External Libraries</a>
<ul>
<li><a href="#sec-4-1">JSON</a></li>
</ul>
</li>
</ul>
</div>
</div>

<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1">Why Kotlin and Gradle</h2>
<div class="outline-text-2" id="text-1">
<p>
Compared with clojure and scala, kotlin is much lighter, as I'm going
to write some android apps, it does matter. I've also tried mirah, but
kotlin seems more promising.
</p>

<p>
Comming from the groovy world, Gradle has now become the de facto
build tool for android development. I have very little experience with
ant and maven, but gradle is more succinct and declarative.
</p>
</div>
</div>

<div id="outline-container-sec-2" class="outline-2">
<h2 id="sec-2">Get Started</h2>
<div class="outline-text-2" id="text-2">
<p>
Gradle does have some project templates included, but very limited
compared to <code>lein</code>. A list of templates can be get with <code>gradle help
--task :init</code>, groovy and scala are included, but not kotlin. Just use
the java-library template:
</p>

<div class="org-src-container">

<pre class="src src-shell">gradle init --type java-library
</pre>
</div>

<p>
this will generate a bunch of files:
</p>

<pre class="example">.
├── build.gradle
├── gradle
│&nbsp;&nbsp; └── wrapper
│&nbsp;&nbsp;     ├── gradle-wrapper.jar
│&nbsp;&nbsp;     └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── src
    ├── main
    │&nbsp;&nbsp; └── java
    │&nbsp;&nbsp;     └── Library.java
    └── test
        └── java
            └── LibraryTest.java

7 directories, 8 files
</pre>

<p>
run <code>git init</code>, add a simple <code>.gitignore</code>
</p>

<pre class="example">.gradle
/build
</pre>

<p>
It is said that the gradle stuff(<code>gradlew</code>, <code>gradle/wrapper</code>) should
be included, so that the project can be build with the exact version
of gradle in the future.
</p>
</div>
</div>

<div id="outline-container-sec-3" class="outline-2">
<h2 id="sec-3">Building with Gradle</h2>
<div class="outline-text-2" id="text-3">
<p>
Modify <code>build.gradle</code>, add dependencies for kotlin:
</p>

<div class="org-src-container">

<pre class="src src-groovy">apply <span style="color: #66D9EF;">plugin</span>: <span style="color: #E6DB74;">'kotlin'</span>

buildscript {
  ext.kotlin_version = <span style="color: #E6DB74;">'0.10.195'</span>
  repositories {
    mavenCentral()
  }
  dependencies {
    classpath <span style="color: #E6DB74;">"org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"</span>
  }
}
dependencies {
    compile <span style="color: #E6DB74;">'org.slf4j:slf4j-api:1.7.7'</span>
    compile <span style="color: #E6DB74;">"org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"</span>
    testCompile <span style="color: #E6DB74;">'junit:junit:4.12'</span>
}
</pre>
</div>
</div>

<div id="outline-container-sec-3-1" class="outline-3">
<h3 id="sec-3-1">Build</h3>
<div class="outline-text-3" id="text-3-1">
<p>
Create the directory to put kotlin source:
</p>

<div class="org-src-container">

<pre class="src src-shell">mkdir -p src/main/kotlin
touch src/main/kotlin/Main.kt
</pre>
</div>

<p>
add a simple <code>Main.kt</code>:
</p>

<div class="org-src-container">

<pre class="src src-kotlin"><span style="color: #F92672;">fun</span> main(args: <span style="color: #66D9EF;">Array&lt;String&gt;</span>) {
    println(<span style="color: #E6DB74;">"kotlin!"</span>)
}
</pre>
</div>

<p>
now build
</p>

<div class="org-src-container">

<pre class="src src-shell">gradle build
</pre>
</div>
</div>
</div>

<div id="outline-container-sec-3-2" class="outline-3">
<h3 id="sec-3-2">Gradle Run</h3>
<div class="outline-text-3" id="text-3-2">
<p>
It would be sweet to run the program directly, and there is a
<code>application</code> plugin for that purpose. Add two more lines to
<code>build.gradle</code>, to apply the plugin and specify the entry class:
</p>

<pre class="example">apply plugin: 'application'

mainClassName = "_DefaultPackage"
</pre>

<p>
Now the project can be build and launched with one single command
</p>

<div class="org-src-container">

<pre class="src src-shell">gradle run
</pre>
</div>
</div>
</div>

<div id="outline-container-sec-3-3" class="outline-3">
<h3 id="sec-3-3">Passing Arguments</h3>
<div class="outline-text-3" id="text-3-3">
<p>
It turns out to be a little tricky, but here is what I end up with:
</p>

<div class="org-src-container">

<pre class="src src-shell">gradle run -Dexec.args=<span style="color: #E6DB74;">"placeholder arg1 arg2"</span>
</pre>
</div>

<p>
A <code>run</code> block is needed in <code>build.gradle</code>:
</p>

<div class="org-src-container">

<pre class="src src-groovy">run {
  <span style="color: #66D9EF;">args</span> System.<span style="color: #FD971F;">getProperty</span>(<span style="color: #E6DB74;">"exec.args"</span>).split() 
}
</pre>
</div>

<p>
Update <code>Main.kt</code> to use the <code>args</code>:
</p>

<div class="org-src-container">

<pre class="src src-kotlin"><span style="color: #F92672;">fun</span> fib(n: <span style="color: #66D9EF;">Int</span>)<span style="color: #66D9EF;">:Int</span> {
    <span style="color: #F92672;">return</span> when (n) {
           <span style="color: #AE81FF;">0</span>,<span style="color: #AE81FF;">1</span> -&gt; <span style="color: #AE81FF;">1</span>
           <span style="color: #F92672;">else</span> -&gt; fib(n - <span style="color: #AE81FF;">1</span>) + fib(n - <span style="color: #AE81FF;">2</span>)
    }
}

<span style="color: #F92672;">fun</span> main(args: <span style="color: #66D9EF;">Array&lt;String&gt;</span>) {
    <span style="color: #F92672;">if</span> (args.size() &gt; <span style="color: #AE81FF;">1</span>) {
        println(fib(args[<span style="color: #AE81FF;">1</span>].to<span style="color: #66D9EF;">Int</span>()))
    } <span style="color: #F92672;">else</span> {
        println(<span style="color: #E6DB74;">"i need a number"</span>)
    }
}
</pre>
</div>
</div>
</div>
</div>

<div id="outline-container-sec-4" class="outline-2">
<h2 id="sec-4">Using External Libraries</h2>
<div class="outline-text-2" id="text-4">
<p>
Declear your dependencies in <code>build.gradle</code>:
</p>

<pre class="example">dependencies {
    ...
    compile 'com.github.kevinsawicki:http-request:6.0'
    ...
}
</pre>

<p>
Import, nothing fancy:
</p>

<div class="org-src-container">

<pre class="src src-kotlin"><span style="color: #F92672;">import</span> com.github.kevinsawicki.http.<span style="color: #66D9EF;">HttpRequest</span>

<span style="color: #F92672;">fun</span> main(args: <span style="color: #66D9EF;">Array&lt;String&gt;</span>) {
    <span style="color: #F92672;">val</span> <span style="color: #FD971F;">url</span><span style="color: #66D9EF;">:String</span> = <span style="color: #E6DB74;">"https://hacker-news.firebaseio.com/v0/topstories.json"</span>
    <span style="color: #F92672;">val</span> <span style="color: #FD971F;">response</span><span style="color: #66D9EF;">:String</span> = <span style="color: #66D9EF;">HttpRequest</span>.get(url).body()
    println(response)
}
</pre>
</div>
</div>

<div id="outline-container-sec-4-1" class="outline-3">
<h3 id="sec-4-1">JSON</h3>
<div class="outline-text-3" id="text-4-1">
<pre class="example">dependencies {
    ...
    compile 'com.fasterxml.jackson.module:jackson-module-kotlin:2.5.1'
    ...
}
</pre>

<div class="org-src-container">

<pre class="src src-kotlin"><span style="color: #F92672;">import</span> com.github.kevinsawicki.http.<span style="color: #66D9EF;">HttpRequest</span>
<span style="color: #F92672;">import</span> com.fasterxml.jackson.module.kotlin.*

<span style="color: #F92672;">fun</span> main(args: <span style="color: #66D9EF;">Array&lt;String&gt;</span>) {
    <span style="color: #F92672;">val</span> <span style="color: #FD971F;">url</span><span style="color: #66D9EF;">:String</span> = <span style="color: #E6DB74;">"https://hacker-news.firebaseio.com/v0/topstories.json"</span>
    <span style="color: #F92672;">val</span> <span style="color: #FD971F;">response</span><span style="color: #66D9EF;">:String</span> = <span style="color: #66D9EF;">HttpRequest</span>.get(url).body()
    <span style="color: #F92672;">val</span> <span style="color: #FD971F;">mapper</span> = jackson<span style="color: #66D9EF;">ObjectMapper</span>()
    <span style="color: #F92672;">val</span> <span style="color: #FD971F;">ids</span><span style="color: #66D9EF;">:Array&lt;Int&gt;</span> = mapper.read<span style="color: #66D9EF;">Value</span>(response)
    <span style="color: #F92672;">for</span> (id in ids) {
        println(id)
    }
}
</pre>
</div>
</div>
</div>
</div>
</div>

答案 1 :(得分:0)

我自己也找到了答案。请查看以下链接,详细了解如何使用koltin&#39;。

http://zweifisch.github.io/kotlin-with-gradle.html