Android App Development : Errors Encountered

Compilation Error

02-03-2023

  1. ERROR: JAVA_HOME is not set and no 'java' command could be found....

Install the latest Java for windows 64 from oracle link, which ill install it inC:\Program files (x86) folder.

The installer from Java site , is installing it in Program files (x86)

If it shows error download the Java15 JDK from archive and install it.
solution reference

set the environment variable JAVA_HOME as C:\Program Files\Java\<jdkversion>

  1. Kotlin Error 1
    AAPT: error: resource android:attr/lStar not found stackoverflow

Explanation:
This happens when apply plugin: 'kotlin-android' is used in (in app/build.gradle).
dependencies{ will have implementation "androidx.core:core-ktx:+"
The issue was that one of the external libraries the app depends on has a dependency on androidx.core:core-ktx:+ (in app/build.gradle) which meant that was always compiling with the latest version. My app is still working on SDK 28, but the latest version of androidx.core:core-ktx has the minimal SDK of 31, which resulted in this conflict.

Solution: 3 Options
I chose addin in app/build.gradle

configurations.all {
    resolutionStrategy {
        force 'androidx.core:core-ktx:1.6.0'
    }
}
  1. Kotlin error 2
    Class 'kotlin.Unit' was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.16.

In your project level open build.gradle file, increase the ext.kotlin_version from whatever current version that you have like 1.5.0, to the latest stable version 1.6.0 (Whatever is latest at that time). Thanks
You can get latest version from here:
https://kotlinlang.org/docs/releases.html#release-details
Latest as on 02-03-2023 is 1.8.10 but since i used force 'androidx.core:core-ktx:1.6.0' in app/build.gradle, limit to 1.6.0

  1. Kotlin error 3
    Unable to load class 'org.jetbrains.kotlin.config.LanguageVersion'

clear gradle cache

./gradlew clean

Then Run [File - from android studio menu]

File-> Invalidate caches and restart

  1. Error running 'app'. Cannot find runner for app
    Gradle Sync issue
    The possible reason was one of the following:

    • Gradle sync was in progress
    • Gradle sync had failed

You could also just try to sync the IDE with Gradle with either of these buttons:

  1. The application could not be installed: INSTALL_FAILED_USER_RESTRICTED

Solution

Mainly

  • Turn On "USB Debugging"
  • Turn On "Install via USB"
  1. java.lang.NoClassDefFoundError: Could not initialize class org.codehaus.groovy.vmplugin.v7.Java7

during ./gradlew signingReport

Solution

Your system gradle version is newer than the one it was previously developed. upgrade the graradel for the project.

  • Edit project.gradle
    change classpath (my previous was 4.0.1)

    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2'
  • run ./gradlew sync

Different JDKs

Multiple Gradle daemons might be spawned because the Gradle JDK and JAVA_HOME locations are different. Project 'YOUR PROJECT NAME' is using the following JDK location when running Gradle: 'C:/Program Files/Android/Android Studio/jbr' The system environment variable JAVA_HOME is: 'C:\Program Files\Java\jdk-19' If you dont need to use different paths (or if JAVA_HOME is undefined), youcan avoid spawning multiple daemons by setting JAVA_HOME and the JDK locationto the same path.

Solution

A copy of the latest OpenJDK comes bundled with current versions Android Studio, and this is the JDK version we recommend you use for your Android projects.

java.lang.IllegalStateException: Method setCurrentState must be called on the main thread

Solution : run in runOnUiThread

eg:

@Override public void onProgress(final int step) {
 ((Activity) mContext).runOnUiThread(new Runnable() {
  @Override public void run() {
   profileProgressbar.setProgress(step);
  }
 });
}

OR in Lambda()

(Activity)currentActivity.runOnUiThread(() -> profileProgressbar.setProgress(step));

[references]()

Failed to notify a WebView

From Android 9 onwards, loading http is considered unsafe. Set extraconfig to overcome this.Try this . Official Google Doc