java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to java.util.ArrayList

Issue detail:
I am parsing the List using gson in kotlin with below syntax:
val methods: ArrayList<DataPOJO> = Gson().fromJson(dataObj.getString(withdraw_methods),
Array<DataPOJO>::class.java).toList() as ArrayList<DataPOJO>
Here, It's working fine while API response "dataObj.getString(withdraw_methods)" string array contains more than one object.
In case of one object its giving below error: java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to java.util.ArrayList

Solution:
val methods: ArrayList<DataPOJO> = ArrayList(Gson().fromJson(dataObj.getString(withdraw_methods), Array<DataPOJO>::class.java).toList())

Best article to understand SingletonList Vs. List::of -- Collections :: Singleton List Showdown

Detailed error log Android Studio:
02-20 13:35:20.370 17010-17010/com.myandriodapp W/System.err: java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to java.util.ArrayList
02-20 13:35:20.372 1483-1507/? I/Timeline: Timeline: Activity_windows_visible id: ActivityRecord{c8e1655 u0 com.myandriodapp/.v3_kotlin.cashback.user_activity.ProfileActivity t262} time:51048610
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at com.myandriodapp.v3_kotlin.cashback.user_activity.WithdrawalFragment$onActivityCreated$5.onChanged(WithdrawalFragment.kt:199)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at com.myandriodapp.v3_kotlin.cashback.user_activity.WithdrawalFragment$onActivityCreated$5.onChanged(WithdrawalFragment.kt:50)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:149)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at androidx.lifecycle.LiveData.setValue(LiveData.java:307)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at androidx.lifecycle.MutableLiveData.setValue(MutableLiveData.java:50)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at com.myandriodapp.v3_kotlin.cashback.user_activity.viewmodel.WithdrawalViewModel$AppWithdrawMethodsAPI$1.invokeSuspend(WithdrawalViewModel.kt:101)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at android.os.Handler.handleCallback(Handler.java:742)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at android.os.Looper.loop(Looper.java:157)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5603)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
02-20 13:35:20.395 17010-17010/com.myandriodapp W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)

custom spinner adapter android kotlin

Spinner Custom Item Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageViewFlag"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="5dp"
        app:layout_constraintEnd_toStartOf="@+id/textViewCountryName"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <TextView
        android:id="@+id/textViewCountryName"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Demo"
        android:textColor="#000"
        android:padding="7dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/imageViewFlag"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Spinner Custom Adapter
package com.example.customspinnerexamplekotlin

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import kotlinx.android.synthetic.main.item_custom_spinner.view.*


class CustomSpinnerAdapter(ctx: Context, countries: ArrayList<CountryData>) : ArrayAdapter<CountryData>(ctx, 0, countries) {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        return createItemView(position, convertView, parent);
    }

    override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
        return createItemView(position, convertView, parent);
    }

    fun createItemView(position: Int, recycledView: View?, parent: ViewGroup):View {
        val country = getItem(position)

        val view = recycledView ?: LayoutInflater.from(context).inflate(
                R.layout.item_custom_spinner,
                parent,
                false
        )

        country?.let {
            view.imageViewFlag.setImageResource(country.flag)
            view.textViewCountryName.text = country.countryName
        }
        return view
    }
}

CountryData Object
package com.example.customspinnerexamplekotlin

class CountryData (val countryName: String,
                   val flag: Int)

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#f3f3f3">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="16dp" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineEnd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_end="16dp" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/tv_selected_item"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="Select Country from below spinner"
        android:textColorHint="#000000"
        app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
        app:layout_constraintStart_toStartOf="@id/guidelineStart"
        app:layout_constraintTop_toTopOf="parent">
    </com.google.android.material.textview.MaterialTextView>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cl_spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#ffffff"
        android:padding="5dp"
        app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
        app:layout_constraintStart_toStartOf="@+id/guidelineStart"
        app:layout_constraintTop_toBottomOf="@id/tv_selected_item">

        <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt
package com.example.customspinnerexamplekotlin

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setCustomAdapterSpinner()
    }

    fun setCustomAdapterSpinner() {

        val country_list = arrayListOf<CountryData>()

        country_list.add(CountryData("India", R.drawable.ic_flag_black_24dp))
        country_list.add(CountryData("United States", R.drawable.ic_flag_black_24dp))
        country_list.add(CountryData("Indonesia", R.drawable.ic_flag_black_24dp))
        country_list.add(CountryData("France", R.drawable.ic_flag_black_24dp))
        country_list.add(CountryData("China", R.drawable.ic_flag_black_24dp))

        val adapter = CustomSpinnerAdapter(
                this,
                country_list
        )

        spinner.adapter = adapter

        spinner.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, p1: View?, pos: Int, p3: Long) {
                Toast.makeText(this@MainActivity, "" + (parent?.getItemAtPosition(pos) as CountryData).countryName, Toast.LENGTH_SHORT).show()
            }

            override fun onNothingSelected(p0: AdapterView<*>?) {
            }
        })

        // dynamically adding data after setting adapter to spinner

        country_list.add(CountryData("Japan", R.drawable.ic_flag_black_24dp))
        country_list.add(CountryData("New Zealand", R.drawable.ic_flag_black_24dp))
        country_list.add(CountryData("Other", R.drawable.ic_flag_black_24dp))

        adapter.notifyDataSetChanged()
    }

}

android kotlin custom spinner adapter
Android kotlin Custom Spinner Adapter example


custom spinner with image and text android kotlin
Custom spinner Adapter with image and text android kotlin



android spinner example kotlin

android spinner example - string array xml

res/values/strings.xml
<resources>
    <string name="app_name">KotlinSpinnerExample</string>

    <string-array name="country_arrays">
        <item>India</item>
        <item>United States</item>
        <item>Indonesia</item>
        <item>France</item>
        <item>China</item>
        <item>Japan</item>
        <item>New Zealand</item>
        <item>Other</item>
    </string-array>

</resources>

Populate the Spinner with string array choices

With an array such as mentioned above, you can use below code in your Activity or Fragment to set spinner with the string array using an ArrayAdapter instance:

package com.example.kotlinspinnerexample

import androidx.appcompat.app.AppCompatActivity

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        setSimpleSpinner()
    }

    fun setSimpleSpinner() {
        // Create an ArrayAdapter using the string array (country_arrays) and a default spinner layout
        ArrayAdapter.createFromResource(
                this,
                R.array.country_arrays,
                android.R.layout.simple_spinner_item
        ).also { adapter ->
            // Specify the layout to use when the list of choices appears
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
            // Apply the adapter to the spinner
            spinner.adapter = adapter
        }

       spinner.setOnItemSelectedListener(object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>?, p1: View?, pos: Int, p3: Long) {
                Toast.makeText(this@MainActivity, "" + parent?.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show()
            }
            override fun onNothingSelected(p0: AdapterView<*>?) {
            }
        })
    }
}


Here, I have put the spinner inside the ConstraintLayout and guildeline is on both sides. ConstraintLayout  has white background  and wraps AppCompatSpinner. So I can highlight the spinner according to background (background is grey here).
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#f3f3f3">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_begin="16dp" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guidelineEnd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_end="16dp" />

    <com.google.android.material.textview.MaterialTextView
        android:id="@+id/tv_selected_item"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:hint="Select Country from below spinner"
        android:textColorHint="#000000"
        app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
        app:layout_constraintStart_toStartOf="@id/guidelineStart"
        app:layout_constraintTop_toTopOf="parent">
    </com.google.android.material.textview.MaterialTextView>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/cl_spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:background="#ffffff"
        android:padding="5dp"
        app:layout_constraintEnd_toEndOf="@id/guidelineEnd"
        app:layout_constraintStart_toStartOf="@+id/guidelineStart"
        app:layout_constraintTop_toBottomOf="@id/tv_selected_item">

        <androidx.appcompat.widget.AppCompatSpinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
android kotlin spinner adapter
android kotlin - Spinner example

set string array to spinner android xml
Populate the Spinner with country string array

how to set value in spinner dynamically in android

var country_list = arrayListOf<String>()

        country_list.add("India")
        country_list.add("United States")
        country_list.add("Indonesia")
        country_list.add("France")
        country_list.add("China")
        country_list.add("Japan")
        country_list.add("New Zealand")
        country_list.add("Other")

val adapter = ArrayAdapter(
                this, // Context
                android.R.layout.simple_spinner_dropdown_item, // Layout
                country_list // ArrayList
        )

spinner.adapter = adapter
        

How to use Kotlin Coroutines with volley

How to use API connection with Kotlin Coroutines + Volley
When start developing API with Volley in Kotlin, you want to use Kotlin Coroutine instead of AsyncTask. We see how to use API connection using Volley and Coroutine as an example.
First of all, How can I take advantage of coroutines so I can write my code in concise manner:
Here's an example to Coroutine calling API Android

Volley overview

Volley is an HTTP library that makes networking for Android apps easier and most importantly, faster. Volley is available on GitHub. Volley is an HTTP client developed by google for android development. You can use suspendCancellableCoroutine to make the Volley API request.

NetworkUtility.kt
package com.example.myapplication

import com.android.volley.Request
import com.android.volley.RequestQueue
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import kotlinx.coroutines.suspendCancellableCoroutine
import org.json.JSONObject
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

class NetworkUtility {
 
    companion object {
        val mRequestQueue: RequestQueue by lazy {
            Volley.newRequestQueue(MainApp.get())
        }

        suspend fun APIrequest(url: String): JSONObject {

            return suspendCancellableCoroutine { continuation ->
                try {
                    // Sucess Listner
                    val success = Response.Listener<JSONObject> { response ->
                        if (continuation.isActive) {
                            continuation.resume(response)
                        }
                    }

                    // Error Listner
                    val error = Response.ErrorListener { error ->
                        if (continuation.isActive) {
                            continuation.resume(JSONObject())
                        }
                    }

                    val jsonObjectRequest =
                        JsonObjectRequest(Request.Method.GET, url, null, success, error)

                    mRequestQueue.add(jsonObjectRequest)

                } catch (e: Exception) {
                    e.printStackTrace()
                    if (continuation.isActive) {
                        if (continuation.isActive) {
                            continuation.resumeWithException(e)
                        }
                    }
                }
            }
        }
    }
 
}

MainViewModel.kt
package com.example.myapplication

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.json.JSONObject

class MainViewModel : ViewModel() {

    var usersList: MutableLiveData<ArrayList<User>> = MutableLiveData(arrayListOf())

    var isLoadingData = true
    var showProgress = MutableLiveData<Boolean>()


    fun getData(urlStr: String) {
        showProgress.value = true

        viewModelScope.launch(Dispatchers.IO) {
            val rss = NetworkUtility.APIrequest(urlStr)

            withContext(Dispatchers.Main) {
                // call to UI thread
                isLoadingData = false
                showProgress.value = false
                usersList.value?.addAll(parseJsonString(rss.toString()))
            }
        }
    }

    fun parseJsonString(str: String): ArrayList<User> {
        val jsonOb = JSONObject(str)
        val list: ArrayList<User> = arrayListOf()
        val jsonArray = jsonOb.getJSONArray("data");

        for (j in 0..jsonArray.length() - 1) {
            val user = User(
                jsonArray.getJSONObject(j).getString("id"),
                jsonArray.getJSONObject(j).getString("email"),
                jsonArray.getJSONObject(j).getString("first_name"),
                jsonArray.getJSONObject(j).getString("last_name"),
                jsonArray.getJSONObject(j).getString("avatar")
            )
            list.add(user)
        }
        return list
    }

}


MainActivity.kt
package com.example.myapplication

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.myapplication.databinding.ActivityMainBinding
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.toolbar.*

class MainActivity : AppCompatActivity() {
    lateinit var viewModel: MainViewModel
    lateinit var userAdapter: UserRVAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding: ActivityMainBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_main)

        setSupportActionBar(toolbar)
        getSupportActionBar()?.setDisplayHomeAsUpEnabled(true)

        viewModel = ViewModelProviders.of(this)[MainViewModel::class.java]
        binding.mainVM = viewModel
        binding.setLifecycleOwner(this)

        viewModel.getData("https://reqres.in/api/users?page=1&per_page=12")

        viewModel.showProgress.observe(this, Observer {
            if (it == true) {
                progressBar.visibility = View.VISIBLE
            } else {
                progressBar.visibility = View.GONE
            }
        })

        viewModel.usersList.observe(this, Observer {
            Log.e("ResSize", "" + it.size + "::" + it)
            userAdapter.addList(it)
        })

        // Creates a vertical Layout Manager
        rv_users.layoutManager = LinearLayoutManager(this)
        userAdapter = UserRVAdapter(this)
        rv_users.adapter = userAdapter
    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable name="mainVM" type="com.example.myapplication.MainViewModel" />

    </data>
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    tools:context=".MainActivity">

    <include layout="@layout/toolbar" />

    <TextView
        app:layout_constraintTop_toBottomOf="@+id/toolbar"
        android:id="@+id/tv_select_user"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Users"
        android:layout_margin="7dp"
        android:textColor="@android:color/black"
        app:layout_constraintLeft_toLeftOf="parent"
        android:textSize="20sp"/>


    <androidx.recyclerview.widget.RecyclerView
        android:layout_marginTop="10dp"
        app:layout_constraintTop_toBottomOf="@id/tv_select_user"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/rv_users"
        android:layout_width="match_parent"
        android:layout_height="0dp"/>

    <ProgressBar
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        android:id="@+id/progressBar"
        android:visibility="gone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

UserRVAdapter.kt
package com.example.myapplication

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import kotlinx.android.synthetic.main.ly_item_user.view.*

class UserRVAdapter(val context: Context): RecyclerView.Adapter<UserRVAdapter.ViewHolder>() {

    var items : ArrayList<User> = arrayListOf()

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val posObj=items.get(position);

        holder.tv_name1?.text =posObj.first_name +" "+posObj.last_name
        holder.tv_email1?.text = posObj.email

        GlideApp.with(holder.iv_profile1.context)
            .load(posObj.avatar)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .apply(RequestOptions.circleCropTransform())
            .error(R.drawable.ic_launcher_background)
            .placeholder(R.drawable.ic_launcher_background)
            .into(holder.iv_profile1)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.ly_item_user, parent, false))
    }

    fun addList(itemsNew : ArrayList<User>){
            this.items=itemsNew
            notifyDataSetChanged()
            Log.e("ResSize:ItemsIF", "" + items.size)
    }

    override fun getItemCount(): Int {
        return items.size
    }

    class ViewHolder (view: View) : RecyclerView.ViewHolder(view) {
        // Holds the TextView that will add each animal to
        val tv_name1 = view.tv_name
        val tv_email1 = view.tv_email
        val iv_profile1 = view.iv_profile
    }
}

ly_item_user.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/iv_profile"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="50dp"
        android:layout_margin="10dp"
        android:layout_height="50dp"/>

    <TextView
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@id/iv_profile"
        app:layout_constraintBottom_toTopOf="@+id/tv_email"
        android:id="@+id/tv_name"
        android:textSize="20sp"
        android:text="Pradip"
        android:layout_marginLeft="10dp"
        android:textColor="@android:color/black"
        app:layout_constraintVertical_chainStyle="packed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_marginLeft="10dp"
        app:layout_constraintLeft_toRightOf="@id/iv_profile"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv_name"
        android:id="@+id/tv_email"
        android:text="Tilala"
        android:textColor="@android:color/darker_gray"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <View
        app:layout_constraintTop_toBottomOf="@id/iv_profile"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_width="match_parent"
        android:background="#f3f3f3"
        android:layout_margin="10dp"
        android:layout_height="2dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Best reference : API connection with Volley + Coroutine

Invoke-customs are only supported starting with Android O (--min-api 26) - Android Error


Invoke-customs are only supported starting with Android O (--min-api 26)

Static interface methods are only supported starting with Android N (--min-api 24): okhttp3.Request okhttp3.Authenticator.lambda$static$0(okhttp3.Route, okhttp3.Response)


DEtailed log:

org.gradle.execution.MultipleBuildFailures: Build completed with 1 failures.
at org.gradle.initialization.DefaultGradleLauncher$ExecuteTasks.run(DefaultGradleLauncher.java:386)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:402)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:394)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:165)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:250)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:158)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:92)
at org.gradle.internal.operations.DelegatingBuildOperationExecutor.run(DelegatingBuildOperationExecutor.java:31)
at org.gradle.initialization.DefaultGradleLauncher.runTasks(DefaultGradleLauncher.java:247)
at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(DefaultGradleLauncher.java:159)
at org.gradle.initialization.DefaultGradleLauncher.executeTasks(DefaultGradleLauncher.java:134)
at org.gradle.internal.invocation.GradleBuildController$1.execute(GradleBuildController.java:58)
at org.gradle.internal.invocation.GradleBuildController$1.execute(GradleBuildController.java:55)
at org.gradle.internal.invocation.GradleBuildController$3.create(GradleBuildController.java:82)
at org.gradle.internal.invocation.GradleBuildController$3.create(GradleBuildController.java:75)
at org.gradle.internal.work.DefaultWorkerLeaseService.withLocks(DefaultWorkerLeaseService.java:183)
at org.gradle.internal.work.StopShieldingWorkerLeaseService.withLocks(StopShieldingWorkerLeaseService.java:40)
at org.gradle.internal.invocation.GradleBuildController.doBuild(GradleBuildController.java:75)
at org.gradle.internal.invocation.GradleBuildController.run(GradleBuildController.java:55)
at org.gradle.tooling.internal.provider.runner.ClientProvidedBuildActionRunner.run(ClientProvidedBuildActionRunner.java:55)
at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
at org.gradle.launcher.exec.ChainingBuildActionRunner.run(ChainingBuildActionRunner.java:35)
at org.gradle.launcher.exec.BuildOutcomeReportingBuildActionRunner.run(BuildOutcomeReportingBuildActionRunner.java:58)
at org.gradle.tooling.internal.provider.ValidatingBuildActionRunner.run(ValidatingBuildActionRunner.java:32)
at org.gradle.launcher.exec.BuildCompletionNotifyingBuildActionRunner.run(BuildCompletionNotifyingBuildActionRunner.java:39)
at org.gradle.launcher.exec.RunAsBuildOperationBuildActionRunner$3.call(RunAsBuildOperationBuildActionRunner.java:51)
at org.gradle.launcher.exec.RunAsBuildOperationBuildActionRunner$3.call(RunAsBuildOperationBuildActionRunner.java:45)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:416)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:406)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:165)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:250)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:158)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:102)
at org.gradle.internal.operations.DelegatingBuildOperationExecutor.call(DelegatingBuildOperationExecutor.java:36)
at org.gradle.launcher.exec.RunAsBuildOperationBuildActionRunner.run(RunAsBuildOperationBuildActionRunner.java:45)
at org.gradle.launcher.exec.InProcessBuildActionExecuter$1.transform(InProcessBuildActionExecuter.java:49)
at org.gradle.launcher.exec.InProcessBuildActionExecuter$1.transform(InProcessBuildActionExecuter.java:46)
at org.gradle.composite.internal.DefaultRootBuildState.run(DefaultRootBuildState.java:78)
at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:46)
at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(InProcessBuildActionExecuter.java:31)
at org.gradle.launcher.exec.BuildTreeScopeBuildActionExecuter.execute(BuildTreeScopeBuildActionExecuter.java:42)
at org.gradle.launcher.exec.BuildTreeScopeBuildActionExecuter.execute(BuildTreeScopeBuildActionExecuter.java:28)
at org.gradle.tooling.internal.provider.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:78)
at org.gradle.tooling.internal.provider.ContinuousBuildActionExecuter.execute(ContinuousBuildActionExecuter.java:52)
at org.gradle.tooling.internal.provider.SubscribableBuildActionExecuter.execute(SubscribableBuildActionExecuter.java:59)
at org.gradle.tooling.internal.provider.SubscribableBuildActionExecuter.execute(SubscribableBuildActionExecuter.java:36)
at org.gradle.tooling.internal.provider.SessionScopeBuildActionExecuter.execute(SessionScopeBuildActionExecuter.java:68)
at org.gradle.tooling.internal.provider.SessionScopeBuildActionExecuter.execute(SessionScopeBuildActionExecuter.java:38)
at org.gradle.tooling.internal.provider.GradleThreadBuildActionExecuter.execute(GradleThreadBuildActionExecuter.java:37)
at org.gradle.tooling.internal.provider.GradleThreadBuildActionExecuter.execute(GradleThreadBuildActionExecuter.java:26)
at org.gradle.tooling.internal.provider.ParallelismConfigurationBuildActionExecuter.execute(ParallelismConfigurationBuildActionExecuter.java:43)
at org.gradle.tooling.internal.provider.ParallelismConfigurationBuildActionExecuter.execute(ParallelismConfigurationBuildActionExecuter.java:29)
at org.gradle.tooling.internal.provider.StartParamsValidatingActionExecuter.execute(StartParamsValidatingActionExecuter.java:60)
at org.gradle.tooling.internal.provider.StartParamsValidatingActionExecuter.execute(StartParamsValidatingActionExecuter.java:32)
at org.gradle.tooling.internal.provider.SessionFailureReportingActionExecuter.execute(SessionFailureReportingActionExecuter.java:55)
at org.gradle.tooling.internal.provider.SessionFailureReportingActionExecuter.execute(SessionFailureReportingActionExecuter.java:41)
at org.gradle.tooling.internal.provider.SetupLoggingActionExecuter.execute(SetupLoggingActionExecuter.java:48)
at org.gradle.tooling.internal.provider.SetupLoggingActionExecuter.execute(SetupLoggingActionExecuter.java:32)
at org.gradle.launcher.daemon.server.exec.ExecuteBuild.doBuild(ExecuteBuild.java:67)
at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
at org.gradle.launcher.daemon.server.exec.WatchForDisconnection.execute(WatchForDisconnection.java:37)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
at org.gradle.launcher.daemon.server.exec.ResetDeprecationLogger.execute(ResetDeprecationLogger.java:26)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
at org.gradle.launcher.daemon.server.exec.RequestStopIfSingleUsedDaemon.execute(RequestStopIfSingleUsedDaemon.java:34)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:74)
at org.gradle.launcher.daemon.server.exec.ForwardClientInput$2.call(ForwardClientInput.java:72)
at org.gradle.util.Swapper.swap(Swapper.java:38)
at org.gradle.launcher.daemon.server.exec.ForwardClientInput.execute(ForwardClientInput.java:72)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
at org.gradle.launcher.daemon.server.exec.LogAndCheckHealth.execute(LogAndCheckHealth.java:55)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
at org.gradle.launcher.daemon.server.exec.LogToClient.doBuild(LogToClient.java:62)
at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
at org.gradle.launcher.daemon.server.exec.EstablishBuildEnvironment.doBuild(EstablishBuildEnvironment.java:81)
at org.gradle.launcher.daemon.server.exec.BuildCommandOnly.execute(BuildCommandOnly.java:36)
at org.gradle.launcher.daemon.server.api.DaemonCommandExecution.proceed(DaemonCommandExecution.java:104)
at org.gradle.launcher.daemon.server.exec.StartBuildOrRespondWithBusy$1.run(StartBuildOrRespondWithBusy.java:50)
at org.gradle.launcher.daemon.server.DaemonStateCoordinator$1.run(DaemonStateCoordinator.java:295)
at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)
at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)
at java.lang.Thread.run(Thread.java:748)
Caused by: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:mergeExtDexDebug'.
at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:38)
at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.executeTask(EventFiringTaskExecuter.java:73)
at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:52)
at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:49)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:416)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:406)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:165)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:250)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:158)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:102)
at org.gradle.internal.operations.DelegatingBuildOperationExecutor.call(DelegatingBuildOperationExecutor.java:36)
at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter.execute(EventFiringTaskExecuter.java:49)
at org.gradle.execution.plan.LocalTaskNodeExecutor.execute(LocalTaskNodeExecutor.java:43)
at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:355)
at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:343)
at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:336)
at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:322)
at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker$1.execute(DefaultPlanExecutor.java:134)
at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker$1.execute(DefaultPlanExecutor.java:129)
at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.execute(DefaultPlanExecutor.java:202)
at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.executeNextNode(DefaultPlanExecutor.java:193)
at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.run(DefaultPlanExecutor.java:129)
... 6 more
Caused by: org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$ArtifactResolveException: Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.rethrowFailure(DefaultConfiguration.java:1195)
at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration.access$2100(DefaultConfiguration.java:138)
at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration$ConfigurationFileCollection.getFiles(DefaultConfiguration.java:1170)
at org.gradle.api.internal.file.AbstractFileCollection.iterator(AbstractFileCollection.java:72)
at org.gradle.internal.snapshot.impl.DefaultFileSystemSnapshotter$FileCollectionLeafVisitorImpl.visitCollection(DefaultFileSystemSnapshotter.java:240)
at org.gradle.api.internal.file.AbstractFileCollection.visitLeafCollections(AbstractFileCollection.java:233)
at org.gradle.api.internal.file.CompositeFileCollection.visitLeafCollections(CompositeFileCollection.java:205)
at org.gradle.internal.snapshot.impl.DefaultFileSystemSnapshotter.snapshot(DefaultFileSystemSnapshotter.java:126)
at org.gradle.internal.fingerprint.impl.AbstractFileCollectionFingerprinter.fingerprint(AbstractFileCollectionFingerprinter.java:48)
at org.gradle.api.internal.tasks.execution.DefaultTaskFingerprinter.fingerprintTaskFiles(DefaultTaskFingerprinter.java:46)
at org.gradle.api.internal.tasks.execution.ResolveBeforeExecutionStateTaskExecuter.createExecutionState(ResolveBeforeExecutionStateTaskExecuter.java:93)
at org.gradle.api.internal.tasks.execution.ResolveBeforeExecutionStateTaskExecuter.execute(ResolveBeforeExecutionStateTaskExecuter.java:73)
at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(ValidatingTaskExecuter.java:62)
at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(SkipEmptySourceFilesTaskExecuter.java:108)
at org.gradle.api.internal.tasks.execution.ResolveBeforeExecutionOutputsTaskExecuter.execute(ResolveBeforeExecutionOutputsTaskExecuter.java:67)
at org.gradle.api.internal.tasks.execution.ResolveAfterPreviousExecutionStateTaskExecuter.execute(ResolveAfterPreviousExecutionStateTaskExecuter.java:46)
at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:94)
at org.gradle.api.internal.tasks.execution.FinalizePropertiesTaskExecuter.execute(FinalizePropertiesTaskExecuter.java:46)
at org.gradle.api.internal.tasks.execution.ResolveTaskExecutionModeExecuter.execute(ResolveTaskExecutionModeExecuter.java:95)
at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:57)
at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:56)
at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:36)
... 27 more
Caused by: org.gradle.api.internal.artifacts.transform.TransformException: Failed to transform artifact 'retrofit.jar (com.squareup.retrofit2:retrofit:2.7.1)' to match attributes {artifactType=android-dex, dexing-enable-desugaring=false, dexing-is-debuggable=true, dexing-min-sdk=14, org.gradle.usage=java-runtime-jars}.
at org.gradle.api.internal.artifacts.transform.TransformingArtifactVisitor.lambda$visitArtifact$1(TransformingArtifactVisitor.java:61)
at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:191)
at org.gradle.api.internal.artifacts.transform.TransformingArtifactVisitor.visitArtifact(TransformingArtifactVisitor.java:50)
at org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ArtifactBackedResolvedVariant$SingleArtifactSet.visit(ArtifactBackedResolvedVariant.java:112)
at org.gradle.api.internal.artifacts.transform.TransformCompletion.visit(TransformCompletion.java:42)
at org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.CompositeResolvedArtifactSet$CompositeResult.visit(CompositeResolvedArtifactSet.java:83)
at org.gradle.api.internal.artifacts.ivyservice.resolveengine.artifact.ParallelResolveArtifactSet$VisitingSet.visit(ParallelResolveArtifactSet.java:64)
at org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration.visitArtifacts(DefaultLenientConfiguration.java:256)
at org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration.access$500(DefaultLenientConfiguration.java:69)
at org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$2.run(DefaultLenientConfiguration.java:231)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:402)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:394)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:165)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:250)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:158)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:92)
at org.gradle.internal.operations.DelegatingBuildOperationExecutor.run(DelegatingBuildOperationExecutor.java:31)
at org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration.visitArtifactsWithBuildOperation(DefaultLenientConfiguration.java:228)
at org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration.access$200(DefaultLenientConfiguration.java:69)
at org.gradle.api.internal.artifacts.ivyservice.DefaultLenientConfiguration$1.visitArtifacts(DefaultLenientConfiguration.java:133)
at org.gradle.api.internal.artifacts.configurations.DefaultConfiguration$ConfigurationFileCollection.getFiles(DefaultConfiguration.java:1167)
... 46 more
Caused by: org.gradle.api.internal.artifacts.transform.TransformException: Execution failed for DexingNoClasspathTransform: /home/espl/.gradle/caches/modules-2/files-2.1/com.squareup.retrofit2/retrofit/2.7.1/ab61c867c73bdf57224bcc40525f42fea72960a0/retrofit-2.7.1.jar.
at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvoker.lambda$invoke$1(DefaultTransformerInvoker.java:172)
at org.gradle.internal.Try$Failure.mapFailure(Try.java:182)
at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvoker.lambda$invoke$2(DefaultTransformerInvoker.java:172)
at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvoker.fireTransformListeners(DefaultTransformerInvoker.java:219)
at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvoker.lambda$invoke$3(DefaultTransformerInvoker.java:117)
at org.gradle.api.internal.artifacts.transform.ImmutableTransformationWorkspaceProvider.lambda$withWorkspace$0(ImmutableTransformationWorkspaceProvider.java:81)
at org.gradle.cache.internal.LockOnDemandCrossProcessCacheAccess.withFileLock(LockOnDemandCrossProcessCacheAccess.java:90)
at org.gradle.cache.internal.DefaultCacheAccess.withFileLock(DefaultCacheAccess.java:194)
at org.gradle.cache.internal.DefaultPersistentDirectoryStore.withFileLock(DefaultPersistentDirectoryStore.java:170)
at org.gradle.cache.internal.DefaultCacheFactory$ReferenceTrackingCache.withFileLock(DefaultCacheFactory.java:194)
at org.gradle.api.internal.artifacts.transform.ImmutableTransformationWorkspaceProvider.withWorkspace(ImmutableTransformationWorkspaceProvider.java:76)
at org.gradle.api.internal.artifacts.transform.AbstractCachingTransformationWorkspaceProvider.lambda$withWorkspace$0(AbstractCachingTransformationWorkspaceProvider.java:54)
at com.google.common.cache.LocalCache$LocalManualCache$1.load(LocalCache.java:4717)
at com.google.common.cache.LocalCache$LoadingValueReference.loadFuture(LocalCache.java:3444)
at com.google.common.cache.LocalCache$Segment.loadSync(LocalCache.java:2193)
at com.google.common.cache.LocalCache$Segment.lockedGetOrLoad(LocalCache.java:2152)
at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2042)
at com.google.common.cache.LocalCache.get(LocalCache.java:3850)
at com.google.common.cache.LocalCache$LocalManualCache.get(LocalCache.java:4712)
at org.gradle.api.internal.artifacts.transform.AbstractCachingTransformationWorkspaceProvider.withWorkspace(AbstractCachingTransformationWorkspaceProvider.java:53)
at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvoker.invoke(DefaultTransformerInvoker.java:116)
at org.gradle.api.internal.artifacts.transform.TransformationStep.lambda$transform$0(TransformationStep.java:104)
at org.gradle.internal.Try$Success.flatMap(Try.java:102)
at org.gradle.api.internal.artifacts.transform.TransformationStep.transform(TransformationStep.java:101)
at org.gradle.api.internal.artifacts.transform.TransformationNode$ChainedTransformationNode$1.lambda$transform$0(TransformationNode.java:191)
at org.gradle.internal.Try$Success.flatMap(Try.java:102)
at org.gradle.api.internal.artifacts.transform.TransformationNode$ChainedTransformationNode$1.transform(TransformationNode.java:190)
at org.gradle.api.internal.artifacts.transform.TransformationNode$ArtifactTransformationStepBuildOperation.call(TransformationNode.java:229)
at org.gradle.api.internal.artifacts.transform.TransformationNode$ArtifactTransformationStepBuildOperation.call(TransformationNode.java:212)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:416)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$CallableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:406)
at org.gradle.internal.operations.DefaultBuildOperationExecutor$1.execute(DefaultBuildOperationExecutor.java:165)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:250)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:158)
at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:102)
at org.gradle.internal.operations.DelegatingBuildOperationExecutor.call(DelegatingBuildOperationExecutor.java:36)
at org.gradle.api.internal.artifacts.transform.TransformationNode$ChainedTransformationNode.execute(TransformationNode.java:187)
at org.gradle.api.internal.artifacts.transform.TransformationNodeExecutor.execute(TransformationNodeExecutor.java:37)
... 15 more
Caused by: com.android.builder.dexing.DexArchiveBuilderException: Error while dexing.
The dependency contains Java 8 bytecode. Please enable desugaring by adding the following to build.gradle
android {
    compileOptions {
        sourceCompatibility 1.8
        targetCompatibility 1.8
    }
}
See https://developer.android.com/studio/write/java8-support.html for details. Alternatively, increase the minSdkVersion to 26 or above.

at com.android.builder.dexing.D8DexArchiveBuilder.getExceptionToRethrow(D8DexArchiveBuilder.java:124)
at com.android.builder.dexing.D8DexArchiveBuilder.convert(D8DexArchiveBuilder.java:101)
at com.android.build.gradle.internal.dependency.BaseDexingTransform$transform$1.invoke(DexingTransform.kt:90)
at com.android.build.gradle.internal.dependency.BaseDexingTransform$transform$1.invoke(DexingTransform.kt:48)
at com.android.build.gradle.internal.tasks.BlocksUtilsKt.recordArtifactTransformSpan(BlocksUtils.kt:33)
at com.android.build.gradle.internal.dependency.BaseDexingTransform.transform(DexingTransform.kt:67)
at org.gradle.api.internal.artifacts.transform.DefaultTransformer.transform(DefaultTransformer.java:173)
at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvoker$TransformerExecution.execute(DefaultTransformerInvoker.java:274)
at org.gradle.internal.execution.steps.ExecuteStep.lambda$execute$1(ExecuteStep.java:33)
at java.util.Optional.orElseGet(Optional.java:267)
at org.gradle.internal.execution.steps.ExecuteStep.execute(ExecuteStep.java:33)
at org.gradle.internal.execution.steps.ExecuteStep.execute(ExecuteStep.java:26)
at org.gradle.internal.execution.steps.CleanupOutputsStep.execute(CleanupOutputsStep.java:58)
at org.gradle.internal.execution.steps.CleanupOutputsStep.execute(CleanupOutputsStep.java:35)
at org.gradle.internal.execution.steps.ResolveInputChangesStep.execute(ResolveInputChangesStep.java:48)
at org.gradle.internal.execution.steps.ResolveInputChangesStep.execute(ResolveInputChangesStep.java:33)
at org.gradle.internal.execution.steps.CancelExecutionStep.execute(CancelExecutionStep.java:39)
at org.gradle.internal.execution.steps.TimeoutStep.executeWithoutTimeout(TimeoutStep.java:73)
at org.gradle.internal.execution.steps.TimeoutStep.execute(TimeoutStep.java:54)
at org.gradle.internal.execution.steps.CatchExceptionStep.execute(CatchExceptionStep.java:35)
at org.gradle.internal.execution.steps.CreateOutputsStep.execute(CreateOutputsStep.java:51)
at org.gradle.internal.execution.steps.SnapshotOutputsStep.execute(SnapshotOutputsStep.java:45)
at org.gradle.internal.execution.steps.SnapshotOutputsStep.execute(SnapshotOutputsStep.java:31)
at org.gradle.internal.execution.steps.CacheStep.executeWithoutCache(CacheStep.java:201)
at org.gradle.internal.execution.steps.CacheStep.execute(CacheStep.java:70)
at org.gradle.internal.execution.steps.CacheStep.execute(CacheStep.java:45)
at org.gradle.internal.execution.steps.BroadcastChangingOutputsStep.execute(BroadcastChangingOutputsStep.java:49)
at org.gradle.internal.execution.steps.StoreSnapshotsStep.execute(StoreSnapshotsStep.java:43)
at org.gradle.internal.execution.steps.StoreSnapshotsStep.execute(StoreSnapshotsStep.java:32)
at org.gradle.internal.execution.steps.RecordOutputsStep.execute(RecordOutputsStep.java:38)
at org.gradle.internal.execution.steps.RecordOutputsStep.execute(RecordOutputsStep.java:24)
at org.gradle.internal.execution.steps.SkipUpToDateStep.executeBecause(SkipUpToDateStep.java:96)
at org.gradle.internal.execution.steps.SkipUpToDateStep.lambda$execute$0(SkipUpToDateStep.java:89)
at java.util.Optional.map(Optional.java:215)
at org.gradle.internal.execution.steps.SkipUpToDateStep.execute(SkipUpToDateStep.java:54)
at org.gradle.internal.execution.steps.SkipUpToDateStep.execute(SkipUpToDateStep.java:38)
at org.gradle.internal.execution.steps.ResolveChangesStep.execute(ResolveChangesStep.java:77)
at org.gradle.internal.execution.steps.ResolveChangesStep.execute(ResolveChangesStep.java:37)
at org.gradle.internal.execution.steps.legacy.MarkSnapshottingInputsFinishedStep.execute(MarkSnapshottingInputsFinishedStep.java:36)
at org.gradle.internal.execution.steps.legacy.MarkSnapshottingInputsFinishedStep.execute(MarkSnapshottingInputsFinishedStep.java:26)
at org.gradle.internal.execution.steps.ResolveCachingStateStep.execute(ResolveCachingStateStep.java:90)
at org.gradle.internal.execution.steps.ResolveCachingStateStep.execute(ResolveCachingStateStep.java:48)
at org.gradle.internal.execution.impl.DefaultWorkExecutor.execute(DefaultWorkExecutor.java:33)
at org.gradle.api.internal.artifacts.transform.DefaultTransformerInvoker.lambda$invoke$2(DefaultTransformerInvoker.java:148)
... 50 more
Caused by: com.android.tools.r8.CompilationFailedException: Compilation failed to complete
at com.android.tools.r8.utils.t.a(:55)
at com.android.tools.r8.D8.run(:11)
at com.android.builder.dexing.D8DexArchiveBuilder.convert(D8DexArchiveBuilder.java:99)
... 92 more
Caused by: com.android.tools.r8.utils.AbortException: Error: Invoke-customs are only supported starting with Android O (--min-api 26)
at com.android.tools.r8.utils.Reporter.a(:21)
at com.android.tools.r8.utils.Reporter.a(:7)
at com.android.tools.r8.utils.t.a(:36)
... 94 more

android kotlin Create a HTTP Request without using any library

In this blog article, we will see how to create a HTTP Request without using any third party library.
In Android App Development , we usually depend on api on server with backend database and to create a connection with the server, we need to create HTTP Requests.
For API integration purpose we generally use popular networking libraries like Volley, Retrofit etc. in our project so that we can easily create the http requests for our use cases.
Of course, these libraries help us a lot to make our work easy and our HTTP Request calls faster.
But what if we don’t want to use any of these third party libraries.

Android - HttpURLConnection example

Let us see how to do this in Android and Kotlin:
Kotlin Code Approach:
  1. Create an suspend fun request with response type String
  2. Add suspendCancellableCoroutine that will resume on response of the HTTP API request and resumeWithException in case of any exception in API Call.
  3. There is no need to use the callback interface in case of the Kotlin Couroutines.
Kotlin Code
NetworkUtilityK.kt
package com.example.myapplication

import kotlinx.coroutines.suspendCancellableCoroutine
import org.json.JSONObject
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.Reader
import java.net.HttpURLConnection
import java.net.URL
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException

class NetworkUtilityK {
 
 companion object {

        suspend fun request(pageno: Int): String{
            return suspendCancellableCoroutine { continuation ->
                try {
                    val reader: BufferedReader
                    val url = URL("https://reqres.in/api/users?page=" + pageno + "&per_page=6")

                    with(url.openConnection() as HttpURLConnection) {
                        requestMethod = "GET"
                        reader = BufferedReader(InputStreamReader(inputStream) as Reader?)

                        val response = StringBuffer()
                        var inputLine = reader.readLine()
                        while (inputLine != null) {
                            response.append(inputLine)
                            inputLine = reader.readLine()
                        }
                        reader.close()

                        if (continuation.isActive) {
                            continuation.resume(response.toString())
                        }
                    }

                } catch (e: Exception) {
                    e.printStackTrace()
                    if (continuation.isActive) {
                        continuation.resumeWithException(e)
                    }
                }
            }
        }
    }
}

Calling the suspend fun request(pageno: Int)  inside Activity's onCreate() method :
class MainActivity : AppCompatActivity() {

    internal var page_count_dynamic = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding: ActivityMainBinding =
            DataBindingUtil.setContentView(this, R.layout.activity_main)

        setSupportActionBar(toolbar)
        getSupportActionBar()?.setDisplayHomeAsUpEnabled(true);

        viewModel = ViewModelProviders.of(this)[MainViewModel::class.java]
        binding.mainVM = viewModel
        binding.setLifecycleOwner(this);

        CoroutineScope(Dispatchers.IO).launch{
            val rss = NetworkUtilityK.request(page_count_dynamic )
            withContext(Dispatchers.Main) {
                // call to UI thread and parse response
                val newlist=viewModel.parseJsonString(rss.toString())
                Log.e("ResSizedata","--list"+1+"::" + newlist.size+"::"+newlist)
            }
        }
}
}

Calling the suspend fun request(pageno: Int)  inside ViewModel :
class MainViewModel : ViewModel() {

    fun getData(page: Int) {
            viewModelScope.launch(Dispatchers.IO) {
                val rss = NetworkUtilityK.request(page)
                withContext(Dispatchers.Main) {
                    // call to UI thread
       
                    val newlist=parseJsonString(rss.toString())
                     
                }
        }
    }
 }

Android Code
NetworkUtility.java
package com.example.myapplication;

import android.os.AsyncTask;
import android.util.Log;

import org.json.JSONObject;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

public class NetworkUtility {
    public static void getdata(APICallback apiCallback) {
        new MyHttpRequestTask(apiCallback).execute();
    }

    static class MyHttpRequestTask extends AsyncTask<String, Integer, String> {
        APICallback apiCallback;

        public MyHttpRequestTask(APICallback apiCallback) {
            this.apiCallback = apiCallback;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            this.apiCallback.onSucess(s);
        }

        @Override
        protected String doInBackground(String... params) {
            BufferedReader reader;
            String res = null;
            try {
                URL url = new URL("https://reqres.in/api/users?page=" + pageno + "&per_page=6");
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                // setting the  Request Method Type
                httpURLConnection.setRequestMethod("GET");

                try {
                    reader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                    StringBuffer response = new StringBuffer();
                    String inputLine;
                    while ((inputLine = reader.readLine()) != null) {
                        response.append(inputLine);
                    }
                    reader.close();
                    // to log the response code of your request
                    Log.d("MyApp", "MyHttpRequestTask doInBackground : " + httpURLConnection.getResponseCode());

                    res = response.toString();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    // this is done so that there are no open connections left when this task is going to complete
                    httpURLConnection.disconnect();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return res;
        }
    }
}

Popular Posts