starter program with Compose on desktop very lagging on Linux - kotlin

I just started a desktop project with Intellij, built with the default generated code, ran it on Linux, and it worked. Then I added a counter variable for the button text (shown as follows), it also ran as before. But when I click on the button, the change of text on reaction to the click takes as long as 10 seconds. Wonder what can be wrong.
I am running Xubuntu 22.04.
#Composable
#Preview
fun App() {
var text by remember { mutableStateOf("Hello, World!") }
var ic by remember { mutableStateOf(0) }
MaterialTheme {
Button(onClick = {
ic++
text = "Hello, Desktop! $ic"
}) {
Text(text)
}
}
}
fun main() = application {
Window(onCloseRequest = ::exitApplication) {
App()
}
}
My build.gradle:
import org.jetbrains.compose.compose
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.6.10"
id("org.jetbrains.compose") version "1.1.1"
}
group = "me.xxxx"
version = "1.0"
repositories {
google()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
dependencies {
implementation(compose.desktop.currentOs)
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "11"
}
compose.desktop {
application {
mainClass = "MainKt"
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "Test"
packageVersion = "1.0.0"
}
}
}

Well, it turns out a VirtualBox setting issue. I run Xubuntu on VirtualBox hosted by MacBook Pro. I need to set graphics controller to VMSVGA on VirtualBox. So now it's responsive.

Related

androidx.compose.ui.window.MenuBar cannot be found

I am trying to use the androidx.compose.ui.window.MenuBar from Jetbrain Tutorial but the problem is that when I try to use it in my project it does not show up.
Here is the problem:
It only offers me to import from those 2 locations but when I use the import it does not complain or resolve the error.
build.gradle.kts
import org.jetbrains.compose.compose
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.6.10"
id("org.jetbrains.compose") version "1.1.0"
}
group = "me.whate"
version = "1.0"
repositories {
google()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
dependencies {
implementation(compose.desktop.currentOs)
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
}
compose.desktop {
application {
mainClass = "MainKt"
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "Test"
packageVersion = "1.0.0"
}
}
}
I have tried to invalidate caches, etc but no success so far.
Anybody has any suggestion?
You have to modify it like this:
#OptIn(ExperimentalComposeUiApi::class)
fun main() = application {
Window(onCloseRequest = ::exitApplication) {
var text by remember { mutableStateOf("Hello, World!") }
DesktopMaterialTheme {
MenuBar {
// your menubar code here
}
Button(onClick = {
text = "Hello, Desktop!"
}) {
Text(text)
}
}
}
}
and completely remove the App() function
Prefix MenuBar with FrameWindowScope, e.g. FrameWindowScope.MenuBar

How to fix `Expression 'jar' cannot be invoked as a function` error in gradle file?

I have the following gradle file. I'm getting a Expression 'jar' cannot be invoked as a function error on the line where jar is used (jar {). How can I fix this?
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.6.21"
java
application
}
group = "me.talha"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
testImplementation(kotlin("test"))
}
tasks.test {
useJUnitPlatform()
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "11"
}
application {
mainClass.set("MainKt")
}
jar {
manifest {
attributes 'Main-Class': 'MainKt'
}
}
Turns out I did not need to add any of the jar stuff. I can simply use the gradle distZip task which generates the needed jars and executable scripts and all works fine.

Kotlin with gradle build system cant add JRAW compile "net.dean.jraw:JRAW:1.1.0"

I am all new to Kotlin and gradle and wanted to create a reddit scraper app for fun.
Cant get the guide from this page to work.
https://mattbdean.gitbooks.io/jraw/content/quickstart.html
The guide tells me to put this in the gradle file:
repositories {
jcenter()
}
dependencies {
compile "net.dean.jraw:JRAW:$jrawVersion"
}
But when i put that in my buid.gardle.kts file the ide reports an error
See image below of where the error occurs. The error message is Unexpected tokens.
Found the answer to my question, simply had to add in the variable in the gradle file...
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
var jrawVersion = "1.1.0" //needed to add this in obviously
plugins {
kotlin("jvm") version "1.4.21"
application
}
group = "me.jeppe"
version = "1.0"
repositories {
mavenCentral()
jcenter()
}
dependencies {
testImplementation(kotlin("test-junit"))
compile ("net.dean.jraw:JRAW:$jrawVersion")
}
tasks.test {
useJUnit()
}
tasks.withType<KotlinCompile>() {
kotlinOptions.jvmTarget = "1.8"
}
application {
mainClassName = "MainKt"
}

Page not found message using micronaut, Gradle and Kotlin

I just started using Micronaut, but I can't get a simple endpoint to work. I get this response for http://localhost:8901/game_compositions/
{
"message": "Page Not Found",
"_links": {
"self": {
"href": "/game_compositions/",
"templated": false
}
}
}
I have seen a few other questions here about the same response but none of them helped me. I have annotation processor activated for my build and I do not return null.
I assume I have some simple stupid mistake, but I can't find it. According to the sample projects I seem to have all I need.
This is my project:
Controller:
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces
#Controller("/game_compositions")
class CompositionsController {
#Produces(MediaType.APPLICATION_JSON)
#Get(uri = "/")
fun getCompositions(): Iterable<String> {
return listOf("something")
}
}
Build.gradle (mostly autogenerated from the https://micronaut.io/launch/ site, so there might be some unnecessary stuff there for a simple service. Feel free to point it out)
plugins {
id("io.micronaut.application") version "1.2.0"
kotlin("jvm") version "1.4.10"
kotlin("kapt") version "1.4.10"
kotlin("plugin.allopen") version "1.4.10"
}
version = "0.1"
group = "com.example.backend"
val kotlinVersion=project.properties.get("kotlinVersion")
repositories {
mavenCentral()
jcenter()
}
micronaut {
runtime("netty")
testRuntime("junit5")
processing {
incremental(true)
annotations("com.example.backend.*")
}
}
dependencies {
implementation("io.micronaut:micronaut-validation")
implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
implementation("io.micronaut:micronaut-runtime")
implementation("io.micronaut.sql:micronaut-jdbc-hikari")
implementation("io.micronaut.sql:micronaut-hibernate-jpa")
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
runtimeOnly("ch.qos.logback:logback-classic")
runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
}
application {
mainClass.set("com.example.backend.ApplicationKt")
}
java {
sourceCompatibility = JavaVersion.toVersion("14")
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "14"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "14"
}
}
}
Application.yml (the database connection is so far unused untested)
micronaut:
application:
name: Backend
server:
port: 8901
datasources:
default:
url: jdbc:mysql://localhost:3306/db
username: root
password: root
driver-class-name: org.mariadb.jdbc.Driver
jpa:
default:
properties:
hibernate:
hbm2ddl:
auto: none
show_sql: true
Thanks to Jeff Scott Brown in the comments I have been able to determine that the order of the plugins is the problem. I was not aware that the order mattered in gradle. Apparently Micronaut requires the kapt plugin to be added first. Anyone with more insight in to this is welcome to post a better answer, for me it worked to change the start of my gradle file to this:
plugins {
kotlin("jvm") version "1.4.10"
kotlin("kapt") version "1.4.10"
id("io.micronaut.application") version "1.2.0"
kotlin("plugin.allopen") version "1.4.10"
}
Edit: An important note if anyone attempts to use my gradle file. It turns out kapt does not support java 14 and that caused some very hard to track down trouble for me. This is my current working gradle file:
plugins {
kotlin("jvm") version "1.4.10"
kotlin("kapt") version "1.4.10"
kotlin("plugin.allopen") version "1.4.10"
kotlin("plugin.jpa") version "1.4.10"
id("io.micronaut.application") version "1.2.0"
}
version = "0.1"
group = "com.example.backend"
val kotlinVersion: String by project
repositories {
mavenCentral()
jcenter()
}
micronaut {
runtime("netty")
testRuntime("junit5")
processing {
incremental(true)
annotations("com.example.backend.*")
}
}
dependencies {
annotationProcessor("io.micronaut.data:micronaut-data-processor")
implementation("io.micronaut:micronaut-validation")
implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
implementation("io.micronaut:micronaut-runtime")
implementation("io.micronaut.sql:micronaut-jdbc-hikari")
implementation("io.micronaut.sql:micronaut-hibernate-jpa")
implementation("io.micronaut.data:micronaut-data-hibernate-jpa")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
runtimeOnly("org.mariadb.jdbc:mariadb-java-client")
runtimeOnly("ch.qos.logback:logback-classic")
runtimeOnly("com.fasterxml.jackson.module:jackson-module-kotlin")
}
application {
mainClass.set("com.example.backend.ApplicationKt")
}
java {
sourceCompatibility = JavaVersion.toVersion("11")
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "11"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "11"
}
}
}

Kotlin Js file size is too big

I currently have a problem with the file size of kotlin js. I have one multiplatform lib which has around 160Kb and integrate it in the kotlin js project. The kotlin js project itself does not have that much code.
After assembling (which should enable also DCE for shrinking) the Js file size is up to 2.5MB. I really don't know why it bloats up like that. I hope anybody has a clue. Following is my gradle.config, maybe I forgot something to add.
Short update:
We found out when serving the file of 2.5MB from CDN githack. The file is optimized to 500KB. How is that even possible. Does that mean that the actual DCE of kotlin isn't working at all?
plugins {
kotlin("js") version "1.4.10"
}
group = "me.at"
version = "1.0-SNAPSHOT"
repositories {
mavenLocal()
mavenCentral()
jcenter()
maven {
url = uri("https://dl.bintray.com/kotlin/kotlinx")
}
maven {
url = uri("https://dl.bintray.com/kotlin/kotlin-js-wrappers")
}
maven {
url = uri("https://dl.bintray.com/ekito/koin")
}
}
dependencies {
implementation(kotlin("stdlib-js"))
implementation("org.jetbrains:kotlin-styled:1.0.0-pre.110-kotlin-1.4.10")
implementation(npm("styled-components", "~5.1.1"))
implementation(npm("inline-style-prefixer", "~6.0.0"))
implementation("com.mp.multi:multi:1.1")
implementation("org.koin:koin-core:3.0.0-alpha-4")
implementation(npm("html-webpack-plugin", "3.2.0"))
}
kotlin {
js {
browser {
binaries.executable()
webpackTask {
cssSupport.enabled = true
}
runTask {
cssSupport.enabled = true
}
testTask {
useKarma {
useChromeHeadless()
webpackConfig.cssSupport.enabled = true
}
}
}
}
}