Kotlin Packages

Hey all!

Hopefully there’s a quick and easy answer to this, I am coding kotlin using bash to compile and run my kotlin files. My problem is I tried downloading some libraries (the okhttp group) and I’m unsure how to use them. I tried putting them in my working directory, where I have ParentDirectory/pullDatabase.jar and ParentDirectory/okhttp3 folder, which is my downloaded library full of jar files.

In my pullDatabase.kt file I have my header of:

package okhttp3.guide;

import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

But when I try compiling it throws like 20 unresolved references and expecting member declarations. Anyone able to clue me in where I’m wrong? I know how to do it using Android SDK but it would be nice to be able to code without a huge IDE.

Any help would be huge, thanks!

I don’t know how to compile kotlin (with libraries, etc) using bash(command line) but I imagine that it works the same way as any other jar in your project. Just make sure you have all transitive dependencies as well.

Just out of interest, what’s wrong with gradle or maven? No need for a hughe IDE, just gradle and your text editor of choice.

Yeah, I know my okhttp3 relies on okio, think I set that up correctly. I was looking for a cleaner way to have my program import the necessary files through the compiling process rather than having to put them all into bash, kinda like C. But I could do a make file to clean that up.

I’m new to kotlin and was hoping to figure out the language itself first, then move on to learning the full SDK later. Are you able to use gradle/maven outside of SDK? What exactly do those files do? I understand to use additional libraries you just throw them into the dependencies but beyond that gradle is a mystery.

The basic idea is that gradle manages your dependencies, so you just say “I want okhttp3” and gradle automatically pulls all dependencies for okhttp3, eg okio and also all dependencies for that, etc.

The truth is that kotlin is meant to be used with a build system (mainly gradle but maven is also supported). While the command line works(it is actually used by all build tools) it is not well documented and not really used by anyone. The basics of gradle are pretty easy, but it can get very powerful (and complex) for big projects (look at the gradle files of the kotlin compiler :wink:).
A simple example is here: kotlin-dsl-samples/build.gradle.kts at master · gradle/kotlin-dsl-samples · GitHub

plugins {
    application
    kotlin("jvm") version "1.3.21"
}

application {
    mainClassName = "samples.HelloWorldKt"
}

dependencies {
    compile(kotlin("stdlib"))
}

repositories {
    jcenter()
}

the plugins section tells gradle that you are building an application (runnable, if you create a library just leave out that line) and that you are using kotlin plus the version.
For the application you need to specify the main class.
Than you have the dependencies and repositories blocks. Dependencies is where you put okhttp3, etc.

implementation("com.squareup.okhttp3:okhttp:4.2.2")  // compile is also fine, if you create a library you should check the difference between "implementation" and "api". "compile" is the old system and works as well

The repositories block tells gradle where to download the jars from. In most cases jcenter() is enough, most libraries are uploaded there.
Also nearly all libraries will have an instruction of what to add to the dependencies block in their documentation(okhttp3 example).

Also you should read https://kotlinlang.org/docs/reference/using-gradle.html
You can skip the end starting from annotation processing (this goes into some more advanced features). It also links some more examples at the bottom.

Once you have that set up you can simply build by typing gradle build. Obviously this requires you to install gradle first.

First off, huge thanks, great breakdown.

That makes sense, I was curious since the gradle files are focused on heavily with SDK. So my impression is that gradle is very similar to a make file, am I correct or totally off?

So for my pullDatabase function, gonna look something like

plugins {
application
kotlin(“jvm”) version “1.3.61”
}

application {
mainClassName = “main”
}

dependecnies {
implementation(“com.squareup.okhttp:okhttp:4.2.2”)
}

repositories {
jcenter()
}

Another question, should I include the std-lib under dependencies or will the default library be automatically applied?

Definitely reading up on gradle. And I just type gradle build in bash then the regular java -jar pullDatabase.jar to execute?

Pretty much.

You need to include the standard library yourself. You can use implementation(kotlin("stdlib")) for that.

Gradle provides multiple tasks. gradle build basically runs all the tasks required to run the application and also runs all the tests you have in “src/test/kotlin”. If you want to skip the tests you can use gradle assemble. Just try gradle tasks hand gradle help --task <taskname> to get an overview of what gradle can do.

Yes and no. There are a few more things you have to consider.

  1. the output directory for the jar is build/libs
  2. this folder will only contain the jar of your code so you can’t simply run it with java -jar because you are missing all the dependencies.
    There are plugins to create a so called “fat jar” which will bundle all dependencies into one jar.

There are however a few other ways. If you just want to test localy you can use gradle run which will run your program. If you want to share it with someone else first you can use gradle assembleDist which will call both gradle distZip and gradle distTar, which you can also use. The jars contain 2 folders bin and lib. lib contains all the jars and bin contains a batch file for windows and a shell script for linux/ios which can be used to run the program. They just contain a bit more complex version of “java -jar <all the required jars>” that is ensured to run on all (well most) java platforms.

Great, I have experience with makefiles so that gives me some confidence. I put the stdlib in and have been making good use of the gradle tasks.

That makes total sense, definitely going to make a fat jar and get some use out of gradle run.

Again, thanks a ton for being patient and willing to teach. Can I give you a recommendation on linked-in or something? At the very least I’ll be sure to pay it forward!

Lul I’m still in University. No need to do anything for me. Just join the comunity and have fun with kotlin. I’m here because I have fun answering questions :wink: But thanks anyways.