Unresolved reference: CustomTarget while using glide - kotlin

I am using glide in my kotlin project i am getting the error "Unresolved reference: CustomTarget " . I would like to know how to fix this error.
My code is :
Glide.with(context)
.asBitmap()
.load(url)
.into(object : CustomTarget<Bitmap>() {
override fun onLoadCleared(placeholder: Drawable?) {
}
override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
}
})
What i tried is
Added the following code :
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.module.AppGlideModule
#GlideModule
class RequiredAppGlideModule : AppGlideModule()
Added the following in gradle
implementation 'com.github.bumptech.glide:annotations:4.7.1'
implementation "com.github.bumptech.glide:okhttp3-integration:4.6.1"
implementation 'com.github.bumptech.glide:annotations:4.7.1'
kapt 'com.github.bumptech.glide:compiler:4.14.2'
kapt "android.arch.lifecycle:compiler:1.0.0"
File -> Invalidate caches ,Restart
Reference :
get image from Firebase storage. GlideApp unresolved reference
Unresolved reference: GlideApp in activity written in Kotlin
Glide cannot resolve asBitmap()
How does one use glide to download an image into a bitmap?

Related

Gradle Kotlin DSL can't find java.awt package to use browser

tasks.register("openTestReports") {
doLast {
java.net.URL("build/reports/tests/test/index.html")
}
}
fails to work as it says
Unresolved reference: net

Kotlin serialization plugin does not generate serializers in Gradle project

I'm trying to use the Kotlinx Serialization libraries and the associated Gradle plugin. It's supposed to generate serializers for when when I annotate a class with #Serializable, like this:
#Serializable
data class Message(val version: String)
// (...)
Json.encodeToString(Message("1.0"))
It does not work, and the annotation is highlighted with:
kotlinx.serialization compiler plugin is not applied to the module, so
this annotation would not be processed. Make sure that you've setup
your buildscript correctly and re-import project.
As expected, the code does not compile as I'm expecting to use the Serializers further down the line, with the error:
Type mismatch: inferred type is Message but
SerializationStrategy<TypeVariable(T)> was expected
Here is my app-level build.gradle:
plugins {
id "java"
id 'org.jetbrains.kotlin.jvm' version '1.6.10'
id 'org.jetbrains.kotlin.plugin.serialization' version '1.6.10'
}
kotlin {
jvmToolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}
dependencies {
implementation "com.amazonaws:aws-lambda-java-core:$lambda_runtime_version"
implementation "software.amazon.awssdk:dynamodb-enhanced:$aws_sdk_version"
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$kotlin_serialization_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
task fatJar(type: Jar) {
archiveClassifier = 'lambdaCode'
duplicatesStrategy = DuplicatesStrategy.WARN
from sourceSets.main.output
dependsOn configurations.runtimeClasspath
from {
configurations.runtimeClasspath.findAll { it.name.endsWith('jar') }.collect { zipTree(it) }
}
}
I'm using Gradle 7.3.3, and see all this in IntelliJ. I tried launching gradle build from the command line, with similar results, so the IDE does not seem to blame.
I read a lot of similar questions but I could not find an answer that worked for me. Can anyone shed some light on my situation?

Unable to resolve cinterop IOS import in Kotlin Multiplatform

I have followed the Kotlin documentation for adding iOS dependencies. In my case the dependency is a pre-compiled framework provided through a third party. So I have followed the case for framework without cocoapod.
I placed my MyFramework.def file in /src
language = Objective-C
modules = MyFramework
package = MyFramework
Then I added the following to the build.gradle.kts in the Kotlin object
```
ios {
binaries {
framework {
baseName = "shared"
}
}
}
iosArm64() {
compilations.getByName("main") {
val JWBLe by cinterops.creating {
// Path to .def file
defFile("src/nativeInterop/cinterop/MyFramework.def")
compilerOpts("-framework", "MyFramework", "-F/Users/user/Projects/MyFramework/ios/SDK")
}
}
binaries.all {
// Tell the linker where the framework is located.
linkerOpts("-framework", "MyFramework", "-F/Users/user/Projects/MyFramework/ios/SDK")
}
}
sourceSets {
val commonMain by getting
val commonTest by getting {
dependencies {
implementation(kotlin("test-common"))
implementation(kotlin("test-annotations-common"))
}
}
val androidMain by getting {
dependencies {
implementation("com.google.android.material:material:1.2.1")
}
}
val androidTest by getting {
dependencies {
implementation(kotlin("test-junit"))
implementation("junit:junit:4.13")
}
}
val iosMain by getting
val iosTest by getting
}
Then I build the project. The library does indeed get seen and I see that in External Libraries, there is a shared-cinterop-MyFramework.klib
However, when I try to import this package into my code under src/iosMain/kotlin/com.example.testapp.shared/platform.kt
I get unresolved error for the library. It seems like I should also need to add something to sourceSets? But I am unsure.
First of all, I got to notice that the Gradle script is incorrect. In this case, the iosArm64 target was declared twice - by the target shortcut and once again where you configure the cinterop. To avoid this duplication, it would be better to configure cinterop like that:
ios()
val iosArm = targets.getByName("iosArm64") as org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
// A bit dirty cast, but as I'm sure iosArm64 is the Native target, it should be fine. Needed to make highlighting below work as expected.
iosArm.apply {
compilations.getByName("main") {
val JWBLe by cinterops.creating {
// Path to .def file
defFile("src/nativeInterop/cinterop/MyFramework.def")
compilerOpts("-framework", "MyFramework", "-F/Users/user/Projects/MyFramework/ios/SDK")
}
}
binaries.all {
// Tell the linker where the framework is located.
linkerOpts("-framework", "MyFramework", "-F/Users/user/Projects/MyFramework/ios/SDK")
}
}
However, this adjustment won't help with accessing cinterop bindings from the iosMain. In the current state of commonizer, it can share only platform libraries. So anyway, moving all code utilizing those bindings into the src/iosArm64Main folder is the best option available at the moment. Here go an issue from the official tracker to upvote and subscribe - Support commonization of user-defined libraries.
So after some playing around I found the answer.
The dependency was set for a module of iosArm64 which is not available to the iosMain.
I created another folder src/iosArm64Main and placed the source file there. At that point it was able to resolve the library.

Android - Glide ".placeholder" method not recognized

I have a recycler view. In the adapter's onBindViewHolder method I have the following code to load an image:
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
Log.i("TEST-APP", "Binding View Holder")
Glide.with(context)
.load(items[position])
.placeholder(R.drawable.animated_loading_icon)
.into(holder.imageView)
}
However, Android Studio is saying that "placeholder" is an unresolved reference. This is confusing because the documentation indicates that this is the correct way to load a placeholder.
What am I doing wrong?
Also, here are my imports in the RecyclerViewAdapter class
package com.example.myname.recylerviewtest
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.*
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.recyclerview_item_column.view.*
Lastly, here are my dependencies in build.gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
api 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'me.zhanghai.android.materialprogressbar:library:1.4.2'
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
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'
}
As shown in the Glide documentation:
Most options in Glide can be applied using the RequestOptions class
and the apply() method.
Use request options to apply (among others):
Placeholders
Transformations
Caching Strategies
Component specific options, like encode quality, or decode Bitmap configurations.
So, if you want to use placeholder, you have two options.
One of them is to do it this way:
Glide.with(context)
.load(items[position])
.apply(RequestOptions()
.placeholder(R.drawable.animated_loading_icon)
)
.into(holder.imageView)
And the other option would be to implement the Generated API

Dagger and Kotlin. Dagger doesn't generate component classes

I'm new with kotlin and Dagger. I have a little problem that I do not how to solve and I don't find a solution.
So this is what I have:
#Module
class AppModule (app: Application) {
private var application: Application;
init {
this.application = app;
}
#Provides fun provideApplication(): Application? {
return application;
}
#Provides fun provideResources(): Resources? {
return application.resources;
}
}
#Singleton
#Component(modules = arrayOf(AppModule::class))
interface AppComponent: AppComponentBase {
public class Initializer {
private constructor(){}
companion object {
fun Init(app: Application): AppComponent? {
return DaggerAppComponent.builder().appModule(AppModule(app)).build()
}
}
}
}
AppComponentBase: This interface contain all the methods needed by this component.
Now, the problem is that this DaggerAppComponent class is not generated by Dagger if I do this DaggerAppComponent.builder().appModule(AppModule(app)).build() invocation within the companion object.
If a invoke the same line any were by the companion object dagger generate de class without any problem.
An other thing I did look for a solution was create an other different class with the same structure, and importe the DaggerAppComponent as internal object, and I the same result happened.
I don't what to have the initialization of the component outside. So, there any other alternative solution, or what am I doing wrong?.
You need to have the kapt processor in build.gradle:
kapt {
generateStubs = true
}
dependencies {
...
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.google.dagger:dagger:2.0.2'
kapt 'com.google.dagger:dagger-compiler:2.0.2'
...
}
This extension will generate the code for dagger.
Additionally, for newer gradle versions, you can also apply the plugin in your build.gradle:
apply plugin: 'kotlin-kapt'
dependencies {
...
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
compile 'com.google.dagger:dagger:2.0.2'
kapt 'com.google.dagger:dagger-compiler:2.0.2'
...
}
You can check this project for reference
I don't know when this change happened, but on 1.1.2 of the Kotlin gradle plugin you replace this (in your module's build.gradle):
kapt {
generateStubs = true
}
with this:
apply plugin: 'kotlin-kapt'
and then make sure to replace dependencies that use annotationProcessor with kapt.
For example, the old way would be to use:
annotationProcessor (
'some.library:one:1.0'
...
'some.library.n:n.0'
)
and now you use:
kapt (
'some.library:one:1.0'
...
'some.library.n:n.0'
)
UPDATE FOR KOTLIN 1.1.4
generateStubs does not work anymore. Feel free to make a build with the latest Kotlin and it would tell you in the Messages section of Android Studio that it is not necessary anymore. Here's an up-to-date list of dependencies for Dagger2 for Android and Kotlin
apply plugin: 'kotlin-kapt'
//...
// Other Gradle stuff
//...
dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:1.1.4-3"
compile 'com.google.dagger:dagger-android:2.12'
kapt 'com.google.dagger:dagger-android-processor:2.12'
compileOnly 'com.google.dagger:dagger:2.12'
kapt 'com.google.dagger:dagger-compiler:2.12'
}
This issue can be fixed with the bellow change which is different from original answer
Following will also work well to fix this issue
with plugins
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
and dependencies
implementation "com.google.dagger:dagger:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
For reference check out this Gist
Use these dependencies if you are using kotlin as the primary language for android application development
// Dagger dependencies
def dagger_version = "2.44"
implementation "com.google.dagger:dagger:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version" // if you use the support libraries
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
My case must be some exclusion rule in Dagger's implementation
com.mycompany.native -> Dagger doesn't generate code
com.mycompany.other -> Dagger generates code
I wasted quite some time on this :-( I hope it helps someone else!
Sorry, I didn’t create a new issue, but decided to answer under similar questions, because maybe someone find it useful!
I faced to ridiculous issue! «Make Project» do not generate dagger files. They are generated while preparing run your App! But I was trying to solve problem for hours and didn't think of simple trying to click Run..)))
So, maybe you faced to the same problem:
Neither «Sync Project with Gradle» nor «Clean» and «Build Project» help generate dagger files.
-> Then just Run your App!
After the first Run App my Dagger started generated files if I click Make Project.
If u have problem withe DaggerComponent, You should add
apply plugin: 'kotlin-kapt'
kapt {
generateStubs = true
}
to build.gradleit will generate kotlin code for dagger 2 otherwise project will only build on Rebuild