Kotlin class NoClassDefFoundError crash - android

I have an existing Android Project that uses the following libs:
AutoValue
Dagger2
RxJava
Retrolambda
I am trying to add Kotlin Support so that I can slowly migrate the project over to Kotlin.
Here is what I have done.
Added Kotlin dependency.
Converted one of the classes over to Kt class and moved over to src/main/kotlin/..package..
Added kotlin in source set.
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
When I do a clean build and deploy the app, I get NoClassDefFoundError exception. However, If I deploy again it works just fine.
Any suggestions? I do not have any annotation in the class I converted so I did not apply kapt plugin.
Note: I am using latest kotlin 1.0.4. Also I have instant run disabled.

Go to Files > Settings and turn off completely Instant Run, I mean all checkboxes should be unchecked.
Clean and rebuild project.
Then it should work
Edit: As you said that Instant is disabled - using protip check your configuration and update Gradle and Android Studio if you're not using the latest.
According to this issue, changing Gradle plugin version from 2.10 to 2.14.1 may help.
Protip:
Use combination Ctrl+Shift+A to find commands like
Configure Kotlin in Project
Configure Kotlin Updates
Convert Java File to Kotlin

first you need to configure you android studio with kotlin , for this need to add kotlin plugin in android studio, and then need to add dependencies and then convert java classes to kotlin please have look this video.
https://www.youtube.com/watch?v=DN2EDdycZ3c

Suffix the Kotlin classes with kt when calling them from Java.
Otherwise add #file:JvmName("Utils") before the package in the Kotlin class if you must call it in java with whatever custom name you wish like Utils in this case.
more details can be found here

Related

How to access LibraryExtension in custom gradle plugin

I would like to write a custom gradle plugin that manipulates either com.android.build.gradle.AppExtension or com.android.build.gradle.LibraryExtension. The basic form of the plugin is:
class AndroidLibrary : Plugin<Project> {
override fun apply(project: Project) {
var lib: LibraryExtension = project.extensions.getByName("android") as LibraryExtension
lib.minSdkVersion = "26"
}
}
The problem is that the class LibraryExtension cannot be resolved. That class is contained in "com.android.tools.build:gradle:7.1.2". Note, there is no problem accessing either of these classes in a gradle.build.kts script file. I just can't access either of those classes from within a custom plugin like seen above. I've tried adding a dependency to com.android.tools.build.gradle 7.1.2 in the build script, but that doesn't work. I also tried adding it as a buildscript dependency, and that didn't help either. I also got the
com.android.tools.build:gradle jar file and added it as a dependency, but that too didn't work.
It seems to me to be reasonable to expect a custom plugin of being able to manipulate the android build settings but I just can't find a way to resolve either LibraryExtension or AppExtension from within the custom plugin project.
If someone knows what I need to do to resolve those two classes, that would be greatly appreciated. I'm currently under the impression, that this is an architectural limitation of with android's gradle plugin and that I really can't access the "android" build section using LibraryExtension or AppExtension from a custom plugin and dsl. If that is the case, that too would be useful to know.
check this answer for your question, it is gonna solve your problem.
Initially I was building the plugin using IDEA. I could build the project by using a "gradlew build" command. I noticed that I would get the following message whenever I tried to sync the project:
This version of the Android Support plugin for IntelliJ IDEA (or Android Studio) cannot open this project, please retry with version 2020.1.1 or newer
I had been ignoring that issue since I could successfully build the project using gradle commands. Here's a link the explains what the message means: https://intellij-support.jetbrains.com/hc/en-us/community/posts/4405168877202-This-version-of-the-Android-Support-plugin-for-IntelliJ-IDEA-or-Android-Studio-cannot-open-this-project-please-retry-with-version-2020-3-1-or-newer-
One of the suggestions to this problem was to build the project in Android Studio instead of waiting for IDEA to incorporate later android build. I did that and that indeed fixed that issue. The additional side effect was that I could now build the plugin. I was able to add dependencies for both gradleApi and to com.android.build.gradle and was able to resolve AppExtension of LibraryExtension. I had added the same dependencies I when I was using IDEA IDE. I had been customarily building my custom gradle plugin using IDEA. This was my first custom gradle plugin that I was going to manipulate an "android" build configuration.

How to get rid of Kotlin DSL warnings

I have just started learning KotlinDSL recently.
And in Android added buildSrc. In this folder I have module plugins: AppModulePlugin, CommonModulePlugin, FeatureModulePlugin. All of this compiles and the application installs correctly, everything is fine. but in these files a warning is displayed:
Cannot access 'com.android.build.gradle.internal.dsl.Lockable'
which is a supertype of 'com.android.build.gradle.BaseExtension'.
Check your module classpath for missing or conflicting dependencies
Please tell me how to get rid of these warnings?
IntelliJ has had this issue for months unfortunately - it reports correct code as invalid. See KTIJ-3769. I believe it's dependent on ticket KTIJ-19669.
As a workaround you can either replace the helper methods that the plugins introduce with 'plain' Gradle Kotlin
// androidExtension { }
project.extensions.configure<BaseExtension> {
}
Or only create buildSrc plugins in Kotlin (*.kt files), not Kotlin Script (*.kts files). This requires using the java-gradle-plugin and defining the plugins in buildSrc/build.gradle.kts.

Getting weird bugs when trying to update to Kotlin 1.4.0. How to make it work with Gradle and IntelliJ IDEA 2020.2.1?

Kotlin 1.4.0 is stable now. Therefor, I wanted to update my multi module Android project to use it. I set IDEA to use Kotlin plugin 1.4.0-release-IJ2020.2-1 and in my buildSrc build.gradle.kts using Kotlin DSL, I'm loading Kotlin for the jvm like this:
plugins {
kotlin("jvm") version "1.4.0"
}
My app level plugins block looks like this
plugins {
id("com.android.application")
id("com.google.gms.google-services")
kotlin("android")
kotlin("kapt")
id("kotlin-android-extensions")
id("androidx.navigation.safeargs.kotlin")
}
I have also added the Kotlin stdlib to my app level build.gradle.kts dependencies
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.0")
When trying to build my project now, I get multiple Errors like the following:
'let((T) -> R): R' is only available since Kotlin 1.3.50 and cannot be used in Kotlin 1.3
I don't understand. How is gradle trying to use Kotlin 1.3 here? Any idea on how to fix this? Its working fine when using Kotlin v1.3.72 instead.
What I tried so far:
Clean Project
Invalidate caches and restart
Delete .gradle folder and restart
Fix broken classes paths
UPDATE
Forgot to mention that I'm also getting the following warning. How is it unsupported when it is stable?
> Configure project :buildSrc
WARNING: Unsupported Kotlin plugin version.
The `embedded-kotlin` and `kotlin-dsl` plugins rely on features of Kotlin `1.3.72` that might work differently than in the requested version `1.4.0`.
Maybe you should add the Kotlin standard lib to your dependencies explicitly?
dependencies {
implementation(kotlin("stdlib"))
}
With Kotlin 1.4 it is no longer required it add this dependency. The plugin applies the Stdlib by default to the projects it is applied to.
From the Kotlin 1.4 release notes:
You no longer need to declare a dependency on the stdlib library in any Kotlin Gradle project, including a multiplatform one. The dependency is added by default.
The automatically added standard library will be the same version of the Kotlin Gradle plugin, since they have the same versioning.

Gradle Kotlin DSL: Inspection "Newer Library Versions Available" not working

I have converted my Gradle build scripts to the Kotlin DSL. Since I was starting with a small new project, everything went according to plan. When referencing more and more dependencies I wanted to put their version numbers in the script as constants, especially for those versions which are used in several places.
In my app/build.gradle.kts I have basically the following:
dependencies {
implementation("androidx.appcompat:appcompat:1.0.0")
...
}
Android Studio inspections tell me, that I should upgrade to 1.1.0. I changed it to
val appCompat = "1.0.0"
dependencies {
implementation("androidx.appcompat:appcompat:$appCompat")
...
}
but now I do not get that inspection hint anymore.
I compared my Kotlin script to what I find in the Sunflower reference project and found it to be working there. So, I experimented with defining extra-values with
extra.apply {
set("appCompat", "1.0.0")
}
implementation("androidx.appcompat:appcompat:${extra["appCompat"]}")
but got no inspection hint either.
To me, it seems that the inspection is broken using the Kotlin DSL. Do you agree or do you have a working setup for this?
In my environment inspection is broken also. So I used from third-party plugin and run it task in some interval for checking available update and manage it.
More details: https://github.com/jmfayard/gradle-dependencies-plugins

What is the correct way to move gradle dependency versions to a variable?

In order to maintain the version numbers of my gradle dependencies, I chose the below pattern.
In my project level build.gradle I added:
ext.versions = [
'kotlin_version' : '1.2.30',
'dagger' : '2.16'
]
And in my app module's build.gradle I added:
implementation "com.google.dagger:dagger:${versions.dagger}"
implementation "com.google.dagger:dagger-android-support:${versions.dagger}"
implementation "com.google.dagger:dagger-android:${versions.dagger}"
kapt "com.google.dagger:dagger-compiler:${versions.dagger}"
kapt "com.google.dagger:dagger-android-processor:${versions.dagger}"
But my problem is after doing this, I lost the lint warnings of "Newer Library versions available".
What is the correct way to do this without missing the lint checks?
Note: I have also tried other ways like moving these versions to gradle.properties file (for global variables).
I am looking for a solution inside Android Studio. There is one solution which I already found:
Analyze -> Run Inspection by name... -> Type "Newer Library Versions Available"
But my concern is, it is easy to miss on updates until we run some or the other script. That is why I am trying to find a way where dependency versions can be put in a variable and get lint warning for new updates.
See this answer: https://stackoverflow.com/a/46296198/2053763
There is a link to a gradle plugin which can check for dependency updates.
Android Studio 3.1.2 offers some lint checks while using version variables, but still misses some of the updates. See image below:
You can extract dependencies versions into variables stored in a separate Gradle file and then check and update them from the Project Structure (ctrl+alt+shift+S) (screenshot from Android Studio 3.6.3):

Categories

Resources