Kotlin: NoClassDefFoundError - Android Studio shows classes that shouldn't be there - android

I just updated my small Android project to Kotlin. And as a result I replaced annotationProcessor with kapt everywhere it was used in my build.gradle file. My problem is that although I don't use Guava Android Studio is allowing me to e.g. import com.google.common.base.Preconditions and use it, but when it comes to launching the app NoClassDefFoundError is thrown.
So I assume that Guava isn't compiled with my project () and that's why I am getting this error. But then why Android Studio allowing me to use it?

Related

Kotlin stdlib files missing from the apk during runtime

I am using a library which has several kotlin dependencies such as kotlin_Stdlib_jdk7
My app also has other kotlin dependencies such as kotlin_Stdlib_jdk8
When i try to call an api from the library i get this exception
java.lang.NoSuchMethodError: No interface method load(): ...internal/XU
where internal/XU is kotlin.Unit. This happens only during runtime. It's fine during compile time.
Should this be fixed by the library in their proguard file or change their gradle setup? Anyway to fix this from my app?

AndroidX migrate dependency / libraries

I have successfully migrated my project to AndroidX. App is running perfectly, but I am getting compile time errors, because my dependencies use support package.
Reason of this error
Because PhotoView is a dependency class, which uses android.support.v7.widget.AppCompatImageView which is no more available in my project. Because it is now androidx.appcompat.widget.AppCompatImageView
Project still run?
Yes, android.enableJetifier convert this dependency to AndroidX at runtime, but I want to get rid of compile time errors.
Is there a quick fix for now?
If you depend on a library that references the older Support Library, Android Studio will update that library to reference androidx instead via dependency translation. Dependency translation is automatically applied by the Android Gradle Plugin 3.2.0-alpha14, which rewrites bytecode and resources of JAR and AAR dependencies (and transitive dependencies) to reference the new androidx-packaged classes and artifacts. We will also provide a standalone translation tool as a JAR.
I see (using ./gradlew app:dependencies) that rxbinding's design dependency is updated to the new com.google.android.material dependency. Passing com.google.android.material.snackbar.Snackbar to a library function that references android.support.design.widget.Snackbar themselves makes Android Studio show a compiler error, but actually compiling and running the app works. I assume AS can't really handle these changes yet.
It seems there are some caching issues, removing .idea/libraries and performing a Gradle sync makes the errors disappear.
I solved this issue by deleting .idea folder and syncing project again.
This seems a bug of IDE not Jetifier, it does not re-sync dependencies after migrating.
Jetifier does its work well. It converts all dependencies support libraries into androidx at building time. See #this post for good explaination.
My fix for this was converting the library with the compile time error to AndroidX and submitting a pull request to the library.

Not able to view Dagger generated classes source code in IDE

I'm having trouble seeing the generated dagger classes in my project. The app builds, dagger injects the classes just fine, etc. but I can't actually view the dagger classes in my IDE - they show up as red (indicating an error), and trying to step into the class while debugging and/or viewing the source code to see how they are doing things doesn't work.
Here's a sample project that doesn't work for me: https://github.com/erikcaffrey/Dagger2-MVP-Sample
All I did was pull it, run it, and then went to the CategoryApplication class and tried to see the source for the DaggerAppComponent class, but I am not able to.
For reference, here's what I see:
Some additional information: On other computers I do not have this problem (Windows 10, MacBook Pro #2) but my main development MacBook Pro if affected by this. The dagger generated files exist in my project structure and I can view them manually - but ideally the IDE can pick these up and not show this as an error visually. I am using Android Studio 3.1.4 but this was also occurring in older versions (not sure on the exact numbers though)
The sample project I am using has the following gradle dependencies setup:
annotationProcessor 'com.google.dagger:dagger-compiler:2.15'
implementation 'com.google.dagger:dagger:2.15'
It doe snot use the android dagger components, nor the android support components, FWIW.
Update: I noticed that my Android Studio instance on my dev MacBook Pro does not list the dagger generated files in the app/build/generated/source/apt/ directory when viewing the project files in the 'Project' setting. The files on the hard drive do exist, but they do not show up in the IDE... so I doubt this is a dagger issue - just something wrong with the IDE not picking up these files.
def daggerVer = 2.12 // or latest version
implementation "com.google.dagger:dagger:$daggerVer"
implementation "com.google.dagger:dagger-android-support:$daggerVer"
annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVer"
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVer"
In your build.gradle file, add this line:
dependencies {
apt 'com.google.dagger:dagger-compiler:2.0'
}
which will make the generated sources visible in android studio.
I noticed this was not an issue on Android Studio 3.2 beta, so I figured it was a configuration issue. I went ahead and uninstalled Android Studio and deleted all references to Android Studio from my machine and then reinstalled Android Studio 3.1.4. The issue is no longer occurring.

Android Studio 3 and Gradle 4.3 -> Library modules no longer process local JARs -> how to handle this?

Upgrade to Android Studio 3.0.0 mentions this, and doesn't elaborate on how to handle it:
Library modules no longer process local JARs. This is to speed up incremental builds that are caused by changes to a library module's code.
So I have a project with a library project in it. In my library project's build.gradle file I have this:
compile files('libs/com.somelib.somepackage.jar')
I changed compile to implementation and when I tried to run my app, all my classes that tried to access the import com.somelib.somepackage.SomeClass import statement threw an error that this package didnt exist.
I changed back to compile and I was able to build and run my app.
I want to comply to the new rules since compile is deprecated and will be removed with the next Gradle release, so how do I go about doing that?
If you are trying to access classes from the .jar that is included in the library project from the app project, you will have to use api instead of implementation otherwise the classes will only be accessible in the library project:
implementation files('libs/com.somelib.somepackage.jar')
should be
api files('libs/com.somelib/somepackage.jar')
As said by the documentation:
... When a module includes an api dependency, it's letting Gradle know
that the module wants to transitively export that dependency to other
modules, so that it's available to them at both runtime and compile
time ...
Reference:
https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html#new_configurations

Kotlin class NoClassDefFoundError crash

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

Categories

Resources