Kotlin Js file size is too big - kotlin

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
}
}
}
}
}

Related

Using insecure protocols with repositories on gradle. React Native

I'm upgrading the React Native version to the 0.69.3. And when I try to run on android. I get this error:
Using insecure protocols with repositories, without explicit opt-in, is unsupported. Switch Maven repository 'maven9(http://repo.brightcove.com/releases)' to redirect to a secure protocol (like HTTPS) or allow insecure protocols.
I tried the following solutions:
Add the allowInsecureProtocol = true param.
Just add the s on the http.
But still throw me this error.
This is my code in that part:
allprojects {
repositories {
mavenLocal()
maven {
url("$rootDir/../node_modules/react-native/android")
}
maven {
url("$rootDir/../node_modules/jsc-android/dist")
}
maven { url 'https://artifactory.img.ly/artifactory/imgly' }
mavenCentral {
content {
excludeGroup "com.facebook.react"
}
}
google()
jcenter()
maven { url 'https://repo.brightcove.com/releases' }
maven {
url "http://repo.brightcove.com/releases"
allowInsecureProtocol = true
}
maven { url 'https://maven.google.com' }
maven { url "https://www.jitpack.io" }
}
}

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.

How can I customize a KotlinCompile task with a Gradle Kotlin buildSrc plugin?

I'm trying to simplify some of my Gradle builds. A typical build.gradle.kts looks like
plugins {
base
idea
java
id("biz.aQute.bnd.builder")
kotlin("jvm")
id("org.jetbrains.dokka")
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.osgi:osgi.annotation:7.0.0")
implementation("org.osgi:osgi.cmpn:7.0.0")
implementation("org.osgi:osgi.core:7.0.0")
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.2")
}
tasks.compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
}
tasks.compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
}
version = "0.0.0"
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
tasks.jar {
archiveBaseName.set("project.name.api")
}
tasks.named<Test>("test") {
useJUnitPlatform()
}
I'm trying to reduce repetition, specifically with the tasks.compileKotlin and tasks.compileTestKotlin blocks.
I've read that I should use the buildSrc folder for defining simplifications like this. The structure of the folder is
buildSrc/
src/
main/
kotlin/
Example.kt
build.gradle.kts
Example.kt
import org.gradle.kotlin.dsl.KotlinBuildScript
import org.gradle.kotlin.dsl.dependencies
import org.gradle.kotlin.dsl.kotlin
//import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
fun KotlinBuildScript.configureModule() {
tasks.named<KotlinCompile>("compileKotlin") {
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
}
}
buildSrc/build.gradle.kts
plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}
But this setup just results in an Unresolved reference: kotlin error during the build.
I can't figure out what I need to change where in order to successfully reference the KotlinCompile class. I tried specifying the kotlin plugin, idea plugin, and tried adding dependencies to kotlin in the buildSrc/build.gradle.kts, but I haven't been able to find any examples on github or google on how to set this up.
I'd like to do the same for most of the build script as well, not just the KotlinCompile task, so any guidance on what or how I should search for these other task types would be appreciated.
Looks like you're creating an extension function on KotlinBuildScript which may or may not be the issue. Regardless, there's no need for extension functions since you can just use the DSL directly.
Gradle calls the shared build logic convention plugins: https://docs.gradle.org/current/samples/sample_convention_plugins.html#compiling_convention_plugins
So your Example.kt would become kotlin-conventions.gradle.kts:
// kotlin-conventions.gradle.kts
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
}
}
Then inside your main Gradle build file, apply the convention plugin:
// build.gradle.kts
plugins {
`kotlin-conventions`
}
Credit goes to Francisco Mateo's answer, this exists only for a complete final product
One additional problem I was having, was that I was trying to use kotlin-gradle-plugin:1.4.31 in by buildSrc/build.gradle.kts file. There's some weird bug there, but downgrading slightly to 1.4.30 resolved the contributing issue.
My final configuration looks like
buildSrc/build.gradle.kts
plugins {
`kotlin-dsl`
}
repositories {
mavenCentral()
}
dependencies {
implementation("biz.aQute.bnd.builder:biz.aQute.bnd.builder.gradle.plugin:5.3.0")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30")
implementation("org.jetbrains.dokka:dokka-gradle-plugin:1.4.20")
}
Each of the external plugins required in the subsequent plugin file need to be specified as dependencies in the buildSrc/build.gradle.kts file, as specified in the Applying an external plugin in precompiled script plugin section from the link in Francisco's answer.
The plugin file also needs the .gradle.kts extension (if using Kotlin), so my buildSrc/src/main/kotlin/hummingbird-conventions.gradle.kts file looks like
plugins {
base
idea
java
id("biz.aQute.bnd.builder")
id("org.jetbrains.dokka")
kotlin("jvm")
}
repositories {
mavenCentral()
}
dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation("org.osgi:osgi.annotation:7.0.0")
implementation("org.osgi:osgi.cmpn:7.0.0")
implementation("org.osgi:osgi.core:7.0.0")
testImplementation("com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0")
testImplementation("org.junit.jupiter:junit-jupiter-api:5.4.2")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.4.2")
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
tasks {
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
useIR = true
}
}
named<Test>("test") {
useJUnitPlatform()
}
}
And I could apply this in the subproject's build.gradle.kts file like
plugins {
`hummingbird-conventions`
}
where the plugin id is the file name in the buildSrc src folder.

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"
}

protobuf gradle plugin does not compile

I'm trying to compile protobuf files using the gradle plugin, but I get the following error:
java.io.IOException: Can't write [/Users/elavi/dev/sdk3/android/showcaseapp/build/intermediates/multi-dex/debug/componentClasses.jar]
(Can't read [/Users/elavi/.gradle/caches/modules-2/files-2.1/com.google.protobuf/protobuf-java/3.0.0/6d325aa7c921661d84577c0a93d82da4df9fa4c8/protobuf-java-3.0.0.jar(;;;;;;**.class)]
(Duplicate zip entry [protobuf-java-3.0.0.jar:com/google/protobuf/ExperimentalApi.class]))
Not sure why this happens...
The protobuf files are generated correctly, as expected, but then the final step fails with this weird error.
This is my gradle file:
apply plugin: 'com.android.library'
apply plugin: 'com.google.protobuf'
apply plugin: 'idea'
group = GROUP
version = VERSION_NAME
apply from: 'versioning.gradle'
buildscript {
repositories {
mavenCentral()
}
}
android {
compileSdkVersion 26
buildToolsVersion '26.0.2'
flavorDimensions "default"
defaultConfig {
minSdkVersion 15
targetSdkVersion 26
versionCode buildVersionCode()
versionName VERSION_NAME
consumerProguardFiles 'tangram-proguard-rules.txt'
}
// Add proto files location to be used with the protobuf plugin
sourceSets {
main {
proto {
srcDir '../../common/vendored/proto'
}
}
}
}
dependencies {
compile 'com.google.protobuf:protobuf-lite:3.0.0'
compile 'io.grpc:grpc-stub:1.6.1'
compile 'io.grpc:grpc-protobuf:1.0.0-pre2'
compile 'javax.annotation:javax.annotation-api:1.2'
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'com.android.support:support-annotations:27.0.0'
implementation project(':core')
}
// Protobuf configuration. Taken from the documentation: https://github.com/google/protobuf-gradle-plugin
protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0' } plugins {
javalite {
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0-pre2'
} } generateProtoTasks {
all().each { task ->
task.builtins {
remove java
}
task.plugins {
javalite { }
grpc {
option 'lite'
}
}
} } generatedFilesBaseDir = "$projectDir/build/gen" }
clean { delete protobuf.generatedFilesBaseDir }
idea { module {
sourceDirs += file("${protobuf.generatedFilesBaseDir}/main/java");
sourceDirs += file("${protobuf.generatedFilesBaseDir}/main/grpc"); } }
//apply from: file('gradle-mvn-push.gradle')
I simply added what's written in the protobuf readme (https://github.com/google/protobuf-gradle-plugin), didn't do any fancy stuff...
maybe you should remove compile 'com.google.protobuf:protobuf-lite:3.0.0' entry on dependencies section, also you have duplicated entries and some config on last versions are missing. For other side maybe the path for proto sources have issues, my protos are src/main/proto but I only declared proto alone. My brief config is next:
app build.gradle:
apply plugin: 'com.google.protobuf'
android {
...
sourceSets {
main {
proto {
}
}
}
...
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'io.grpc:grpc-okhttp:1.10.0'
implementation 'io.grpc:grpc-protobuf-lite:1.10.0'
implementation 'io.grpc:grpc-stub:1.10.0'
implementation 'javax.annotation:javax.annotation-api:1.2'
// full protobuf (optional)
// protobuf 'com.google.protobuf:protobuf-java:3.4.0'
...
}
protobuf {
protoc {
artifact = "com.google.protobuf:protoc:3.0.2"
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.1.2'
}
javalite {
artifact = "com.google.protobuf:protoc-gen-javalite:3.0.0"
}
}
generateProtoTasks {
all().each { task ->
task.plugins {
javalite {}
grpc {
// Options added to --grpc_out
option 'lite'
}
}
}
}
generatedFilesBaseDir = "$projectDir/build/generated"
}
main project build.gradle:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
classpath "com.google.protobuf:protobuf-gradle-plugin:0.8.2"
...
}
}