Cannot access 'kotlinx.coroutines.CoroutineScope' which is a supertype of 'androidx.lifecycle.LifecycleCoroutineScope' - android-studio

Tried to use room api and had to deal with asynchronous tasks you know why.
I chose kotlin coroutines and androidx.lifecycle. Tried to write some code and got 2 errors displaying in IDE
Cannot access 'kotlinx.coroutines.CoroutineScope' which is a supertype of 'androidx.lifecycle.LifecycleCoroutineScope'. Check your module classpath for missing or conflicting dependencies
Cannot access class 'kotlinx.coroutines.Job'. Check your module classpath for missing or conflicting dependencies
error
Here is my gradle module
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
}
android {
compileSdk 31
defaultConfig {
applicationId "com.jacksafblaze.sqlitedemo"
minSdk 21
targetSdk 31
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildFeatures{
viewBinding true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "androidx.room:room-ktx:2.3.0"
implementation "androidx.room:room-runtime:2.3.0"
kapt "androidx.room:room-compiler:2.3.0"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0"
implementation'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0'
implementation'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.0'
implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:5'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
and gradle project
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
I tried to invalidate caches and restart, to recreate app from scratch, and to reinstall android studio and I am still getting this error.

upgraded 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0' to
'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.0-RC' and everything works fine

I have the same problem, if I manually add imports in the class the project will compile but the IDE will still display an error.
If you don't want to wait coroutines-android 1.6 release manually adding the import is a workaround, but you will have no autocompletion.
Android Studio build without autocompletion example
You could also have some problems with room, moshi and all libraries that use kapt, you can try to update to latest release (beta/RC) to see if the problem will be fixed in new releases.
For example, when I am writing this lines, moshi is not compatible with kotlin 1.6 yet, and a fix is planed to be released as soon as possible, see here for details.

Related

Build error when trying to Run basic activity

I'm on day 1 of learning Android Studio / Kotlin and working my way through the (official?) tutorials.
Having added some basic views to an empty layout and resolved a couple of "coding practice" warnings, when I try to run the app on a USB connected tablet, I get a build failed message.
(I haven't got as far as any Kotlin coding yet.)
I'm completely out of my depth at this stage and can't find a solution that isn't beyond my level of understanding.
The errors are ...
Caused by: java.lang.NoClassDefFoundError: kotlin/reflect/full/KClasses
Caused by: java.lang.ClassNotFoundException: kotlin.reflect.full.KClasses
If there's any other information worth sharing, I'm afraid I don't know what it is at the moment.
ANy suggestions on what I need to do to or investigate to resolve the build fail?
First build.gradle file:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.5.10"
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.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
}
}
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Second build.gradle file:
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.test04"
minSdkVersion 19
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Try these steps.
Update your Kotlin version from "1.5.10" to the latest "1.5.31" in your first build.gradle file. Also update the Kotlin plugin: File -> Settings -> Languages and Frameworks -> Kotlin and if there's a new version there will be an Install button to click. Then restart Android Studio and run your project to see if it will work.
If that doesn't work, you can manually add the missing reflection library to your project. I don't see in your dependencies anything that uses reflection, but maybe I'm missing something. So, add this line into the dependencies section of your second build.gradle file:
implementation "org.jetbrains.kotlin:kotlin-reflect"
Then click the "Sync Project with Gradle Files" button near the right end of the toolbar (elephant with blue arrow icon), and try running your project again.
The Android Build Tools you're using is also out of date. I'm not sure it could be related, but you can update that as well in the first build.gradle file. Change the 4.2.2 to 7.0.3 and click the Gradle sync button again. Android Studio might complain that you need to update your Gradle version, so click yes if it offers to update it for you.

Will apps developed using chaquopy create issues while deployment?

I would like to use chaquopy for the communication between java/kotlin and python code in android Studio. Below shown is my build gradle file:
plugins {
id 'com.android.application'
id 'com.chaquo.python'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.image_processing_using_chaquopy"
python{
pip{
install "numpy"
install "opencv-contrib-python-headless"
install "pillow"
}
}
sourceSets{
main{
python{
srcDirs = ["src/main/python"]
}
}
}
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
ndk {
abiFilters "armeabi-v7a", "x86"
}
python{
buildPython "C:/Users/hp/AppData/Local/Programs/Python/Python39/python.exe"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}
Here, we are giving the python.exe local path and chaquopy is not listed in the dependencies too. Will it cause any problem while deploying this app on android phones or some other external devices?
Here, we are giving the python.exe local path
This only affects the build process: this path is not used at runtime. In fact, it couldn't be used at runtime even if we wanted to, because an Android app has no direct access to the filesystem of your computer, even when you're using an emulator.
However, to make it easier for other developers to build your source code, it would be better to remove the buildPython line and allow Chaquopy to find Python on the PATH, as it says in the documentation. If you installed Python from python.org using the default settings, then the py launcher will already be on the PATH, so this will work automatically.
chaquopy is not listed in the dependencies too
That's no problem: the Chaquopy plugin at the top of the file will add any necessary dependencies.

Android Studio cannot sync and build NEW and OLD kotlin projects

When I am creating a new project in Android Studio, and load old projects I got this:
java.lang.NoClassDefFoundError: org/gradle/internal/impldep/com/google/common/collect/Lists
java.lang.ClassNotFoundException: org.gradle.internal.impldep.com.google.common.collect.Lists
ERROR: Unable to load class 'org.gradle.internal.impldep.com.google.common.collect.Lists'.
Possible causes for this unexpected error include:
Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt
Now I am totally stucked with that.
1, I downloaded studio again and installed to an other drive, the probleme still exists.
2, I deleted all of my Android Studios from my computer, I downloaded and installed again. Started a new kotlin project, the same error happened.
3, invalidate/restart not fixed
4, redownload dependencies not fixed
5, update gradle not fixed
6, flutter plugin disabled, not fixed
I was used my old Android Studio for flutter/dart and kotlin too. It looks like I can not build anymore native apps/kotlin.
my gradle wrapper
distributionUrl=https://services.gradle.org/distributions/gradle-5.6.4-all.zip
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.3.61'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.6.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
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "hu.lacas.myapplication"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
app build gradle
Any way to fix this?
Thanks
LOL. I found an old project what were built!!!
I copied this strings from that:
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.71"
}
and
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
and finally synced, built!!!
Something is very broken in the new Android Studio, because I got a lot of error/sync failed even if I create a NEW PROJECT

Getting Failed to resolve: com.github while adding my library from github using jitpack Error. How to resolve?

I build and added this library with demo on github. Now, I am trying to import it on other project.
Following are my gradle files that are not compiling.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
and
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.mayank.myapplication"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.github.mayank1513:SP_DrawerLayout:1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
I looked at following answers
Error:(23, 20) Failed to resolve: com.github.chrisbanes:PhotoView:2.1
Android Studio Failed to resolve:com.github.PhilJay:MPAndroidChart:v2.1.3
jitpack.io failed to resolve github repo
I am not able to resolve the issue still. Gradle Sync fails with error
Failed to resolve: com.github
Posting answer for anyone else facing same issue.
After a day or more now following import in graddle works which wasn't working earlier.
dependencies {
implementation 'com.github.mayank1513:SP_DrawerLayout:master-SNAPSHOT'
I guess that might be because jitpack may be taking some time to process the library. The dependency with version name still doesn't work.
I assume that version name is same as the version name you specify inside your library's build.graddle file.
Thanks to the jitpack team. The problem was that I needed to release the repository on github by following a few more steps.
go to release tab
Create new release

appcompat-v7:28.0.0-beta01 Broke after update

I am beginner in Android studio and installed it yesterday. Then I created a empty project that worked just fine. Today I had to update and now I am fighting with that error message:
As you can see I already included the Examples. Any ideas how I can fix that?
Here the full app.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "at.sumser.fateandlove"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0-beta01'
implementation 'com.android.support:support-media-compat:26.1.0'
implementation 'com.android.support:animated-vector-drawable:28.0.0-beta01'
implementation 'com.android.support.constraint:constraint-layout:1.1.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation'com.google.android.gms:play-services-location:15.0.1'
}
Funny, that it tells me to add 'com.android.support:support-media-compat:26.1.0' when the SDK is 28? Any ideas?
UPDATE
Here is also the build.gradle:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = '1.2.41'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
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
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Now the 'com.android.support:support-media-compat:26.1.0' does also tell me about a problem:
Question: Does Google not test their releases? Or is the autoresolver in Android Studio just broken? I lost now nearly 3 hours to find a fix for this issue...
I just had the same issue when updating to AppCompat version 28.
First I recommend to use the latest version which is 28.0.0-rc01 instead of 28.0.0-beta01.
If you then still get the warning that all libraries must use the exact same version specification, you can force to use the latest version for the corresponding library.
You can do so by adding a resolution strategy to your apps build.gradle file within android { } like this:
configurations.all {
resolutionStrategy {
force 'com.android.support:support-v4:28.0.0-rc01'
force 'com.android.support:support-media-compat:28.0.0-rc01'
force 'com.android.support:customtabs:28.0.0-rc01'
}
}

Resources