Gradle DSL method not found: 'compileOnly()' - intellij-idea

I'm trying to add new module to my application. I successfully added movie-api module (you can see in picture below), but when I try to add another module (client-app), I'm getting error as shown in picture.
I tried with different solutions including Gradle DSL method not found: 'compile()', but didn't work for me.
Appreciate your help!
Build.gradle file:
buildscript {
ext {
springBootVersion = '1.4.3.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
jar {
baseName = 'client-app'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
compile('org.springframework.boot:spring-boot-starter')
compileOnly('org.projectlombok:lombok')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

compileOnly was introduced in Gradle 2.12. Make sure you are using a new enough version, with both the command-line and the IDE.

Related

WARNING: API 'variant.getPackageLibrary()' is obsolete and has been replaced with 'variant.getPackageLibraryProvider()'

I just upadated kotlin to 1.3.30 and I now get this error when syncing gradle:
WARNING: API 'variant.getPackageLibrary()' is obsolete and has been
replaced with 'variant.getPackageLibraryProvider()'. It will be
removed at the end of 2019. For more information, see
https://d.android.com/r/tools/task-configuration-avoidance. To
determine what is calling variant.getPackageLibrary(), use
-Pandroid.debug.obsoleteApi=true on the command line to display a stack trace. Affected Modules: hydatabase
Here is my build.gradle:
apply plugin: 'com.squareup.sqldelight'
apply plugin: 'kotlin-multiplatform'
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 19
}
lintOptions {
abortOnError false
}
}
sqldelight {
Database {
packageName = "com.company.hydatabase"
}
}
kotlin {
targets {
fromPreset(presets.jvm, 'jvm')
fromPreset(presets.android, 'android')
}
sourceSets {
commonMain.dependencies {
api 'org.jetbrains.kotlin:kotlin-stdlib-common'
}
jvmMain.dependencies {
api 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
// ICU4J: Use DecimalFormat
// Get rid of this when minSDKLevel = API 24 - Nougat (7.0)
// https://developer.android.com/guide/topics/resources/internationalization.html
api 'com.ibm.icu:icu4j:60.2'
}
androidMain.dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
api "com.squareup.sqldelight:android-driver:1.1.1"
}
androidMain.dependsOn jvmMain
}
}
task copyDatabase(type: Copy) {
from "${rootProject.file('hyappcommon/Databases/').path}"
into "${rootProject.file('hydatabase/src/main/assets/databases/').path}"
include '**/*.sqlite'
}
preBuild.dependsOn(copyDatabase)
// workaround for https://youtrack.jetbrains.com/issue/KT-27170
configurations {
compileClasspath
}
If you debug, it shows
REASON: The Kotlin plugin is currently calling this API. We are working to solve this.
To see this error please run
./gradlew -Pandroid.debug.obsoleteApi=true --stacktrace
As tommyboy said, the Kotlin plugin is calling this deprecated API.
If you don't want to get this warning while Kotlin is working on this, you can just use the previous version of Kotlin plugin like:
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.21"
}
It's probably a bug and fixed soon
You can revert back to the previous version or add this line to gradle.properties
android.debug.obsoleteApi=true
In my project gradle file I had
buildscript {
ext.kotlin_version = '1.3.31'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
Simple changing
ext.kotlin_version = '1.3.31' to ext.kotlin_version = '1.3.41' solved the problem
when using version 1.3.31 i had tried gradlew -Pandroid.debug.obsoleteApi=true
It mentioned
WARNING: API 'variant.getPackageLibrary()' is obsolete and has been
replaced with 'variant.getPackageLibraryProvider()'. It will be
removed at the end of 2019.
For more information, see https://d.android.com/r/tools/task-configuration-avoidance.
REASON: The Kotlin plugin is currently calling this API. We are working to solve this.
WARNING: Debugging obsolete API calls can take time during
configuration. It's recommended to not keep it on at all times.
Looks like it is solved in 1.3.41
It's an issue with the Kotlin plugin, as mentioned here. It'll get fixed in a later version.
After I updated Kotlin to 1.3.30, the following dependencies cause the error:
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.4'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// ... other dependencies
}
I have reported the issue here:
https://github.com/bintray/gradle-bintray-plugin/issues/284
https://github.com/dcendents/android-maven-gradle-plugin/issues/81
By the way, you can ignore that error message.
The issue is tracked here and it is fixed.
Just use the Kotlin Gradle plugin v 1.3.40 or higher.
WARNING: API 'variant.getPackageLibrary()' is obsolete and has been replaced with 'variant.getPackageLibraryProvider()'.
It will be removed at the end of 2019.
Just updated to "v1.3.40-release-Studio3.4-1" plugin.
as you can see in https://youtrack.jetbrains.com/issue/KT-30784
I met this problem when I use kotlin plugin with library plugin. I found that if you use kotlin plugin with application plugin, it works well. But if you use kotlin plugin with library plugin, it will cause this problem. So that means:
// work well:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
// error:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
As the error diplayed that, you can use ./gradlew -Pandroid.debug.obsoleteApi=true --stacktrace to find out with module caused this problem.
Then I found that one of my module used the wrong plugin combination above. And that seems to be a bug of kotlin plugin. So finally, I upgraded the kotlin plugin, and then it worked well. Below is the kotlin plugin that I finally used:
buildscript {
ext.kotlin_version = '1.3.40'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
You can run this command in the root project
gradlew -Pandroid.debug.obsoleteApi=true
and the warning will be disappeared.

launch is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2

I'm trying to run the simplest example with coroutines:
import kotlinx.coroutines.*
fun main() {
GlobalScope.launch {
delay(1000L)
println("${Thread.currentThread().name}: World")
}
println("${Thread.currentThread().name}: Hello")
Thread.sleep(2000L)
println("${Thread.currentThread().name}: Finish!")
}
And my build.gradle file looks like this:
buildscript {
// Consider moving these values to `gradle.properties`
ext.kotlin_version = '1.3.0-rc-146'
ext.kotlin_gradle_plugin_version = '1.3.0-rc-198'
ext.kotlinx_coroutines = '1.0.0-RC1'
repositories {
maven { url "https://kotlin.bintray.com/kotlin-eap" }
mavenCentral()
jcenter()
google()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}
}
plugins {
id 'org.jetbrains.kotlin.jvm' version "1.1.51"
}
apply plugin: 'idea'
apply plugin: 'application'
group 'by.kotlin'
version '1.0-SNAPSHOT'
mainClassName = 'MainKt'
repositories {
maven { url "https://kotlin.bintray.com/kotlin-eap" }
mavenCentral()
jcenter()
google()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-common:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinx_coroutines"
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
But when I run this example, I've got the following errors:
e: ...Main.kt: (6, 17): 'launch(CoroutineContext = ..., CoroutineStart = ..., [ERROR : Bad suspend function in metadata with constructor: Function2]<CoroutineScope, Continuation<Unit>, Any?>): Job' is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2
e: ...Main.kt: (7, 9): Suspend function 'delay' should be called only from a coroutine or another suspend function
e: ...Main.kt: (7, 9): 'delay(Long): Unit' is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2
> Task :compileKotlin FAILED
Why do these errors occur? I'm completely confused, because the first error says that launch "is only available since Kotlin 1.3 and cannot be used in Kotlin 1.2", but I use Kotlin 1.3 in my build.gradle file (in particular, '1.3.0-rc-146')...
UPD
It seems that the reason of problem is in IntelliJ IDEA Settings:
But how to fix it, if the latest language version, which can be selected there, is 1.2, not 1.3?
Make sure you have updated Kotlin to 1.3. You can do this from Preference->Lanugage & Framework->Kotlin Updates
Then change the version of kotlin.jvm plugin to 1.3.0 in gradle. (https://plugins.gradle.org/plugin/org.jetbrains.kotlin.jvm)
plugins {
id 'org.jetbrains.kotlin.jvm' version "1.3.0"
}
And for including coroutines
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.jetbrains.kotlinx', name: 'kotlinx-coroutines-core', version: '1.0.0'
}
It should be fine now.
You must change kotlin plugin version
Your current kotlin plugin version is 1.2.51
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.51"
}
this is correct
buildscript {
ext.kotlin_version = '1.3.0'
ext.kotlin_gradle_plugin_version = '1.3.0'
ext.kotlinx_coroutines = '1.0.0'
repositories {
maven { url "https://kotlin.bintray.com/kotlin-eap" }
mavenCentral()
jcenter()
google()
}
dependencies {
'org.jetbrains.kotlin:kotlin-gradle-plugin:'+kotlin_version
}
}
I solved this by manually updating the Kotlin-IntelliJ Plugin.
First, download the newer version of the Kotlin plugin that is compatible with your version of IntelliJ https://plugins.jetbrains.com/plugin/6954-kotlin/versions/stable
Then in IntelliJ's Settings -> Plugins, click on the settings/gear icon on the top-right side. From there choose Install Plugin from Disk..., choose the zip file you got from the intellij website. Then it will ask you to restart the IDE, and that's it :)
If it has started to occur only recently then chances are two links are generating same file with same name, in this case removing one of your recent file from build.gradle will help you resolving the issue.
In my case this was causing the issue
implementation 'com.miguelcatalan:materialsearchview:1.4.0'
implementation 'com.github.Ferfalk:SimpleSearchView:0.2.0'
After removing first one out of them the issue was resolved.

How to make idea plugin in gradle generate proper project configuration for Kotlin?

I have configured my build.gradle to run gradle test and gradle run properly. However, IDEA does not show any run/test tasks after importing the configuration generated by gradle idea. It seems that these tasks are not included at ipr/iws at all.
Here is my build.gradle:
buildscript {
ext.kotlin_version = '1.2.0'
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jetbrains.dokka:dokka-gradle-plugin:$dokka_version"
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.2'
}
}
apply plugin: 'kotlin'
apply plugin: 'org.jetbrains.dokka'
apply plugin: 'application'
apply plugin: 'org.junit.platform.gradle.plugin'
apply plugin: 'idea'
dokka {
outputFormat = 'html'
outputDirectory = "$buildDir/javadoc"
}
sourceSets {
test.kotlin.srcDirs += 'src/test/kotlin'
}
junitPlatform {
enableStandardTestTask true
}
defaultTasks 'run'
mainClassName = 'simpledb.server.Startup'
repositories {
maven { url "http://maven.aliyun.com/nexus/content/groups/public/" }
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile 'org.junit.jupiter:junit-jupiter-api:5.0.2'
testRuntime (
'org.junit.jupiter:junit-jupiter-engine:5.0.2',
'org.junit.platform:junit-platform-launcher:1.0.2'
)
testCompile "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}
I was wondering is there any way to make generated project configurations include these tasks(run/test)? I have also heard people saying Don't use gradle idea, so is it impossible to use gradle idea to implement this?
It is simply not necessary in your case. Just open the build.gradle file with IDEA and everything should be smooth. The idea Gradle plugin is somewhat deprecated. Not officially, but it was created by Gradle team and is not actively developed to adapt to new IDEA versions and features and so on. So if you don't need special customizations, just open the build.gradle with IDEA and you should be good to go.

How to properly configure Kotlin plugin for Gradle?

I'm trying to build a basic Gradle project. My build.gradle file is below. I keep getting the below error. I don't get it because you can find the plugin here... where I think I'm directing to: https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-gradle-plugin
Update: same thing happens to the spring-boot plugin if I comment out the Kotlin line. It's not just specific to the Kotlin plugin.
The error:
Error:(21, 0) Plugin [id: 'kotlin', version: '1.1.1'] was not found in any of the following sources:
- Gradle Core Plugins (not a core plugin, please see https://docs.gradle.org/3.3/userguide/standard_plugins.html for available core plugins)
- Gradle Central Plugin Repository (no 'kotlin' plugin available - see https://plugins.gradle.org for available plugins)
Build.gradle:
buildscript {
ext.kotlin_version = '1.1.1'
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE")
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
plugins {
id 'java'
id 'kotlin' version "1.1.1"
id 'spring-boot'
}
group 'si.dime.kotlin.tutorials.rest'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile("org.springframework.boot:spring-boot-starter-web:1.3.3.RELEASE")
}
For non-core Gradle plugins (those that are not from the org.gradle namespace), you have to use a fully qualified id as well as a version number.
The official Kotlin documentation contains the id to use:
plugins {
id "org.jetbrains.kotlin.jvm" version "<version to use>"
}
(You can also find these ids by searching for the plugin on the plugins.gradle.org site.)

Gradle dependencies in Idea project

I'm trying to use Gradle, but Idea dont see dependices. Here is my build.gradle
group 'me.ozka'
version '1.0.1'
buildscript {
ext.kotlin_version = '1.0.3'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'java'
apply plugin: 'kotlin'
sourceCompatibility = 1.5
repositories {
mavenCentral()
}
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
testCompile group: 'junit', name: 'junit', version: '4.11'
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.20'
compile 'org.eclipse.jetty:jetty-server:9.0.0.RC2'
compile 'org.eclipse.jetty:jetty-servlet:9.0.0.RC2'
}
When i'm trying import import org.eclipse.jetty.server.Server; Idea signal me an error. What i'm doing wrong?
upd: i'm also don't see jar files in Idea External library folder.
upd2: i'm add to build file apply plugin: 'jetty' and it's solve my problem!
You need to refresh your gradle dependencies.
Here are some ways of doing this.
Approach 1 (GUI):
Go to View -> Tool Windows -> Gradle
Click on the big refresh button (Refresh all gradle projects).
Approach 2 (Command):
gradle build --refresh-dependencies