Unable to import coroutines in Kotlin - kotlin

When I am using following in one of the .kt files,
import kotlinx.coroutines.*
I am getting compilation error saying "Unresolved reference: coroutines".
Context: I am building Android App using Kotlin in Android Studio.
What could I be doing wrong?

Make sure you are importing the library in your build.gradle file:
dependencies {
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2'
// Other dependencies here...
}

Related

Unable to import Gson in gradle file

I'm trying without success to import this import com.google.gson.Gson in one gradle file I've created just to manage some tasks. I'm able to import this Gson in other parts of the app, it is only here where I'm having issues.
In my gradle.app I have
I also tried an answer from Importing GSON using gradle, but when I open the menu nothing appears.
What could I be doing wrong?

Cocoapod dependency in iosMain of a Kotlin Multiplatform project, cocoapod unresolved reference

I am trying to create a wrapper around AWS Amplify to use for my project. I have a module inside my shared ( common ) module called Amplify. Here I integrated cocoapods as instructed in the official docs. But when I try to import anything from the iosMain I keep getting Unresolved reference: cocoapods.
My project structure is as follows
Common module (shared)
|- root module
|- other features
|- amplify wrapper module
In the root module I have the
kotlin {
ios {
binaries {
framework {
baseName = "Framework"
linkerOpts.add("-lsqlite3")
export(project(":common:main"))
}
}
}
And the setup for the cocoapods in the amplify module
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
plugins {
id("multiplatform-setup") // this is from buildSrc, it adds the multiplatform plugin
id("android-setup")
id("org.jetbrains.kotlin.native.cocoapods")
kotlin("plugin.serialization") version "1.5.10"
}
version = "1.0"
kotlin {
sourceSets {
named("commonMain") {
dependencies {
}
}
}
cocoapods {
summary = "Amplify wrapper for KMP project"
homepage = "Link to a Kotlin/Native module homepage"
frameworkName = "AmplifyKMP"
pod("Amplify")
pod("AmplifyPlugins/AWSCognitoAuthPlugin")
pod("AmplifyPlugins/AWSPinpointAnalyticsPlugin")
}
}
My idea is that I'd be able to expose my wrapper from the commonMain code, that would call actual implementations from the Amplify Android and Amplify IOS libraries. My first problem is the cocoapods being unresolved and secondly all of the examples from the official docs and github all have cocoapods in the main module (root module in my case) from where they export the framework, I am not sure my approach is even feasible.
The first problem is easy. You need to add kotlin("native.cocoapods") to your plugins section.
On the second, can a sub-module import pods with cinterop and make them available to a module that depends on them? I haven't tried it. In theory the cocoapods plugin should be able to just import pods definitions to kotlin. However, the cocoapods Kotlin gradle plugin (aka kotlin("native.cocoapods")) will configure ios targets to create a framework. That might create issues with the dependency config.
In the amplify module I don't see that you're defining any iOS targets, so you'll probably need to do that, but that config will be altered by kotlin("native.cocoapods"). You'll likely need to step in and modify that yourself. You can step in and do that in gradle, but I would prepare to spend some time tweaking that.
https://github.com/touchlab/KaMPKit/blob/main/shared/build.gradle.kts#L114

Kotlin - Import Jar and Use Class

I have created a library in kotlin using gradle init and following the prompts. It compiles and produces a jar file in lib/build/libs. I then have another project that needs to access functions in the library. through intelliJ, I add the dependency by going to Project Settings > Modules > Dependencies and adding the jar file. I then attempt to import the package defined in the library with import DemoLib where DemoLib is the name of the package (and the name of the library). This does not compile as it does not recognize the package name. I have also tried importing as a library rather than a jar, with the same results. How can I achieve the desired result?
EDIT: In case it helps here is the code:
Library:
package DemoLib
class Library {
fun someLibraryMethod(): Boolean {
return true
}
}
Client Code:
package DemoClientAppOne
import DemoLib.*
class App {
val greeting: String
get() {
return "Hello World!"
}
}
fun main() {
println(App().greeting)
}
Not terribly interesting, but the point is that DemoLib is an unresolved reference even after adding the jar as a dependency
You have three options
Combine two projects under same IDE project and use Gradle dependencies on project:
settings.gradle:
include 'project-lib'
build.gradle:
implementation project(':project-lib')
See https://docs.gradle.org/current/userguide/declaring_dependencies.html#sub:project_dependencies
Install the library into local repository and add it as a Gradle dependency:
apply plugin: 'maven'
then run gradle publishToMavenLocal
and add mavenLocal() repository in gradle.build file.
See How to install a compiled by Gradle jar into the local Gradle Repository files-2.1 instead of the Maven repository?
Use this jar as a flat library Gradle dependency (assuming this jar is located in the libs directory:
repositories {
flatDir {
dirs 'libs'
}
}
dependencies {
implementation name: 'lib-jar-name'
}
If DemoLib is a package,
you need to
import DemoLib.*
to import all its names into scope. Just import DemoLib wouldn't do anything. Or better,
import DemoLib.SomeClass
to import only one specific name. Actually, if you start typing SomeClass (assuming that's a name in your DemoLib package), IDEA should suggest adding the import.
Also, it's better to follow naming conventions from the beginning, and DemoLib is not a good package name:
Names of packages are always lower case and do not use underscores (org.example.project). Using multi-word names is generally discouraged, but if you do need to use multiple words, you can either simply concatenate them together or use camel case (org.example.myProject).

react native - TypeError: undefined is not a function

I'm exporting an external module of this library: https://github.com/GoIntegro/react-native-account-manager
Since the library is outdated and doesn't work beyond for > 0.47, I had to include this library as an external module.
Everything was working fine, but now suddenly upon calling react-native upgrade, I'm getting this error:
My settings.gradle contains this:
include ':react-native-account-manager'
project(':react-native-account-manager').projectDir = new File(rootProject.projectDir, '../external_modules/react-native-account-manager/android')
And this is in my app level build.gradle file:
dependencies {
...
compile project(':react-native-account-manager')
...
}
Anyone has any idea how am I supposed to fix this? Is there something wrong with the way I'm importing this library or is it something within the library that is outdated that I need to fix?

intelliJ fails to resolve 0.13.9 compiler interface importing sbt project

Importing a project into intelliJ, I get:
SBT project import
[warn] [FAILED ] org.scala-sbt#compiler-interface;0.13.9!compiler-interface.jar: (0ms)
Here's the import options I selected:
After the import everything works though.
I don't get this error in sbt when I clone the same project. What might it be? how do I avoid it?