MissingPropertyException: Could not get unknown property 'test' for SourceSet container - android-gradle-plugin

I have a gradle task which contains the below code:
project.sourceSets.test.java.isEmpty()
when I execute the task, it throws
groovy.lang.MissingPropertyException: Could not get unknown property 'test' for SourceSet container of type org.gradle.api.internal.tasks.DefaultSourceSetContainer.
However, I did declare a empty test sourceSet in build.gradle
sourceSets {
test {}
}
Why is the property 'test' not found in the task?

Related

Kotest + Multiplatform Kotlin/JS project: Unable to initialize main class io.kotest.launcher.LauncherKt

Having troubles setting up Kotest in my multiplatform project (only using JS, multiplatform part is needed for some libraries).
Here's relevant parts of my Gradle file. If this is relevant, it's in one of the modules of a project, I have multiple projects that create a separate html and js files, and then collect them in one place with a separate task (using template from Kromex to write a browser extension)
js(IR) {
binaries.executable()
useCommonJs()
browser {
webpackTask {
outputFileName = "base.js"
sourceMaps = false
report = true
}
distribution {
directory = File("$projectDir/../build/distributions/")
}
}
}
sourceSets {
val jsMain by getting {
dependencies {
...
}
}
val jsTest by getting {
dependencies {
implementation("io.kotest:kotest-framework-engine:5.5.1")
implementation("io.kotest:kotest-assertions-core:5.5.1")
implementation("io.kotest:kotest-property:5.5.1")
}
}
}
I tried both this, and also with kotest-framework-engine in commonTest (as specified in quickstart guide) - result is the same.
At first, after trying to run it (with Intellij Plugin) it complains that there's no JDK specified. After I manually go to project structure and switch jsTest module SDK from Kotlin SDK that it uses by default to "Project SDK 15", it stops, but then a new problem happens:
Error: Unable to initialize main class io.kotest.launcher.LauncherKt
Caused by: java.lang.NoClassDefFoundError: io/kotest/core/engine/TestEngineListener
each time I try to launch a test. Also i've checked, io.kotest.core.engine.TestEngineListener exists and can be accessed, while io.kotest.launcher.LauncherKt does not
What I might be doing wrong?

android two buildTypes - error: duplicate class

Android Studio 3.4
I have 2 build types:
debug
release
so my project structure is:
src/debug/java/
src/main/java/
src/release/java/
I has CartActivity. This class has different implementation for release version and debug version.
So this class location is in TWO folders:
src/debug/java/activityCartActivity
src/main/java/activityCartActivity
But when I build project by gradlew assemble I get error:
> Task :scanlib:processDebugJavaRes NO-SOURCE
> Task :scanlib:transformClassesAndResourcesWithPrepareIntermediateJarsForDebug
> Task :app:javaPreCompileDebug FROM-CACHE
> Task :app:compileDebugJavaWithJavac FAILED
\app\src\debug\java\com\myproject\app\cart\CartActivity.java:66: error: duplicate class: com.myproject.app.cart.CartActivity
public class CartActivity extends AppCompatActivity {
You can't do it.
src/debug/java/activityCartActivity
src/main/java/activityCartActivity
Check the official doc:
All source code in the java/ directories are compiled together to generate a single output.
And in particular:
Note: For a given build variant, Gradle throws a build error if it encounters two or more source set directories that have defined the same Java class. For example, when building a debug APK, you cannot define both src/debug/Utility.java and src/main/Utility.java. This is because Gradle looks at both these directories during the build process and throws a "duplicate class" error. If you want different versions of Utility.java for different build types, you can have each build type define its own version of the file and not include it in the main/ source set.
Move CartActivity class from 'src/main/java/activityCartActivity' to src/release/java/activityCartActivity'.
"If above mentioned change does not work" modify java source path in app/build.gradle as
sourceSets {
main {
//java.srcDirs = ['src/main/java']
aidl.srcDirs = ['src/main/aidl']
renderscript.srcDirs = ['src/main/rs']
jni.srcDirs = []
jniLibs.srcDirs = []
res.srcDirs = ['src/main/res']
assets.srcDirs = []
}
test{
java.srcDirs = ['test']
}
debug {
java.srcDirs = ['src/debug/java']
}
release {
java.srcDirs = ['src/release/java']
}
}

How to add default `jvm` sourceSet to `android` target in Kotlin Multiplatform?

With Kotlin/Multiplatform 1.3.11 i had the following build.gradle:
fromPreset(presets.jvm, 'jvm')
fromPreset(presets.android, 'jvm') // reusing jvm sources for android platform impl
So basically i wanted to reuse all code for jvm target for android target.
With just-released Kotlin/Multiplatform 1.3.20 now i'm getting an error:
The target 'jvm' already exists, but it was not created with the 'android' preset. To configure it, access it by name in kotlin.targets or use the preset function 'jvm'
Open File
I've tried to migrate to new syntax:
jvm()
android() {
sourceSets.add(kotlin.targets.jvm.compilations.main.defaultSourceSet)
}
but it does not reuse jvm defaultSourceSet for Android target:
Expected class 'URL' has no actual declaration in module
so it does not use default jvm sourceset actually and not throwing Groovy syntax error.
What's wrong?
Actually, the best solution is to have some common sourceset and one for each platform:
commonJvmMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}
}
jvmMain {
dependsOn commonJvmMain
}
androidMain {
dependsOn commonJvmMain
}
Put shared code in commonJvmMain sourceset directory.
https://github.com/JetBrains/kotlin-native/issues/2577

gradle test raise error: java.lang.NoClassDefFoundError: Could not initialize class groovy.lang.GroovySystem

I try to run "gradle test", and get error
My test is
class HelperTest extends ro.gd.Test {
Plugin o;
void setUp() {
o = new Plugin();
}
void testGetIdeaDeps() {
def r = o.ideaDeps
asrHaveVal r
}
}
when i run gradle test, it raise:
junit.framework.AssertionFailedError: Exception in constructor: testGetIdeaDeps (java.lang.NoClassDefFoundError: Could not initialize class groovy.lang.GroovySystem
at org.codehaus.groovy.reflection.ClassInfo.isValidWeakMetaClass(ClassInfo.java:221)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClassForClass(ClassInfo.java:191)
at org.codehaus.groovy.reflection.ClassInfo.getMetaClass(ClassInfo.java:236)
at ro.gd.idea.HelperTest.$getStaticMetaClass(HelperTest.groovy)
at ro.Test.<init>(Test.groovy)
at ro.gd.Test.<init>(Test.groovy)
at ro.gd.idea.HelperTest.<init>(HelperTest.groovy)
...
Here is my full code
I fix this question. the reason is "groovy.lang.GroovyRuntimeException: Conflicting module versions", for detail, following is my build.gradle
compile 'org.codehaus.groovy:groovy-all:+'
compile gradleApi()
I guess gradleApi() will auto "compile localGroovy()", and this groovy version is 2.3.6, but latest version is 2.4.3
I find this error message in one test report
the solution is to specify groovy version like following
compile 'org.codehaus.groovy:groovy-all:2.3.6'
and you check your groovy version with 'gradle dependencies|grep groovy'

Unable to resolve class org.gradle.util.TestUtil in Gradle

This error means that class TestUtil is not on the classpath and the compiler can not find it. I came across such error hundred times before and there was missing Jar or wrong written class names, but now I just don't know what is wrong.
In my buildSrc dir I have custom Task and I made test for it:
package com.example.core.tasks;
import spock.lang.Specification
import org.gradle.api.Project
import org.gradle.util.TestUtil
public class GetInfoTaskTest extends Specification {
def "check files"(){
given:
def project = TestUtil.createRootProject()
when:
GetInfoTask getInfo = project.tasks.create("getInfo", GetInfoTask)
then:
getInfo instanceof GetInfoTask.class
}
}
In the build script in the buildSrc dir:
dependencies {
compile localGroovy()
testCompile 'org.spockframework:spock-core:0.7-groovy-1.8'
testCompile gradleApi()
}
Error:
GetInfoTaskTest.groovy: 4: unable to resolve class org.gradle.util.TestUtil
# line 3, column 1.
import org.gradle.util.TestUtil
^
I checked that this TestUtil is not internal. I still don't know why gradle can not find it.
TestUtil is an internal class. You can't use it in your own code.