I am trying to move a large scale app to view binding. we are using dagger.android at a lot of areas, so i am guessing the View Binding and dagger are not faring well. here is the error i am getting :
Type net.myapp.base.abstracts.vb.BaseFragmentVB_MembersInjector is defined multiple times:
Users/anshsachdeva/Desktop/android/myapp-android/feature/search/build/intermediates/runtime_library_classes_jar/stagingDebug/classes.jar:net/myapp/base/abstracts/vb/BaseFragmentVB_MembersInjector.class, /
Users/anshsachdeva/Desktop/android/myapp-android/feature/flashcard/build/intermediates/runtime_library_classes_jar/stagingDebug/classes.jar:net/myapp/base/abstracts/vb/BaseFragmentVB_MembersInjector.class
Some Context:
The app uses the classic clean architecture, where all the child activities, fragments, etc extend from the base classes . the project is also having multiple feature modules like app ,base, feature/search, feature/flashcard,domain , etc . The base classes reside in base module, while the classes extending from them resides in their associated module like feature/search.
To support view binding, i added some intermediate abstract classes that would act as holders of Viewbinding :passing an inflated view to parent while handling the lifecycle of viewbinding instance and providing safe access to children, both at the same time. i kept those in base class module only.
now everything was working well and the children classes are able to access the base View binding classes, but when i try to build the project, this error occurs. I am not sure why is this happening?
(PS:I work on several branches. the view binding code was in one of the branches. last week i made this code, built it and run , and it was working. Then i switched branches and started working on other things. this week, i come back to the same branch and code does not build )
Thankyou everyone. I changed my build environment from staging to debug and it worked. Now need to work on the newer problem of why the build environment is not working.
Related
In kotlin to access an element in the layout you do not need to do:
val k = findViewById(R......)
The element should be automatically imported into the kotlin file that connected to the view F.E if you have a text element in your main activity, in kotlin you access it by using it's id directly in the file that inflates that view and the import should be added automatically like this.
import kotlinx.android.synthetic.main.activity_main.view.*
However in few recent projects I noticed that this does not happen automatically.
You are correct, the Kotlin synthetics have quite a lot of similarities to the View Binding feature. The main advantage view binding has is that it's a bit harder to use the wrong binding class (with synthetics, you could quite easily import the wrong synthetics class if you have views in multiple layouts with the same ID).
I believe it's deprecated for a number of reasons, namely the type safety issue and the fact that it doesn't work with Java.
I've wrote a short tutorial on view binding here.
After some digging I found that Kotlin Android Extensions is now deprecated. Here is the guide to use the new method.
https://developer.android.com/topic/libraries/view-binding
Another option is to add the plugin in the app.gradle file.
plugins {
...
id 'kotlin-android-extensions'
}
After doing this two questions comes to mind:
Why did they remove the imports?
isn't the method that they demonstrated just doing the exact same thing as in it's importing the whole view?
I am using MVVM android architecture for my application.
I want to implement click event, so, do we need to use data binding architecture components or we can just use activity for handling the click event and validating the user inputs?
What's the best way to implement this?
Well the question would be using the Databinding or not. Here are some pros and cons of Databinding:
Pros:
Makes the code super clean.
Makes the code shorter.
Easy to test.
Cons:
Sometimes it's hard to debug.
It's a little heavy and increases the compile time.
But... since Google has already anounced it as part of the Android Architecture components I believe you should use it.
What's the best way to implement this?
I don't know how familiar you are with the Databinding but you should know something about Binding Adapters, anw in the onClick you won't be needing it. Just add the android:onClick attribute in the XML file. Also you can find this Codelab to properly implement it:
https://codelabs.developers.google.com/codelabs/android-databinding/#0
Example:
First of all make sure you have the Databinding enabled in your build.gradle
android {
...
dataBinding {
enabled true
}
}
After that go to the layout you will use the databinding (and for that make sure it will be an Activity/Fragment), and just type ALT+ TAB in your IDE and than....
After that, define types, for example a ViewModel and it's name. And in the view that will use the click function add what I said above. (android:onClick="#{() -> viewmodel.onLike()}")
You are not finished. You will somehow need to connect your logic to that databinding, so go to your Java/Kotlin code for your Activity/Fragment and:
Replace the setContentView(R.layout.some_activity) with val binding : SomeActivityBinding =
DataBindingUtil.setContentView(this, R.layout.some_activity)
Notice the SomeActivityBinding will be provided by the IDE itself because is handled on the Databinding library according to the xml naming.
That's the most basic. Good luck.
I'm trying to bind objects to my arrayadapter. However, the generated binding doesn't work when I have my model outside of my root folder.
So this works:
app
- Class
And this doesn't:
app
- Models
- Class
I've been fixing an issue with my databinding for a while now and this was the fix. The generator kept including generating "Models.Class" as the targeted binding model.
Any idea why this happens? Having the model class in my root folder isn't ideal.
Apparently folder names are case sensitive, changing to "model" correctly generates binding.
We've been using the Navigation component in our project but have stumbled across an issue which seems unique to our project and the JetBrains Android plugin. I can't pinpoint the exact point it started happening but I noticed it after the release of AS 3.2.
The project builds fine however when editing a navigation XML things such as auto-complete and highlighting are broken and the Android Support Plugin throws this error:
java.lang.IllegalArgumentException: Multiple entries with same key: org.jetbrains.android.dom.navigation.NavigationSchema$TypeRef#0=org.jetbrains.android.dom.navigation.NavigationSchema$NavigatorKeyInfo#17d69e53 and org.jetbrains.android.dom.navigation.NavigationSchema$TypeRef#0=org.jetbrains.android.dom.navigation.NavigationSchema$NavigatorKeyInfo#442c4fde
at com.google.common.collect.ImmutableMap.conflictException(ImmutableMap.java:215)
at com.google.common.collect.ImmutableMap.checkNoConflict(ImmutableMap.java:209)
at com.google.common.collect.RegularImmutableMap.checkNoConflictInKeyBucket(RegularImmutableMap.java:147)
at com.google.common.collect.RegularImmutableMap.fromEntryArray(RegularImmutableMap.java:110)
at com.google.common.collect.ImmutableMap$Builder.build(ImmutableMap.java:393)
at org.jetbrains.android.dom.navigation.NavigationSchema.buildCacheKeys(NavigationSchema.java:488)
at org.jetbrains.android.dom.navigation.NavigationSchema.init(NavigationSchema.java:479)
at org.jetbrains.android.dom.navigation.NavigationSchema.createIfNecessary(NavigationSchema.java:389)
at org.jetbrains.android.dom.AttributeProcessingUtil.processNavAttributes(AttributeProcessingUtil.java:408)
at org.jetbrains.android.dom.AttributeProcessingUtil.processAttributes(AttributeProcessingUtil.java:596)
at org.jetbrains.android.dom.AndroidDomExtender.registerExtensions(AndroidDomExtender.java:57)
at org.jetbrains.android.dom.AndroidDomExtender.registerExtensions(AndroidDomExtender.java:29)
at com.intellij.util.xml.reflect.DomExtenderEP.extend(DomExtenderEP.java:83)
at com.intellij.util.xml.impl.DynamicGenericInfo.runDomExtenders(DynamicGenericInfo.java:134)
at com.intellij.util.xml.impl.DynamicGenericInfo.lambda$checkInitialized$0(DynamicGenericInfo.java:64)
at com.intellij.openapi.util.RecursionManager$2.doPreventingRecursion(RecursionManager.java:98)
at com.intellij.util.xml.impl.DynamicGenericInfo.checkInitialized(DynamicGenericInfo.java:63)
at com.intellij.util.xml.impl.DynamicGenericInfo.getAttributeChildrenDescriptions(DynamicGenericInfo.java:241)
at com.intellij.util.xml.impl.DynamicGenericInfo.processAttributeChildrenDescriptions(DynamicGenericInfo.java:254)
at com.intellij.util.xml.impl.DomSemContributor.lambda$registerSemProviders$5(DomSemContributor.java:199)
at com.intellij.semantic.SemServiceImpl$2.lambda$registerSemElementProvider$0(SemServiceImpl.java:93)
at com.intellij.semantic.SemServiceImpl.createSemElements(SemServiceImpl.java:190)
at com.intellij.semantic.SemServiceImpl.getSemElements(SemServiceImpl.java:161)
at com.intellij.semantic.SemService.getSemElement(SemService.java:37)
at com.intellij.util.xml.impl.DomManagerImpl.getDomHandler(DomManagerImpl.java:390)
at com.intellij.util.xml.impl.GenericValueReferenceProvider.getReferencesByElement(GenericValueReferenceProvider.java:47)
at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistryImpl.getReferences(ReferenceProvidersRegistryImpl.java:135)
at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistryImpl.mapNotEmptyReferencesFromProviders(ReferenceProvidersRegistryImpl.java:123)
at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistryImpl.doGetReferencesFromProviders(ReferenceProvidersRegistryImpl.java:102)
at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry.getReferencesFromProviders(ReferenceProvidersRegistry.java:50)
at com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry.getReferencesFromProviders(ReferenceProvidersRegistry.java:44)
at com.intellij.psi.impl.source.xml.XmlAttributeValueImpl.getReferences(XmlAttributeValueImpl.java:110)
at com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor.checkReferences(XmlHighlightVisitor.java:439)
at com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor.visitXmlAttributeValue(XmlHighlightVisitor.java:406)
at com.intellij.psi.impl.source.xml.XmlAttributeValueImpl.accept(XmlAttributeValueImpl.java:61)
at com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor.visit(XmlHighlightVisitor.java:587)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.runVisitors(GeneralHighlightingPass.java:353)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.lambda$collectHighlights$5(GeneralHighlightingPass.java:286)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.analyzeByVisitors(GeneralHighlightingPass.java:313)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.lambda$analyzeByVisitors$6(GeneralHighlightingPass.java:316)
at com.intellij.codeInsight.daemon.impl.analysis.XmlHighlightVisitor.analyze(XmlHighlightVisitor.java:597)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.analyzeByVisitors(GeneralHighlightingPass.java:316)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.lambda$analyzeByVisitors$6(GeneralHighlightingPass.java:316)
at com.intellij.codeInsight.daemon.impl.DefaultHighlightVisitor.analyze(DefaultHighlightVisitor.java:71)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.analyzeByVisitors(GeneralHighlightingPass.java:316)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.collectHighlights(GeneralHighlightingPass.java:283)
at com.intellij.codeInsight.daemon.impl.GeneralHighlightingPass.collectInformationWithProgress(GeneralHighlightingPass.java:227)
at com.intellij.codeInsight.daemon.impl.ProgressableTextEditorHighlightingPass.doCollectInformation(ProgressableTextEditorHighlightingPass.java:84)
at com.intellij.codeHighlighting.TextEditorHighlightingPass.collectInformation(TextEditorHighlightingPass.java:69)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.lambda$null$1(PassExecutorService.java:423)
at com.intellij.openapi.application.impl.ApplicationImpl.tryRunReadAction(ApplicationImpl.java:1171)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.lambda$doRun$2(PassExecutorService.java:416)
at com.intellij.openapi.progress.impl.CoreProgressManager.registerIndicatorAndRun(CoreProgressManager.java:582)
at com.intellij.openapi.progress.impl.CoreProgressManager.executeProcessUnderProgress(CoreProgressManager.java:532)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:87)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.doRun(PassExecutorService.java:415)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.lambda$run$0(PassExecutorService.java:391)
at com.intellij.openapi.application.impl.ReadMostlyRWLock.executeByImpatientReader(ReadMostlyRWLock.java:147)
at com.intellij.openapi.application.impl.ApplicationImpl.executeByImpatientReader(ApplicationImpl.java:222)
at com.intellij.codeInsight.daemon.impl.PassExecutorService$ScheduledPass.run(PassExecutorService.java:389)
at com.intellij.concurrency.JobLauncherImpl$VoidForkJoinTask$1.exec(JobLauncherImpl.java:161)
at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1056)
at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1692)
at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:157)
We're running the latest version of the navigation component (2.0.0-rc2) and and I've tried all the usual fixes (Invalidate Caches & Restart etc). I've tried using navigation in fresh projects and it seems to work without this error being thrown.
I have tried removing all nav XMLs from our project and then adding in a completely blank XML but it still throws the error. I delved into the source of the plugin and it seems this occurs when the plugin is initially processing the nav file for the design view which doesn't make sense if it's throwing it for a fresh nav file.
I'm currently at a loss as to why this may be happening and any ideas would be greatly appreciated!
The short answer:
I fixed it by moving an anonymous class which extended androidx.navigation.Navigator to a separate class and adding the annotation #Navigator.Name(String name) with a unique name.
If you are also experiencing this issue check that in your project or its dependencies if there are any classes inheriting the Navigator class which are either missing the #Navigator.Name(String name) annotation or using non-unique strings as the name parameter. fragment and activity cannot be used as they are used already within the API.
Further info:
After further delving into the Android Support plugin source code to understand what's going on I've managed to solve the issue on our project.
When initiating the IDE functionality in the Android Support Plugin it goes through your project and any dependencies looking for any class which inherits the Navigator class and gives it a tag for the purpose of managing it in the XML files and design view. This tag is based on an annotation #Navigator.Name(String name). If no annotation is present a default value is used.
These tags have to be unique as they are used as the keys for building an immutable map of tags to navigator types. In our project we had an anonymous class which was extending Navigator class and had no annotation.
There is also an anonymous class extending Navigator within the navigation component source, specifically within the NavDeepLinkBuilder class. As there is only a single default value for the tags any other class inheriting Navigator with no annotation being present in your project or its dependencies will cause this issue.
I am a newbie at robolectric unit testing, i have a fragment which i want to test, ex, ScreenAFragment. This has a hierarchy as below,
ScreenAFragment -> MyAppFragment -> MyLibFragment -> SherlockFragment.
There are 2 problems,
The MyLibFragment has some code which links to a database and does other stuff which don't need to be tested as a 'unit test case'.
This MyLibFragment reference is also causing a "PackageManager#NameNotFoundException"
How do I go about testing this? My initial thought was create a ShadowMyLibFragment and config my test case #config(shadow={ShadowMyLibFragment.class}) to use this instead of the original class. Doesn't seem to be working at the moment, as it still seems to refer to the original library and throws the old error.
Any Solutions? I am using Robolectric 2.4 version.