Gradle DSL method not found : testCompile() - android

I am using gradle version - 3.3-all and gradle plugin - gradle:2.3.3
I want to write test cases. So as per this page, I included the following in the the app's top build.gradle file.
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
But I get the error saying
Error:(11, 0) Gradle DSL method not found: 'testCompile()'
Possible causes:<ul><li>The project 'TopLevelProject' may be using a version of the Android Gradle plug-in that does not contain the method (e.g. 'testCompile' was added in 1.1.0).
What could be the issue?

Add the Java plugin
apply plugin: 'java'

In gradle the configurations: compile runtime testCompile etc are coming from java plugin
But in android i think you need apply plugin: 'com.android.application'
Edit: Please share your root build.gradle and app build.gradle especially android closure, dependencies and plugins and describe which is where.
Sometimes is a structural problem and app related stuff lands in root build.gradle

I don't have the reputation to comment, so I post this as an answer.
I had a similar problem, try applying the Java plugin and the testCompiles in the app's build file, that is in: project/app/build.gradle, not the project's build file.
apply plugin: 'com.android.application'
dependencies {
...
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
}

Related

Why does my android studio not see my gradle file?

I'm getting error=
ERROR:
Could not find method testImplementation() for arguments [junit:junit:4.12] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Looks like you are missing android plugin definition in your gradle. Put this line at top in your gradle file.
apply plugin: 'com.android.application'
Also make sure you are using gradle version above 3.0 and have following repositories added in gradle. Try clean and build after you update your gradle.
repositories {
google()
jcenter()
}
So the problem which is occurring in your build.gradle(app), has the solution. You just remove all the testImplementation and androidTestImplementation from your dependencies. I guess you won't find any issues from there onwards. Usually in the app build.gradle only implementations of the libraries should be there. Go ahead and do it, tell me what you get after that.
dependencies{
implementation
// comment or remove all the testCompilations and androidTestCompilations and sync the gradle
//add this at the end of the line, this is for compilation of the gradle
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
I hope it helps.
Just remove the "testCompile 'junit:junit:4.12'" from build.gradle file:
dependencies {
// .......
testCompile 'junit:junit:4.12'//remove this line
//.......
}
OR
add this line at the end of build.gradle file
repositories {
maven { url 'http://repo1.maven.org/maven2' }
}

Exclude a Gradle dependency that is added by the Realm plugin

Sporadically, my Gradle syncs will fail. I'll be given the unhelpful message that "a 3rd party Gradle plugin" may be the cause. If I open up the Event Log, I'll see the message:
Outdated Kotlin Runtime
Your version of Kotlin runtime in 'Gradle: org.jetbrains.kotlin:kotlin-stdlib:1.2.10#jar' library is 1.2.10-release-109 (1.2.10), while plugin version is 1.2.51-release-Studio3.1-1.
Runtime library should be updated to avoid compatibility problems.
The mismatched number is neither the Kotlin version in my Gradle files, or the version of my Kotlin plugin in Android Studio.
After running a Gradle dependency tree, I found the culprit:
+--- io.realm:realm-android-kotlin-extensions:5.1.0
| \--- org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.10 -> 1.2.51
I'm not including any realm-android-kotlin-extensions library. I'm assuming that it's added by classpath "io.realm:realm-gradle-plugin:5.1.0" and apply plugin: "realm-android"
This makes matters difficult. If it were a regular dependency, I could try something like
implementation "io.realm:realm-android-kotlin-extensions:5.1.0" {
transitive = false
}
or
implementation "io.realm:realm-android-kotlin-extensions:5.1.0" {
exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib-jdk7"
}
Theoretically that would probably work. It would be forced to use the newer version of Kotlin, the error would go away, and hopefully Gradle syncs would work and all would be well with the world. But if I try this approach my Gradle sync fails and I get this error in the Event Log:
Gradle sync failed: Could not find method io.realm:realm-android-kotlin-extensions:5.1.0() for arguments [build_2krw7i3nwfkd5lrq1ly9b8huw$_run_closure3$_closure29#7b5b2081] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
I'm assuming that's because this isn't a dependency I added (perhaps not a public dependency?), that it fails because the dependency is added via the plugin instead of directly in my Gradle file.
So how do I fix this? Perhaps there's a line I can add to tell the Realm plugin to exclude the obsolete dependency? Or am I completely barking up the wrong tree and the solution for my Kotlin version clash issue is something else entirely?
(BTW, if you're wondering why I'm using Realm 5.1.0, 5.3.1 causes some weird bugs in our app, so we're waiting for a later version to be released in the hope that will no longer cause the issues.)
One of the things on my personal "I wish I had known this a year ago" list is that you can manually add what Realm adds to your project instead of relying on the Gradle plugin.
buildscript {
ext.kotlin_version = '1.2.51'
ext.realm_version = '5.3.1'
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath "io.realm:realm-transformer:5.1.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
import io.realm.transformer.RealmTransformer
android.registerTransform(new RealmTransformer(project))
dependencies {
implementation "io.realm:realm-annotations:$realm_version"
implementation "io.realm:realm-android-library:$realm_version"
implementation "io.realm:realm-android-kotlin-extensions:$realm_version" {
exclude group: "org.jetbrains.kotlin", module: "kotlin-stdlib-jdk7"
}
kapt "io.realm:realm-annotations-processor:$realm_version"
}
As per docs.

Warning "Kotlin plugin version is not the same as library version" (but it is!)

I have an Android studio project in which I have added a Java library module, which I call core. My three Gradle build files look like this.
project/build.gradle
buildscript {
ext.kotlin_version = '1.2.40'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
core/build.gradle
apply plugin: 'java-library'
apply plugin: 'kotlin'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
...
}
app/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android { ... }
dependencies {
implementation project(':core')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:appcompat-v7:27.1.1'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
testImplementation 'junit:junit:4.12'
}
The problem I have is that, in core/build.gradle, the kotlin-stdlib-jdk7 line is giving me the warning Plugin version (1.2.40) is not the same as library version (jdk7-1.2.40). I have tried changing it to:
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.2.40"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.2.40"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
But the warning is still there. The build still runs successfully, and I know I can surpress the warning without any problems and ignore it, but I really want to know why this is happening and how I can get rid of it. I am using Android Studio 3.0.1. Does anyone know of a solution to this?
Starting from Kotlin 1.4 dependency on the standard library added by default:
You no longer need to declare a dependency on the stdlib library in any Kotlin Gradle project, including a multiplatform one. The dependency is added by default.
The automatically added standard library will be the same version of the Kotlin Gradle plugin, since they have the same versioning.
For platform-specific source sets, the corresponding platform-specific variant of the library is used, while a common standard library is added to the rest. The Kotlin Gradle plugin will select the appropriate JVM standard library depending on the kotlinOptions.jvmTarget compiler option of your Gradle build script.
Link to Kotlin Gradle plugin documentation.
This is a bug in the Kotlin plugin. I've filed an issue in the Kotlin issue tracker. You can simply ignore the message.
EDIT: JetBrains marked the issue as a duplicate of KT-23744 "Kotlin library and Gradle plugin versions are different" inspection false positive for non-JVM dependencies".
Solution, in my case, I got rid of the line
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
in the app level Gradle and the warning disappear
As the Kotlin page says :
" You no longer need to declare a dependency on the stdlib library in any Kotlin Gradle project, including a multiplatform one. The dependency is added by default.
The automatically added standard library will be the same version of the Kotlin Gradle plugin, since they have the same versioning.
For platform-specific source sets, the corresponding platform-specific variant of the library is used, while a common standard library is added to the rest. The Kotlin Gradle plugin will select the appropriate JVM standard library depending on the kotlinOptions.jvmTarget compiler option of your Gradle build script."
You might be facing this after upgrading kotlin version, Actually, older versions are still in your caches, In this case, you need to do the following steps
Invalidate cache
Clean project
Sync project with gradle files
Now your warning will be gone.
[build.gradle(Module)]
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.5.10'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.10'
...
}
My project automatically added
(implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.5.10')
to the project build file. After moving the implementation to the module file, and removing
(implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.10')
the warning went away.
The 'stdlib' needs to match the 'stdlib-jdk' in the module file.
I faced the same issue while using Firebase with Kotlin.
I had to upgrade all the dependencies with their latest version available.
Note: have your kotlin-reflect and kotlin-stdlib versions same.
after many days i have solve the issue
Update the kotlin_version to '1.4.32'
In my case, I set the version number for all modules the same as gradle of app as latest version, and problem resolved.

Using android Parceler library with minifyEnabled

Whenever I try to minify my project that makes use of the parceler library, I cannot build a release apk because of a lot of warnings from proguard. For example:
Warning:org.parceler.transfuse.gen.FilerResourceWriter: can't find referenced class javax.tools.FileObject
I don't even make use of most of the libraries reported in this messages. What I'd like to know is if someone has encountered this problem and managed to solve it.
I tried to use -dontwarn to suppress all messages, but it does not seems correct, and besides it makes my app crash in rare cases (which makes me thing that some of the warning messages are indeed correct, but I'd like the library to keep the needed classes automatically).
My gradle script is as follows:
apply plugin: 'com.android.application'
...
dependencies {
...
compile 'org.parceler:parceler:1.0.3'
}
You are seeing this error from Proguard because you've included Parceler as a runtime dependency. Parceler was designed to be included into your project as two separate libraries; the annotation processor and the api. If you're running Gradle your build script should look like the following:
compile "org.parceler:parceler-api:1.0.3"
apt "org.parceler:parceler:1.0.3"
See Getting Parceler.
where apt is the android-apt plugin. Secondarily, it can also be run under the provided scope.
Your build script will look like the following in the end:
buildscript {
repositories {
mavenCentral()
}
dependencies {
// replace with the current version of the Android plugin
classpath 'com.android.tools.build:gradle:1.3.0'
// the latest version of the android-apt plugin
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.7'
}
}
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
...
dependencies {
...
compile "org.parceler:parceler-api:1.0.3"
apt "org.parceler:parceler:1.0.3"
}

gradle project sync failed android studio

I'm trying to setup nhaarman/ListViewAnimations inside of Android Studio 1.1.0.
From Setup:
Add the following to your build.gradle:
repositories {
mavenCentral()
}
dependencies {
compile 'com.nhaarman.listviewanimations:lib-core:3.1.0#aar'
compile 'com.nhaarman.listviewanimations:lib-manipulation:3.1.0#aar'
compile 'com.nhaarman.listviewanimations:lib-core-slh:3.1.0#aar'
}
yet Android Studio says:
Gradle project sync failed. Basic functionality (e.g. editing,
debugging) will not work properly.
Error:(26, 0) Gradle DSL method not found: 'compile()'
Possible causes:<ul><li>The project 'X' may be using a version of Gradle that does not contain the method.
Gradle settings</li><li>The build file may be missing a Gradle plugin.
Apply Gradle plugin</li>
You are adding these dependencies in the top level build.gradle file.
You have to add these lines in the module build.gradle file.
root
build.gradle //top level
app
build.gradle //module level
After adding those lines to:
build.gradle (Module: app)
instead of
build.gradle (Project: app)
the error is gone.
Try this:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.nhaarman.listviewanimations:lib-core:3.1.0'
compile 'com.nhaarman.listviewanimations:lib-manipulation:3.1.0'
}
Try removing #arr. worked for me.
//ListView Animations
compile 'com.nhaarman.listviewanimations:lib-core:3.1.0'
compile 'com.nhaarman.listviewanimations:lib-manipulation:3.1.0'

Categories

Resources