Hello, I'm trying to build my kotlin package with ...
# getting-started
i
Hello, I'm trying to build my kotlin package with gradle. I've setup the build.gradle as follows
Copy code
buildscript {
    ext.kotlin_version = '1.2.21'

    repositories {
	    jcenter()
    	mavenCentral()
    }

    dependencies {
	classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
	classpath "org.jetbrains.kotlin:kotlin-stdlib"
    }
}

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.2.21"
}

sourceSets {
    main.kotlin.srcDirs += "./"
}
I'm getting errors related to
kotlin-stdlib
like
Copy code
e: /home/ishan/code/kotlin-server/server/HttpRequest.kt: (8, 19): Unresolved reference: HashMap                                                                                                                       
e: /home/ishan/code/kotlin-server/server/HttpRequest.kt: (16, 38): Unresolved reference: split                                                                                                                        
e: /home/ishan/code/kotlin-server/server/HttpResponse.kt: (22, 31): Unresolved reference: toByteArray                                                                                                                 
e: /home/ishan/code/kotlin-server/server/HttpStatusCodes.kt: (3, 22): Unresolved reference: HashMap                                                                                                                   
e: /home/ishan/code/kotlin-server/server/HttpStatusCodes.kt: (3, 45): Unresolved reference: hashMapOf
a
@Ishan Khare Is this the whole build.gradle?
if yes, you need to move the dependency of
kotlin-stdlib
out of the
buildscript
-scope
i
yes this is the whole build.gradle
after that change i get the following error
Copy code
FAILURE: Build failed with an exception.             

* Where:                  
Build file '/home/ishan/code/kotlin-server/server/build.gradle' line: 19                                  

* What went wrong:        
A problem occurred evaluating root project 'server'. 
> Could not find method classpath() for arguments [org.jetbrains.kotlin:kotlin-stdlib] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
a
change
classpath
to
compile
, because you require the stdlib to be present during compilation
i
This is the update build.gradle
Copy code
buildscript {
    ext.kotlin_version = '1.2.21'

    repositories {
	jcenter()
	mavenCentral()
    }

    dependencies {
	classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

plugins {
    id "org.jetbrains.kotlin.jvm" version "1.2.21"
}

dependencies {
    classpath "org.jetbrains.kotlin:kotlin-stdlib"
}

sourceSets {
    main.kotlin.srcDirs += "./"
}
i
okay, that worked this time
but i now get an error
Copy code
FAILURE: Build failed with an exception.             

* What went wrong:        
Execution failed for task ':compileKotlin'.          
> Could not resolve all files for configuration ':compileClasspath'.                                      
   > Cannot resolve external dependency org.jetbrains.kotlin:kotlin-stdlib:1.2.21 because no repositories are defined.
     Required by:         
         project :
a
you need the
repositories
outside of
buildscript
too
so that gradle can resolve the
kotlin-stdlib
from maven-central
just copy
repositories
i
it worked
👍 1
does it generate a jar file somewhere in the builds directory?
how do I use the compiled output in any program?
lets say i just want to import from this package in a
main
function?
a
@Ishan Khare do you use Intellij? Are both projects using gradle?
i
i don't use Intellij, I currently have this project setup using gradle the other one is not using it currently
a
I'd say that its best to use gradle for both projects, then you can let gradle do all of the work for you: https://docs.gradle.org/current/userguide/multi_project_builds.html
i
okay, and how do i distribute this library as a gradle dep?
a
you mean how do you include one gradle project in another?
i
yes
a