Android - Glide ".placeholder" method not recognized - kotlin

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

Related

Micronaut #SerdeImport annotation not working with Kotlin

I am working on developing an application using Micronaut framework with Kotlin language.
However, I am facing an issue where #SerdeImport annotation is not being recognized when using Micronaut with Kotlin.
I am getting the below error even after using the #SerdeImport annotation.
No serializable introspection present for type MarketSnapshot B.
Consider adding Serdeable. Serializable annotate to type MarketSnapshot B.
Alternatively if you are not in control of the project's source code,
you can use #SerdeImport(MarketSnapshot.class) to enable serialization of this type."}]}}
The same annotation works fine when using Java, however, it fails with the Kotlin program.
Below are the micronaut kotlin related dependencies that are used in build.gradle file
annotationProcessor 'io.micronaut:micronaut-inject-java'
annotationProcessor 'io.micronaut:micronaut-http-validation'
annotationProcessor 'io.micronaut.openapi:micronaut-openapi'
annotationProcessor 'io.micronaut.serde:micronaut-serde-processor'
kapt 'io.micronaut:micronaut-http-validation'
kapt 'io.micronaut:micronaut-inject-java'
implementation 'org.jetbrains.kotlin:kotlin-reflect:${kotlin}'
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlin}'
implementation 'io.micronaut.kotlin:micronaut-kotlin-runtime'
implementation 'io.micronaut.serde:micronaut-serde-jackson'
implementation 'io.micronaut:micronaut-management'
implementation 'io.micronaut:micronaut-http-client'
implementation 'io.micronaut:micronaut-jackson-databind'
implementation 'io.micronaut:micronaut-validation'
implementation 'jakarta.annotation:jakarta.annotation-api'
Do you have something like this in your build.gradle:
configurations.all {
resolutionStrategy.dependencySubstitution {
substitute(module("io.micronaut:micronaut-jackson-databind"))
.using(module("io.micronaut.serde:micronaut-serde-jackson:1.5.0"))
}
}
Also, think annotationProcessor 'io.micronaut.serde:micronaut-serde-processor' should be kapt("io.micronaut.serde:micronaut-serde-processor")
Micronaut Launch has a diff view to show the changes when adding a new feature.
For example: Micronaut Kotlin with serialization-jackson diff shows this for the changes to the build file:
dependencies {
kapt("io.micronaut:micronaut-http-validation")
+ kapt("io.micronaut.serde:micronaut-serde-processor")
implementation("io.micronaut:micronaut-http-client")
- implementation("io.micronaut:micronaut-jackson-databind")
implementation("io.micronaut.kotlin:micronaut-kotlin-runtime")
+ implementation("io.micronaut.serde:micronaut-serde-jackson")
implementation("jakarta.annotation:jakarta.annotation-api")
implementation("org.jetbrains.kotlin:kotlin-reflect:${kotlinVersion}")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlinVersion}")
## -57,3 +58,12 ##
annotations("com.example.*")
}
}
+
+
+
+configurations.all {
+ resolutionStrategy.dependencySubstitution {
+ substitute(module("io.micronaut:micronaut-jackson-databind"))
+ .using(module("io.micronaut.serde:micronaut-serde-jackson:1.5.0"))
+ }
+}

Unresolved reference: CustomTarget while using glide

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?

Data Class with Nullable Parameter in Kotlin 1.5.0 & 1.6.0 Throwing BackendException

Recently updated from Kotlin 1.4.20 to 1.5.30.
I have this class which used to compile no problem
data class Optional<M>(val value: M?)
However, after upgrading, I get the following exception
org.jetbrains.kotlin.backend.common.BackendException: Backend Internal error: Exception during IR lowering
File being compiled: .../Optional.kt
The root cause java.lang.RuntimeException was thrown at: org.jetbrains.kotlin.backend.jvm.codegen.FunctionCodegen.generate(FunctionCodegen.kt:50)
at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException(CodegenUtil.kt:239)
at org.jetbrains.kotlin.backend.common.CodegenUtil.reportBackendException$default(CodegenUtil.kt:235)
at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invokeSequential(performByIrFile.kt:68)
at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:55)
at org.jetbrains.kotlin.backend.common.phaser.PerformByIrFilePhase.invoke(performByIrFile.kt:41)
at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.invoke(CompilerPhase.kt:96)
at org.jetbrains.kotlin.backend.common.phaser.CompositePhase.invoke(PhaseBuilders.kt:29)
at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.invoke(CompilerPhase.kt:96)
at org.jetbrains.kotlin.backend.common.phaser.CompositePhase.invoke(PhaseBuilders.kt:29)
at org.jetbrains.kotlin.backend.common.phaser.NamedCompilerPhase.invoke(CompilerPhase.kt:96)
at org.jetbrains.kotlin.backend.common.phaser.CompilerPhaseKt.invokeToplevel(CompilerPhase.kt:43)
at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.doGenerateFilesInternal(JvmIrCodegenFactory.kt:191)
at org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory.generateModule(JvmIrCodegenFactory.kt:60)
at org.jetbrains.kotlin.codegen.KotlinCodegenFacade.compileCorrectFiles(KotlinCodegenFacade.java:35)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.generate(KotlinToJVMBytecodeCompiler.kt:321)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli(KotlinToJVMBytecodeCompiler.kt:113)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules$cli$default(KotlinToJVMBytecodeCompiler.kt:56)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:169)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:52)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:92)
at org.jetbrains.kotlin.cli.common.CLICompiler.execImpl(CLICompiler.kt:44)
at org.jetbrains.kotlin.cli.common.CLITool.exec(CLITool.kt:98)
at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:412)
at org.jetbrains.kotlin.incremental.IncrementalJvmCompilerRunner.runCompiler(IncrementalJvmCompilerRunner.kt:112)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally(IncrementalCompilerRunner.kt:358)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileIncrementally$default(IncrementalCompilerRunner.kt:300)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileImpl$rebuild(IncrementalCompilerRunner.kt:119)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compileImpl(IncrementalCompilerRunner.kt:170)
at org.jetbrains.kotlin.incremental.IncrementalCompilerRunner.compile(IncrementalCompilerRunner.kt:81)
at org.jetbrains.kotlin.daemon.CompileServiceImplBase.execIncrementalCompiler(CompileServiceImpl.kt:607)
at org.jetbrains.kotlin.daemon.CompileServiceImplBase.access$execIncrementalCompiler(CompileServiceImpl.kt:96)
at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:1658)
I get that it's telling me that Any's toString() method doesn't have a body as I can see that from looking at the source code.
What I don't understand is how to rectify it.
EDIT
Can confirm this still happens when upgrading to kotlin version 1.6.0.
It also happens in this data class:
data class Advertisement(
var id: String,
var image: String,
var navUrl: String?
)
My build.gradle file for this module looks like so:
apply plugin: 'kotlin'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.stustirling.redacted-gradle-plugin'
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"
implementation "io.reactivex.rxjava2:rxkotlin:$rxKotlinVersion"
implementation "io.reactivex.rxjava2:rxjava:$rxJavaVersion"
implementation "com.jakewharton.timber:timber:$timberVersion"
implementation "com.squareup.retrofit2:retrofit:$retrofitVersion"
implementation "com.google.dagger:dagger:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
implementation "org.json:json:$testOrgJsonVersion"
implementation "net.sf.biweekly:biweekly:$biweeklyVersion"
testImplementation "junit:junit:$junitVersion"
testImplementation "org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion"
testImplementation "org.mockito:mockito-inline:$mockitoInlineVersion"
testImplementation "org.json:json:$testOrgJsonVersion"
}
redacted {
redactAllDataClasses true
redactClassName true
}
buildscript {
repositories {
mavenLocal()
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
}
}
My Gradle couldn't find the redacted-gradle-plugin but both of your classes compiles with this minimalistic build.gradle.kts:
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
val kotlinVersion = "1.6.0"
kotlin("jvm") version kotlinVersion
}
repositories {
mavenCentral()
}
Also, while looking at the redacted page at https://github.com/StuStirling/redacted-compiler-plugin, it says "Kotlin compiler plugins are not a stable API", so at least do a "./gradlew clean"!

Not able to run unit tests on kotlin Multiplatform project

I am trying to create a kotlin Multiplatform library which can later convert into java and javascript using  IDEA 2019.3, kotlin 1.3 .
I created a simple junit (4) test class and configured it as follows.
package sample;
import org.junit.Test;
public class Tests {
#Test
public void test1(){
System.out.println("here");
}
}
But while running, it’s failing with
Process finished with exit code 1 Class not found: "sample.DummyTest"
I could not figure out what am I doing wrong. Please advice.
check your name of class test, it could be: com.package.example.Tests
or click right on your class and make the configuration:
if you want to run all test (included unitest from implements modules) in your project, create this configuration:
in your gradle dependencies check this import
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'

httpclient conflict with classes now provided by Android

In Android Studio 3.4.1
app/build.gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'org.apache.httpcomponents:httpclient:4.5.9'
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'
}
But I get error in this line;
implementation 'org.apache.httpcomponents:httpclient:4.5.9'
error:
httpclient defines classes that conflict with classes now provided by Android. Solutions include finding newer versions or alternative libraries that don't have the same problem (for example, for httpclient use HttpUrlConnection or okhttp instead), or repackaging the library using something like jarjar. more... (Ctrl+F1)
Check out this changelog for the Android API.
You should replace the Apache HTTP functions by HttpURLConnection or use this snipped in your build.gradle to continue using the now deprecated Apache libraries.
android {
useLibrary 'org.apache.http.legacy'
}