I've got following error while trying to build prject with MVVM and data binding. I've searched through everything I could find here on StackOverflow, or common mistakes on the internet, but nothing worked for my case.
The only message I receive is this error. It looks like this in buildOutput:
I've had my packages named with capital letters and I've found here that this might be the cause because compiler treat them as names of classes, so I've changed them all to start with small letters, but that haven't helped.
I've created ViewModelFactory for creating my ViewModel inside Activity so I could send additional parameters with constructor using factory, so I've tried to remove it and use no parameters and create instance without using factory for this purpose, but still I havn't got any results (same error)
I was chaning both build.gradle's in different ways but result was always the same.
Finally I've deleted data binding and variable from XML, then I was able to run the app (with other errors, but I would be probably able to deal with those by myself) but I want to leave it as it is and just deal with my error.
I am not experienced with MVVM and data binding so it can be just a stupid mistake, but it is hard to find it if I don't know where I should look for it.
Here I post most important codes, if you need more then let me know:
build.grale(app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.rickmorty"
minSdkVersion 24
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures{
dataBinding = true
viewBinding = true
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.3.0-alpha02'
implementation "android.arch.lifecycle:extensions:1.1.1"
implementation "androidx.cardview:cardview:1.0.0"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.1'
implementation 'io.reactivex.rxjava2:rxjava:2.1.7'
implementation 'com.github.bumptech.glide:glide:4.6.1'
kapt 'com.github.bumptech.glide:compiler:4.4.0'
}
**build.gradle(project)
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
MainActivity
class MainActivity : AppCompatActivity() {
lateinit var mainViewModel: MainViewModel
lateinit var mAdapter: CharactersAdapter
lateinit var api: CharacterAPI
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val retrofit = RetrofitClient.instance
api = retrofit.create(CharacterAPI::class.java)
setupViewModel()
setupRecycler()
mainViewModel.getData().observe(this,
Observer<List<Results>> { t ->
mAdapter = CharactersAdapter(this#MainActivity, t!!)
rvCharacters.adapter = mAdapter
})
}
fun setupRecycler() {
val lManager = LinearLayoutManager(this#MainActivity)
rvCharacters.apply {
setHasFixedSize(true)
layoutManager = lManager
}
rvCharacters.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val visibleItems = lManager.childCount
val totalItems = lManager.itemCount
val firstVisible = lManager.findFirstVisibleItemPosition()
if (dy > 0) {
if (!mainViewModel.isLoading.value!! && (visibleItems + firstVisible) >= totalItems) {
mainViewModel.scrolledNext()
}
} else {
if (!mainViewModel.isLoading.value!! && (totalItems - visibleItems) <= 0) {
mainViewModel.scrolledPrev()
}
}
}
})
}
fun setupViewModel() {
mainViewModel = ViewModelProviders.of(this, MainViewModelFactory(application, api))
.get(MainViewModel::class.java)
DataBindingUtil.setContentView<ActivityMainBinding>(
this, R.layout.activity_main
).apply {
lifecycleOwner = this#MainActivity
viewmodel = mainViewModel
}
}
}
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="viewmodel"
type="com.example.rickmorty.ViewModel.MainViewModel"/>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RelativeLayout
android:id="#+id/rlPageTitleHolder"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHeight_percent="0.1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_centerInParent="true"
android:text="#{() -> viewmodel.pageNumber}"/>
</RelativeLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvCharacters"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#id/rlPageTitleHolder"
app:layout_constraintHeight_percent="0.9"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
As I wrote earlier, after removing variable from XML and binding from MainActivity error disappears.
I know it is a lot of code, so if something is redundant just let me know. There is factory class still missing, but I will post it if it's necessary. Also MainRepo is one I haven't attached here, but it's quite long, but I can post it all if you're gonna need it.
Here is the problem --> android:text="#{() -> viewmodel.pageNumber}"
correct syntax for assigning value using dataBinding is
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:layout_centerInParent="true"
android:text="#{viewmodel.pageNumber}"/>
Related
I keep getting errors: "Unresolved reference: databinding" and "Unresolved reference: ActivityMainBinding".
I am using Android Studio 4.0, Android Gradle Plugin Version: 4.0.0 and Gradle Version 6.1.1.
build.gradle (Module: app):
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.twowaydatabinding"
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
buildFeatures {
dataBinding = true
viewBinding = true
}
}
dependencies {
def lifecycle_version = "2.2.0"
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.0'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
// LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
}
build.gradle (Project):
buildscript {
ext.kotlin_version = "1.3.72"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
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="viewModel"
type="com.example.twowaydatabinding.MainActivityViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- layout content -->
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
MainActivity.kt:
package com.example.twowaydatabinding
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
import com.example.twowaydatabinding.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
private lateinit var viewModel : MainActivityViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
viewModel = ViewModelProvider(this).get(MainActivityViewModel::class.java)
binding.viewModel = viewModel
binding.lifecycleOwner = this
}
}
MainActivityViewModel.xml:
package com.example.twowaydatabinding
import androidx.databinding.Bindable
import androidx.databinding.Observable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
class MainActivityViewModel : ViewModel(), Observable {
#Bindable
val userName = MutableLiveData<String>()
init {
userName.value = "Frank"
}
override fun removeOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
TODO("Not yet implemented")
}
override fun addOnPropertyChangedCallback(callback: Observable.OnPropertyChangedCallback?) {
TODO("Not yet implemented")
}
}
I tried:
Build -> Clean Project, then Build -> Rebuild Project
File -> Invalidate Caches / Restart
but nothing helps. Class ActivityMainBinding is never generting.
When I go to declaration of
import com.example.twowaydatabinding.databinding.ActivityMainBinding in MainActivity.kt it redirects me to activity_main.xml. Code doesn't show any errors, but when I try to build the project those databinding errors shows up.
What am I doing wrong?
Instead Of this line
buildFeatures {
dataBinding = true
viewBinding = true
}
Write this line
buildFeatures {
dataBinding true
viewBinding true
}
Try this in main Activity,
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
This worked for me
1) Delete ".gradle" folder , delte ".idea" folder and delete build folder under app folder
2) Click on "File -> Invalidate Caches / Restart"
Add data binding like this in your build.gradle(Module:app)
defaultConfig {
dataBinding {
enabled true
}
}
I am building a library module that using coroutines.
My library do the following:
get config from the app module
create a fragment that implemented CoroutineScope (ContentListingFragment)
ContentListingFragment handle all it's process fetching data from the network and show them
from the app module:
We should able to get an instance from the ContentListingFragment and add it to a container
The issue: when I am building the app, I am getting the following error:
Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
class nabed.apps.nabedutilslibrary.ui.base.ContentListingFragment, unresolved supertypes: kotlinx.coroutines.CoroutineScope
below is the library module build.gradle
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"
apply plugin: 'androidx.navigation.safeargs'
android {
compileSdkVersion 28
buildToolsVersion "29.0.0"
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = "1.8"
}
}
}
androidExtensions{
experimental = true
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
// Navigation
implementation "android.arch.navigation:navigation-fragment:$navigation_version"
implementation "android.arch.navigation:navigation-ui:$navigation_version"
implementation "android.arch.navigation:navigation-fragment-ktx:$navigation_version"
implementation "android.arch.navigation:navigation-ui-ktx:$navigation_version"
//Kotlin Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0-RC1"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
How to handle this without changes on the app module?
You have to change the dependencies declaration in your library's build.gradle:
//Kotlin Coroutines
api "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.0"
api "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.0-RC1"
api keyword will expose these dependencies to the app module
****put this code in build.gradel file in dependencies block and click on sync****
//Kotlin Coroutines
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.0"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0"
If you are not going to use Coroutines in the app module but would like to use it in the library, decision with api dependencies not the best one. In this way your app module will be also dependent on Coroutines.
I think, better to remove CoroutineScope implementation from ContentListingFragment and create CoroutineScope object as a ContentListingFragment member.
class ContentListingFragment : Fragment() {
private val job = Job()
private val coroutineScope = object : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = job + Dispatchers.IO
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
coroutineScope.launch { ... }
}
override fun onDestroyView() {
super.onDestroy()
job.cancelChildren()
}
}
Also, LifecycleScope form lifecycle-runtime-ktx may be a the easiest solution. It will remove CoroutineScope creation boilerplate code.
class ContentListingFragment : Fragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewLifecycleOwner.lifecycleScope.launch { ... }
}
}
I am trying to communicate with viewmodel from xml and vice versa using MVVM pattern. I have worked on databinding before and successfully worked with Live Data - Dagger - MVVM .
Recently, I have tried to create a new project and since then I cannot track the response with XML and viewmodel. Neither the onClick from XML -> ViewModel nor the assigning value to the textview from View -> XML is working. But there is no crash or anything, just it is not working. I have added all associated files [ MainActivity, activity_main, viewModel, Dagger Module, build.gradle ]
I will really appreciate if anyone can tell me what is going wrong here.
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="viewmodel"
type="aveek.com.vm.ui.home.MainActivityViewModel"/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#{viewmodel.balanceText}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:clickable="true"
android:onClick="#{() -> viewmodel.clickData()}"
app:layout_constraintTop_toTopOf="parent" />
...
</layout>
MainActivity.kt
class MainActivity : AppCompatActivity(),LifecycleOwner {
#Inject
lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding.setLifecycleOwner(this)
with(binding){
this.viewmodel?.let {
it.balanceText.set( "Aveek testing")
it.data.observe(this#MainActivity, Observer {
Toast.makeText(this#MainActivity, "Data is now : $it", Toast.LENGTH_SHORT).show()
})
}
}
}
MainActivityViewModel.kt
class MainActivityViewModel : ViewModel() {
val data = MutableLiveData<Boolean>()
val balanceText = ObservableField<String>()
fun clickData(){
data.value = false
}
}
MainActivityModule.kt
#Module
class MainActivityModule{
/**
* provides binding to Main Activity from respective XML
* #property viewModel
* #property context
* #return binding of the view
*/
#Provides
fun binding(context: MainActivity, viewModel : MainActivityViewModel) :
ActivityMainBinding {
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(context,
R.layout.activity_main)
binding.viewmodel = viewModel
return binding
}
}
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 27
defaultConfig {
applicationId "aveek.test"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
dataBinding {
enabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
kapt {
generateStubs = true
}
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.3'
implementation 'com.android.support:multidex:1.0.2'
implementation 'com.android.support:design:27.1.1'
implementation "android.arch.lifecycle:extensions:1.1.0"
// annotationProcessor "android.arch.lifecycle:compiler:1.1.0"
kapt "com.android.databinding:compiler:$androidPluginVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$daggerVersion"
implementation "com.google.dagger:dagger:$daggerVersion"
implementation "com.google.dagger:dagger-android:$daggerVersion"
implementation "com.google.dagger:dagger-android-support:$daggerVersion"
kapt "com.google.dagger:dagger-compiler:$daggerVersion"
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'
}
ext{
kotlin_version = '1.2.31'
androidPluginVersion = '3.1.0'
daggerVersion = '2.13'
}
you won't be able to use any generated methods to set your variable to the binding, as there's no common superclass besides the ViewDataBinding,so you will be forced to use reflection, or you can use the convenience method setVariable():
binding.setVariable(BR.viewModel, viewModel);
As I suspected, the problem is with Dagger injection. Maybe I have implemented in a wrong approach but the viewmodel is perfectly fine if running without DI.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProviders.of(this).get(MainActivityViewModel::class.java)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.viewmodel = viewModel
binding.setLifecycleOwner(this)
mLifecycleRegistry = LifecycleRegistry(this).apply {
markState(Lifecycle.State.CREATED)
}
with(binding){
this.viewmodel?.let {
it.balanceText.set( "Aveek testing")
it.data.observe(this#MainActivity, Observer {
Toast.makeText(this#MainActivity, "Data is now : $it",
Toast.LENGTH_SHORT).show()
})
}
}
}
So, I have gone through the DI code and found out that in MainActivity.onCreate()
I was doing this
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
which basically overriding the databinding behaviour. So I have updated the code like this
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main)
// and initiated binding here as injecting binding from module before setting content won't be effective
val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)
And removed
// #Inject
// lateinit var binding : ActivityMainBinding
Now it works perfectly fine..
I'm trying to build an upload application modeled off this tutorial: https://codelabs.developers.google.com/codelabs/kotlin-coroutines/#4
I'm setting a breakpoint on line 29 (where I've commented "I never reach this breakpoint. Why not?"). Why don't I reach this breakpoint when I click on the button?
MainActivity.kt
package com.example.uploadwithprogresssimple
import android.arch.lifecycle.*
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.constraint.ConstraintLayout
import android.support.design.widget.Snackbar
import android.widget.TextView
import kotlinx.coroutines.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val viewModel = ViewModelProviders.of(this)
.get(MainViewModel::class.java)
val scheduler: TextView = findViewById(R.id.scheduleUpload)
val rootLayout: ConstraintLayout = findViewById(R.id.rootLayout)
scheduler.setOnClickListener {
viewModel.uploadVideo("/foobar/abc/def")
}
viewModel.status.observe( this, Observer {
// I never reach this breakpoint. Why not?
if (it != null) {
var size = it.size
Snackbar.make(rootLayout, "Size of video upload: ${size}", Snackbar.LENGTH_SHORT).show()
}
})
}
}
data class VideoAsset(private val filename: String)
class MainViewModel : ViewModel() {
private val viewModelJob = Job()
private val uiScope = CoroutineScope(Dispatchers.Main + viewModelJob)
private val _status = MutableLiveData<ArrayList<VideoAsset>>()
val status: LiveData<ArrayList<VideoAsset>>
get() = _status
override fun onCleared() {
super.onCleared()
uiScope.cancel()
}
fun uploadVideo(filename: String) {
uiScope.launch {
delay(1_000)
_status.value?.add( VideoAsset( filename) )
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootLayout"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Schedule upload"
android:id="#+id/scheduleUpload"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>
The app/build.gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.uploadwithprogresssimple"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
def kotlinCoroutines = "1.1.0"
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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'
implementation 'android.arch.lifecycle:extensions:1.1.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$kotlinCoroutines"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$kotlinCoroutines"
}
In this case breakpoint is not reached because setValue(T) method of LiveData object is not called. Calling setValue(T) results in the observers calling their onChanged() method. Try to change your code to call setValue(T) method, i.e.:
val list = _status.value ?: arrayListOf<VideoAsset>()
list.add(VideoAsset(filename))
_status.value = list
Note: setValue(T) in Kotlin is replaced with property assigning: _status.value = list instead of _status.setValue(list)
I have a layout file called activity_suggestions. I am using databinding in it. Hence the file ActivitySuggestionsBinding got generated. The project compiles successfully. But when I try to run the project, I get this error
e: error: cannot access ActivitySuggestionsBinding
I am using android studio 3.1.2 with kotlin version 1.4.1. Any help will be appreciated
Edit
Pasting my module level build.gradle and app level build.gradle
Module Build.gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android {
dataBinding {
enabled = true
}
..
}
dependencies{
..
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "com.google.dagger:dagger:$rootProject.daggerVersion"
implementation "com.google.dagger:dagger-android:$rootProject.daggerVersion"
implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
provided 'javax.annotation:jsr250-api:1.0'
implementation "android.arch.lifecycle:runtime:$rootProject.archVersion"
implementation "android.arch.lifecycle:extensions:$rootProject.archVersion"
annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archVersion"
kapt "com.android.databinding:compiler:3.1.2"
..
}
App build.gradle
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
android{
dataBinding{
enabled = true
}
..
}
dependencies{
compile project(':module')
kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
kapt "com.android.databinding:compiler:3.1.2"
..
}
This is the activity where I am accessing ActivitySuggestionsBinding. This compiles without any error.
class SuggestionsActivityScreen : BaseActivity() {
var binding : ActivitySuggestionsBinding? = null
override fun onCreate(savedInstanceState: Bundle?) {
AndroidInjection.inject(this)
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this,R.layout.activity_suggestions)
binding?.model = SuggestionActivityViewModel()
}
}
On compiling the base module (app), this is the error I get
error: cannot access ActivitySuggestionsBinding
class file for com.dom.comp.databinding.ActivitySuggestionsBinding not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for com.dom.comp.databinding.ActivitySuggestionsBinding not found
This is my activity_suggestions.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:card_view="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="model"
type="com.dom.domp.SuggestionActivityViewModel"/>
</data>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:focusableInTouchMode="true"
android:padding="#dimen/step1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="#{model.namedString}"/>
</RelativeLayout>
</layout>
And I have tried clean, invalidate cache. These don't solve the problem.
In this demo it is work and try this way..
add only databinding enable code into app level gradle file like below code..
dataBinding {
enabled = true
}
below code is my app level gradle file ..
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.example.adruser.rafdemo"
minSdkVersion 23
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dataBinding {
enabled = true
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.google.android.gms:play-services-maps:11.6.0'
implementation 'com.google.android.gms:play-services-location:11.6.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.intuit.sdp:sdp-android:1.0.4'
implementation 'com.github.bumptech.glide:glide:4.7.1'
}
repositories {
mavenCentral()
}
then after in project level gradle.properties file add below line..
android.databinding.enableV2=true
make user_layout.xml file like below code..
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="user"
type="com.example.adruser.rafdemo.model.User"/>
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text='#{user.name}'/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="#+id/ulTvName"
android:text="#{user.dob}"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="#+id/ulTvAge"
android:text="#{Integer.toString(user.age)}"
app:layout_constraintTop_toBottomOf="#+id/ulTvName"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20dp"
android:id="#+id/ulTvDob"
android:text="#{user.dob ?? user.expDob}"
app:layout_constraintTop_toBottomOf="#+id/ulTvAge"
/>
</android.support.constraint.ConstraintLayout>
</layout>
then after make User.java pojo class like below code.. you can make .kt class pojo also
public class User {
public String name,dob,expDob;
public int age;
public User(){}
public User(String name, String dob,String expDob, int age) {
this.name = name;
this.dob = dob;
this.age = age;
this.expDob=expDob;
}
public static String display(){
return "rajesh";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
then finnaly make DatabindingActivity.kt class for binding ..
class DatabindingActivity :AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var binding:UserLayoutBinding=DataBindingUtil.setContentView(this,R.layout.user_layout)
val user = User("karan", null, "25/06/1994", 24)
binding.user=user
}
}
Add following in you app build.gradle
kapt "com.android.databinding:compiler:$android_plugin_version"
apply plugin: 'kotlin-kapt' // This one at top where plugin belong to
This will do the trick.
$android_plugin_version is version of com.android.tools.build:gradle in application build.gradle
Also, add this to your module build.gradle
android {
/// Existing Code
kapt {
generateStubs = true
}
}
I had similar issue.
I was getting error
error: cannot access RepeatPaymentFragmentBinding
class file for RepeatPaymentFragmentBinding not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for my.package.databinding.RepeatPaymentFragmentBinding not found
Spent a couple of days to figure out that the problem is not with GDB but with Dagger2. It turned out, Dagger doesn't support using generated classes in code it generates.
So, I had such class in my :library module:
class RepeatPaymentFragment: BaseFragment<RepeatPaymentFragmentBinding>() {
...
}
And the dagger component for it were located in :app module. So the two annotation processors interfere each other.
After I removed generic from my fragments it compiles fine.
So if you have such error, try to look if you try to inject into (or provide) class which is generated by another annotation proccessor, or inherits from such class, or have generic with such class.