Since I converted my Groovy to Kotlin DSL gradle, my unittests are not working anymore. I get the error:
java.lang.RuntimeException: Method get in org.json.JSONObject not mocked. See http://g.co/androidstudio/not-mocked for details.
So i followed the link and added testoptions to all my build.gradle.kts files.
But after this it still doesn't work.
My (builsSrc) build.gradle.kts file:
plugins {
`kotlin-dsl`
}
repositories {
google()
jcenter()
}
My (App) build.gradle.kts file:
plugins {
id("com.android.library")
kotlin("android")
kotlin("android.extensions")
}
android {
compileSdkVersion(Versions.Android.compileSdkVersion)
defaultConfig {
versionCode = Versions.Android.appVersionCode
versionName = Versions.Android.appVersionName
minSdkVersion(Versions.Android.minSdkVersion)
targetSdkVersion(Versions.Android.targetSdkVersion)
testInstrumentationRunner = Config.Test.instrumentationRunner
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
}
testOptions {
unitTests.setReturnDefaultValues(true)
}
}
dependencies {
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar", "*.aar"))))
implementation(Depends.Kotlin.reflect)
implementation(Depends.Kotlin.kotlinStdLib)
testImplementation(Depends.TestLibraries.json)
testImplementation(Depends.TestLibraries.jUnit)
androidTestImplementation(Depends.TestLibraries.jUnitRunner)
androidTestImplementation(Depends.TestLibraries.espressoCore)
}
It doens't look for me that I'm missing something.
Does anyone have an id on how to fix this?
The following works:
testOptions {
unitTests.apply {
isReturnDefaultValues = true
}
}
Note that you need to do a gradle sync to pick up the configuration
I finally managed to find a solution using:
apply(from = "../testOptions.gradle")
which contains:
android {
testOptions {
unitTests.returnDefaultValues = true
}
}
Related
I'm doing a project with Jetpack Compose Multiplatform Desktop.
I'm not very familiar with Gradle, and I would like to use Material 3 in my project.
So, I added this line in build.gradle.kts file:
implementation("androidx.compose.material3:material3:1.0.0-alpha14")
But when I try to import the lib in a Kotlin file with:
import androidx.compose.material3.*
I've got an unresolved reference issue.
This is my build.gradle.kts file:
import org.jetbrains.compose.desktop.application.dsl.TargetFormat
plugins {
kotlin("multiplatform")
id("org.jetbrains.compose")
}
group = "com.example"
version = "1.0-SNAPSHOT"
repositories {
google()
mavenCentral()
maven("https://maven.pkg.jetbrains.space/public/p/compose/dev")
}
kotlin {
jvm {
compilations.all {
kotlinOptions {
jvmTarget = "18"
freeCompilerArgs = listOf("-opt-in=kotlin.RequiresOptIn")
}
}
withJava()
}
sourceSets {
val jvmMain by getting {
dependencies {
implementation(compose.desktop.currentOs)
implementation("androidx.compose.material3:material3:1.0.0-alpha14")
}
}
val jvmTest by getting
}
}
compose.desktop {
application {
mainClass = "MainKt"
jvmArgs += listOf("-Djava.library.path=./lib")
nativeDistributions {
targetFormats(TargetFormat.Dmg, TargetFormat.Msi, TargetFormat.Deb)
packageName = "FanControl"
packageVersion = "1.0.0"
}
}
}
I think I should be able to use this lib since the release note indicate it support it.
Edit1:
I have this message from Gradle when I try to sync:
Could not resolve: androidx.compose.material3:material3:1.0.0-alpha14
Finally found what was the problem, it was the wrong library,
change
implementation("androidx.compose.material3:material3:1.0.0-alpha14")
with
implementation("org.jetbrains.compose.material3:material3-desktop:1.2.1")
I'm using gradle 7-4-1 and I'm trying to use a catalog to share dependencies between subprojects as documentation
https://docs.gradle.org/current/userguide/platforms.html#sec:sharing-catalogs
This is my settings.gradle.kts
dependencyResolutionManagement {
versionCatalogs {
create("libs") {
version("log4j", "2.17.1")
library("log4j-api", "org.apache.logging.log4j", "log4j-api").versionRef("log4j")
library("log4j-core", "org.apache.logging.log4j", "log4j-core").versionRef("log4j")
library("log4j-slf4j-impl", "org.apache.logging.log4j", "log4j-slf4j-impl").versionRef("log4j")
bundle("log4j", listOf("log4j-api", "log4j-core", "log4j-slf4j-impl"))
}
}
}
rootProject.name = "gawds-db"
include("db-server", "db-client", "db-common")
enableFeaturePreview("VERSION_CATALOGS")
And I'm trying to import into my build.gradle.kts (located in my subproject)
plugins {
`maven-publish`
application
`java-library`
}
java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}
repositories {
mavenCentral()
}
configure<JavaApplication> {
mainClass.set("com.gawds.db.MainApp")
}
tasks.compileJava {
options.isIncremental = true
options.isFork = true
options.isFailOnError = false
}
tasks.named<Test>("test") {
useJUnitPlatform()
}
dependencies {
implementation(libs.log4j.api)
implementation(libs.log4j.core)
implementation(libs.log4j.log4j.slf4j.impl)
}
Note: also tried
implementation(libs.bundles.log4j)
But neither referencing libs.alias nor libs.bundles.alias-bundle work.
My project structure is:
/
| settings.gradle.kts
| db-server /
| build.gradle.kts
Just in case helps someone else. First, not sure why intellij marks as red, however from cmd line looks like it works.
From settings.gradle.kts get rid of
enableFeaturePreview("VERSION_CATALOGS")
This was required in previous versions but not in gradle 7.4.x
Also u need to add the repos in the catalog section, see below:
dependencyResolutionManagement {
repositories {
mavenCentral()
}
versionCatalogs {
create("libs") {
version("log4jVersion", "2.17.1")
library("log4j-api", "org.apache.logging.log4j", "log4j-api").versionRef("log4jVersion")
library("log4j-core", "org.apache.logging.log4j", "log4j-core").versionRef("log4jVersion")
library("log4j-slf4j-impl", "org.apache.logging.log4j", "log4j-slf4j-impl").versionRef("log4jVersion")
bundle("log4j", listOf("log4j-api", "log4j-core", "log4j-slf4j-impl"))
}
}
}
rootProject.name = "gawds-db"
include("db-server", "db-client", "db-common")
Another important point that I did wrong in my tests are:
Assuming this alias:
log4j-slf4j-impl
the typesafe equivalent is
log4j.slf4j.impl
so we need to call in the build.gradle.kts:
implementation(libs.log4j.slf4j.impl)
Also u can print the alias to check the name using:
val versionCatalog = extensions.getByType<VersionCatalogsExtension>().named("libs")
println("${versionCatalog.libraryAliases}")
To improve the previous code, we can create a task to print the catalog alias:
abstract class PrintCatalogAlias #Inject constructor(private val r : Project) : DefaultTask() {
#org.gradle.api.tasks.TaskAction
fun catalogAlias() {
val versionCatalog = r.rootProject.project.extensions.getByType<VersionCatalogsExtension>().named("libs")
println("${versionCatalog.libraryAliases}")
}
}
tasks.register<PrintCatalogAlias>("catalog")
I have the following build.gradle.kts
plugins {
id("com.mkobit.jenkins.pipelines.shared-library") version "0.10.1"
id("com.github.ben-manes.versions") version "0.21.0"
java
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
tasks.withType<Test> {
this.testLogging {
this.showStandardStreams = true
}
}
val log4jVersion = "2.11.2"
val slf4jVersion = "1.7.26"
val declarativePluginsVersion = "1.3.9"
dependencies {
// logging stuffs
testImplementation("org.slf4j:slf4j-api:$slf4jVersion")
testImplementation("org.apache.logging.log4j:log4j-api:$log4jVersion")
testImplementation("org.apache.logging.log4j:log4j-core:$log4jVersion")
testImplementation("org.apache.logging.log4j:log4j-slf4j-impl:$log4jVersion")
testImplementation("org.apache.logging.log4j:log4j-jul:$log4jVersion")
// general testing stuff
testImplementation("org.assertj:assertj-core:3.12.2")
testImplementation("com.lesfurets:jenkins-pipeline-unit:1.3")
testImplementation("junit:junit:4.12") // TODO: update to 5
// jenkins specific deps
testImplementation("org.jenkins-ci.plugins:pipeline-build-step:2.9")
testImplementation("org.jenkinsci.plugins:pipeline-model-api:$declarativePluginsVersion")
testImplementation("org.jenkinsci.plugins:pipeline-model-declarative-agent:1.1.1")
testImplementation("org.jenkinsci.plugins:pipeline-model-definition:$declarativePluginsVersion")
testImplementation("org.jenkinsci.plugins:pipeline-model-extensions:$declarativePluginsVersion")
testImplementation("org.jenkins-ci.plugins.workflow:workflow-cps-global-lib:2.8")
testImplementation("org.jenkins-ci.plugins.workflow:workflow-step-api:2.18")
}
jenkinsIntegration {
baseUrl.set(uri("http://localhost:5050").toURL())
authentication.set(providers.provider { com.mkobit.jenkins.pipelines.http.AnonymousAuthentication })
downloadDirectory.set(layout.projectDirectory.dir("jenkinsResources"))
}
sharedLibrary {
coreVersion.set(jenkinsIntegration.downloadDirectory.file("core-version.txt").map { it.asFile.readText().trim() })
pluginDependencies {
dependency("org.jenkins-ci.plugins", "pipeline-build-step", "2.9")
dependency("org.jenkinsci.plugins", "pipeline-model-api", declarativePluginsVersion)
dependency("org.jenkinsci.plugins", "pipeline-model-declarative-agent", "1.1.1")
dependency("org.jenkinsci.plugins", "pipeline-model-definition", declarativePluginsVersion)
dependency("org.jenkinsci.plugins", "pipeline-model-extensions", declarativePluginsVersion)
dependency("org.jenkins-ci.plugins.workflow", "workflow-step-api", "2.18")
}
}
when I do gradlew build
I got the following error on one of my groovy file:
unable to resolve class org.jenkinsci.plugins.workflow.support.steps.build.DownstreamFailureCause
To me the dependency should be solved in the pluginDependencies section.
I found a solution.
It seems that the class I was having a problem with was introduced in version 2.10
So you need to change
org.jenkins-ci.plugins:pipeline-build-step:2.9
to
org.jenkins-ci.plugins:pipeline-build-step:2.10
I want to use C code in my Kotlin/Multiplatform project (common level).
By this article:
https://theprogrammershangout.com/resources/kotlin/native/creating-c-bindings.md/
I got my .klib file and now I want to add it to my IntelliJ Kotlin/Multiplatform project.
I'm trying to add this to Gradle without success:
implementation files("/path/to/App.klib")
The project is built successfully but the importing App is not resolved.
What am I missing?
I already searched all over the internet and did not found anything helpful.
My build.gradle:
buildscript {
repositories {
google()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
}
}
plugins {
id 'org.jetbrains.kotlin.multiplatform' version '1.4.10'
id 'maven-publish'
}
apply plugin: 'com.android.library'
repositories {
mavenCentral()
google()
jcenter()
}
group 'com.texel.examples.kotlinnative'
version '0.0.1'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName '1.0'
testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
buildTypes {
release {
minifyEnabled false
}
}
sourceSets {
main {
manifest.srcFile 'src/jvmMain/AndroidManifest.xml'
java.srcDirs = ['src/jvmMain/kotlin']
res.srcDirs = ['src/jvmMain/resources']
}
}
}
dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.2'
}
kotlin {
// This is for iPhone emulator
// Switch here to iosArm64 (or iosArm32) to build library for iPhone device
// iosX64("ios") {
// binaries {
// framework()
// }
// }
macosX64("maccos") {
binaries {
framework()
}
}
android("android") {
// you can also publish both "release" and "debug"
publishLibraryVariants("release")
}
sourceSets {
commonMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common:1.4.10'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9-native-mt"
implementation files("/Users/user/Downloads/App.klib")
}
}
jvmMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
}
}
jsMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-js'
}
}
macosMain {
dependencies {
}
}
iosMain {
dependencies {
}
}
}
}
configurations {
compileClasspath
}
Many thanks.
Here is the official documentation: C Interop and also an Using C Interop and libcurl for an App
(you may prefer to look at the https://github.com/JetBrains/kotlin-native/tree/master/samples/libcurl instead)
As I see the gradle setup is pretty different:
hostTarget.apply {
compilations["main"].cinterops {
val libcurl by creating {
when (preset) {
presets["macosX64"] -> includeDirs.headerFilterOnly("/opt/local/include", "/usr/local/include")
presets["linuxX64"] -> includeDirs.headerFilterOnly("/usr/include", "/usr/include/x86_64-linux-gnu")
presets["mingwX64"] -> includeDirs.headerFilterOnly(mingwPath.resolve("include"))
}
}
}
I've created a new Kotlin Multiplatform project for Andrid and iOS (Mobile shared Library). The project works fine but every time I run Gradle sync, it takes more than 5 minutes every time. It always get stuck on the same line:
Gradle: Build model 'org.jetbrains.kotlin.gradle.KotlinMPPGradleModel' for root project 'MyProject'
Why is it taking so long evey time?
I'm using Gradle version 5.1.
Here is my build.gradle file:
buildscript {
ext.kotlin_version = '1.3.11'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.0'
classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4.8.1"
}
}
plugins {
id 'kotlin-multiplatform' version '1.3.11'
}
repositories {
google()
jcenter()
mavenCentral()
maven { url 'https://jitpack.io' }
}
repositories {
mavenCentral()
}
group 'com.example'
version '0.0.1'
apply plugin: "com.android.library"
apply plugin: "com.jfrog.artifactory"
apply plugin: 'maven-publish'
android {
compileSdkVersion 28
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName version
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
compileOptions.incremental = false
}
kotlin {
targets {
fromPreset(presets.android, 'android')
// This preset is for iPhone emulator
// Switch here to presets.iosArm64 to build library for iPhone device
fromPreset(presets.iosX64, 'ios') {
compilations.main.outputKinds('FRAMEWORK')
}
}
sourceSets {
commonMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-common'
}
}
commonTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test-common'
implementation 'org.jetbrains.kotlin:kotlin-test-annotations-common'
}
}
androidMain {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib'
}
}
androidTest {
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-test'
implementation 'org.jetbrains.kotlin:kotlin-test-junit'
}
}
iosMain {
}
iosTest {
}
}
}
configurations {
compileClasspath
}
Screenshot of the problem:
There's a known issue where Kotlin/Native dependencies are re-fetched every sync, which may be what you're seeing. You can see details and follow it here.
As posted to that issue, there's a workaround you may be able to try which essentially involves adding { content { excludeGroup("Kotlin/Native" } } to every item in your repositories block.
As #Brucelet pointed out this is a known issue. To supplement his answer, here is a full implementation of the workaround in Groovy:
repositories {
mavenCentral().content() {
excludeGroup "Kotlin/Native"
}
google().content() {
excludeGroup "Kotlin/Native"
}
jcenter() {
content {
excludeGroup("Kotlin/Native")
}
}
maven {
url 'https://jitpack.io'
content {
excludeGroup("Kotlin/Native")
}
}
}
and in Kotlin DSL:
repositories {
mavenLocal().apply {
content {
excludeGroup("Kotlin/Native")
}
}
maven {
url = uri("https://dl.bintray.com/soywiz/soywiz")
content {
includeGroup("com.soywiz")
excludeGroup("Kotlin/Native")
}
}
jcenter() {
content {
excludeGroup("Kotlin/Native")
}
}
google().apply {
content {
excludeGroup("Kotlin/Native")
}
}
}
Try to run the Gradle task from command lines with --parallel option.
Otherwise, fllow this guide to profile Gradle execution https://guides.gradle.org/performance/