Does butterknife 7.x work with Kotlin M14? - android

I'm trying to use Butterknife with some Kotlin code and also Java code. I know that before M12, there was bad or no support for annotation processing that ButterKnife required. So I have kept my activities in Java. It was working at least in Java with Butterknife 6.x and preM12 Kotlin.
I'm trying now butterknife 7.x with M13 and M14. It should have even annotation processing support, but it's not working for me. bind() function doesn't bind anything in my adapter which is written in Java nor in activity written in Kotlin.
I'm using this in build.gradle (tried latest version on Github):
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
provided files('libs/butterknife-annotations.jar')
kapt files('libs/butterknife-compiler-8.0.0-SNAPSHOT.jar')
compile 'com.jakewharton:butterknife:8.0.0-SNAPSHOT#aar'
}
This doesn't compile. I tried also 'com.neenbedankt.android-apt' which does compile but binding is not working.
I know that there is probably not support in butterknife for that yet. But is there any hack to get it working?

It does work with the current version of Kotlin (1.0.0-beta-3595), I suggest you to take a look at the android-butterknife project which can be found inside the JetBrains's kotlin-examples repo. In short all you need to do is:
Add the following to your app/build.gradle file:
kapt {
generateStubs = true
}
Put the following line inside the dependencies block of the same build.gradle file (assuming you already added compile 'com.jakewharton:butterknife:7.0.1' to your dependencies):
kapt 'com.jakewharton:butterknife:7.0.1'
And that should be it.

Butterknife is supported. Use kapt: [1], [2].
Note that Butterknife does not support private Java fields, so you can use the lateinit modifier to make it public.
Also, if you use kapt,
apply plugin: 'com.neenbedankt.android-apt'
line is not needed anymore.

Related

"kotlin-kapt" vs "annotationProcessor()"?

What is the basic core difference between:
kotlin-kapt
annotationProcessor()
Also, what's the working behavior of these in Kotlin/Java?
I researched a lot about this topic but I'm a little bit confused. So, I need to learn more to clear my concepts.
kapt: Default annotation processor for Kotlin projects, useful to reference generated code at compile time in a kotlin project. In order to use it you should use the kotlin-kapt plugin. It also takes care of java classes
plugins {
kotlin("kapt") version "1.7.10"
}
and the related dependency:
dependencies {
kapt("groupId:artifactId:version")
}
annotationProcessor: gradle directive to specify your own annotationProcessor or a third party one. For example, in the old android projects was common to use Butterknife library that has its own annotation processor:
dependencies {
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
}
In general, an annotation processor is useful to generate code automatically from annotations (for butternife, for example #BindView) and this is true both for kapt and for a random third-party annotation processor.

Duplicate class kotlin classes kotlin version 1.3.70

Error while adding MoshiPack Library in Kotlin latest version 1.3.70 to gradle.build application
Moshi pack
implementation 'com.daveanthonythomas.moshipack:moshipack:1.0.1'
Error Message
Duplicate class kotlin.reflect.KClasses found in modules jetified-kotlin-reflect-1.1.1.jar (org.jetbrains.kotlin:kotlin-reflect:1.1.1) and jetified-kotlin-stdlib-1.3.70.jar (org.jetbrains.kotlin:kotlin-stdlib:1.3.70)
Any suggestions how to solve this issue or any other library I can use in Kotlin so I can use Message Pack.
Thanks in advance
Try to add this to your dependencies:
implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
and make sure you specified your Android NDK location under File>Project Structure...>SDK Location
Starting Kotlin 1.3.70 some basic useful members on KClass included in Kotlin standard library (they were in a kotlin-reflect before).
See "Working with KClass" in https://blog.jetbrains.com/kotlin/2020/03/kotlin-1-3-70-released/
In your case MoshiPack adds a kotlin-reflect library that conflicts with standard library.
You should exclude transitive dependency to resolve the conflict.
If you use KotlinDSL, in build.gradle.kts:
implementation ("com.daveanthonythomas.moshipack:moshipack:1.0.1") {
exclude(group = "org.jetbrains.kotlin", module = "kotlin-reflect")
}
If you use Groovy, in build.gradle:
implementation ('com.daveanthonythomas.moshipack:moshipack:1.0.1') {
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-reflect'
}
I tried this and it worked
implementation "org.jetbrains.kotlin:kotlin-reflect:1.4.10"
i think the only way to solve it , to go back to kotlin version 1.3.61 , so remove 1.3.70 and use 1.3.61
So I finally figured it out and here's how :-
So the main problem is you have injected 2 dependency of same class. Here he used 2 dependency for Kotlin which conflict in runtime to fix that you have to check which dependency is duplicated. (It is most case scenario. It can be with any other dependency i.e. Hilt)
Go to File > Project structure > Dependencies
Check which dependency are repeating. In this case it will have (androidx.core:core:1.8.0) and (androidx.core:core:+)
as you can see there are 2 dependency with same classes version 1.8.0 will have all the class which core:+ will have and this is causing a error.
Now delete (androidx.core:core:+) and hit Apply and sync project.
Now you should be good to go. here is structure after changing dependency.
Note:- This method will show all the android dependency which you might be not included but you will see all the dependency which any app has. Please remove the dependency who are you familiar with do not remove any dependency without any proper knowledge.

Could not find method implementationSdkVersion() for arguments [26] on object of type com.android.build.gradle.AppExtension

Screenshot of the code is here i also replaced compile with implementation. I used latest version of android studio.
You need to use compileSdkVersion at the top of the android section of your build.gradle file, not implementationSdkVersion. implementation is only used for dependency imports within the dependencies section.

Kotlin build cannot get classes created by SquidDatabase

In my project I use SquidDatabase almost everywhere to access the database.
I want to transform my project to Kotlin but when I build there are lots of "Unresolved reference: DepartmentInfo" errors.
That DepartmentInfo class was created by SquidDatabase while building so I think this problem is caused by Kotlin building before other libraries.
I failed to find any way to change Kotlin to the end of buildlist.
How can I solve this problem?
I posted this issue on Github and got reply from developers.
The solotion is here:
build.gradle of your app
//If using kotlin language,please add this
apply plugin: 'kotlin-kapt'
......
dependencies {
compile 'com.yahoo.squidb:squidb:3.2.3'
compile 'com.yahoo.squidb:squidb-annotations:3.2.3'
compile 'com.yahoo.squidb:squidb-android:3.2.3' // For Android projects only
//annotationProcessor 'com.yahoo.squidb:squidb-processor:3.2.3'
// If using the android-apt plugin, this becomes
// apt 'com.yahoo.squidb:squidb-processor:3.2.3'
// If using kotlin language, this becomes
kapt 'com.yahoo.squidb:squidb-processor:3.2.3'
}
It may cause errors if you use DataBinding for Andriod,and the solution is to add one more kapt in your dependencies:
kapt 'com.android.databinding:compiler:$gradle_version'

Android javax.annotation.processing Package missing

I would like to do some annotation processing based on the example in the following link: http://www.zdnetasia.com/writing-and-processing-custom-annotations-part-3-39362483.htm.
However, I would like to implement this in my Android project, and it seems I cannot use the package with the android platform. Do I need to add an external jar or is there something I'm missing?
Thanks.
The javax.annotation.processing package is not included in Android. You don't need to include this inside your compiled app module. You just need this dependency for your compiler/processor module during build time. Therefore, inside your app's build.gradle you should add the following after including your annotation processor:
provided 'org.glassfish:javax.annotation:10.0-b28'
There is a much easier way if you use Android Studio / Gradle.
I found the following looking through the answers of the question How do I add javax.annotation.Generated to Java SE 5?:
For a Gradle build the dependency should be
compile 'org.glassfish:javax.annotation:10.0-b28'
The javax.annotation.processing package is not included in Android, but the Android VM team describes how to include extra javax packages here - it might be applicable to your question.
implementation 'org.glassfish:javax.annotation:10.0-b28'
compileOnly 'com.github.pengrad:jdk9-deps:1.0'
Add these two dependencies in your build.gradle file, this will work.
compileOnly 'org.glassfish:javax.annotation:10.0-b28'
Simply adding compile 'javax.annotation:javax.annotation-api:1.2' should do the trick.
see here for all available versions: https://mvnrepository.com/artifact/javax.annotation/javax.annotation-api
for kotlin DSL you need to use compileOnly("org.glassfish:javax.annotation:10.0-b28")
for groovy DSL you need to use compileOnly 'org.glassfish:javax.annotation:10.0-b28'
I also faced same issue.
So generally we will face this issue when our jdk version will be 8+ .
So for jdk8+ you just need to add below dependency in your build.gradle(:app) file.
compileOnly 'com.github.pengrad:jdk9-deps:1.0'
It will work like a charm. Please give it a try!
I find a method in butterknife. Add this in build.gradle:
compile files(org.gradle.internal.jvm.Jvm.current().getJre().homeDir.getAbsolutePath()+'/lib/rt.jar')

Categories

Resources