Runtime Error using ViewModelProvider(this) in AppCompatActivity - java

I am trying to use a ViewModelProvider for an android dynamic-feature library, which is being added to an existing project. I have the following code in my AppCompatActivity class.
MyModel model = new ViewModelProvider(this).get(MyModel.class);
The project is using the 2.2.0 version of the androidx lifecycle libraries. From my gradle file:
implementation 'androidx.appcompat:appcompat:1.1.0'
def lifecycle_version = "2.2.0"
// ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version"
I am getting the following error at runtime.
java.lang.NoSuchMethodError: No direct method <init>(Landroidx/lifecycle/ViewModelStoreOwner;)V in class Landroidx/lifecycle/ViewModelProvider; or its super classes (declaration of 'androidx.lifecycle.ViewModelProvider' appears in.....apk
Is this a bug? Do I have some dependency issue with something already in the larger app? How would I track down the dependency issue if it is somehow using an older version of ViewModelProvider from the existing app? I would have thought by naming the explicit version of the dependency in my gradle file, it would avoid such conflicts.

I had a similar problem. I don't know why but it works well when I use the deprecated class ViewModelProviders.
MyModel model = new ViewModelProviders.of(this).get(MyModel.class);

Related

XmlObject.Factory.newInstance() error after upgrading xmlbeans.jar to 5.x.x

I need to upgrade Apache's xmlbeans.jar in my project from version 3.1.0 to 5.0.3 because it's a dependency of another jar. But doing so messes up some legacy webservice-related code which utilize XmlBeans as an XML processor, causing compiler errors.
The errors all look like this:
SomeDocument doc = SomeDocument.Factory.newInstance();
... where SomeDocument extends XmlObject.
With the jar's prvious version, the newInstance() method returned an instance of SomeDocument, no problem. However, the new version is trying to create an instance of the parent XmlObject.
How should I refactor this code? Can I cast the XmlObject instance to SomeDocument? Or should I use the XmlObjectFactory instead, as suggested by its javadoc. And if I should, how can I refactor the above code using that class instead?

How to add com.fasterxml.jackson.core.exc.InputCoercionException as a dependency in gradle?

I am getting java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/PropertyNamingStrategies which is used in another project. I have included jackson jar in current gradle project as well. But while starting the project I am getting the above mentioned error. Seems like we need to add com.fasterxml.jackson.core.exc.InputCoercionException as an dependency but I am not able to understand where to add this as a dependency ? Can someone please help ?
java.lang.NoClassDefFoundError Either means - missing dependency with class com/fasterxml/jackson/databind/PropertyNamingStrategies or class was removed meaning jackson libs versions used in your project dependencies won't work together.
How to start solving problems like those.
1, Via IDE try to find missing class if is present. If is not present then try to find jar with missing class on internet and add as dependency. In case your IDE show class is present then problem may be with import scope. Scope management differ per used technology so provide detail which one you use or paste dependencies from build.kts . Make sure you use implementation in case you import this class in project and not runtimeOnly.
2, You found class then try to print project dependency tree command differ per used technology. For gradle ./gradlew dependencies or for submodule ./gradlew submoduleName:dependencies and look at versions of jackson in your project.
3, Check jackson lib with version listed via dependency tree contains missing class.
How to avoid problem like those with spring boot.
I would recoment to use BOM provided by spring boot project, versions in there should work together.
For gradle with kotlin DSL we import it like this
import org.springframework.boot.gradle.plugin.SpringBootPlugin
plugins {
id("org.springframework.boot") version "2.6.2"
}
dependencies {
val springBootPlatform = platform(SpringBootPlugin.BOM_COORDINATES)
annotationProcessor(springBootPlatform)
implementation(springBootPlatform)
//this version has to be searched for spring boot version
implementation(platform("org.springframework.cloud:spring-cloud-dependencies:2021.0.0"))
//put desired jackson dependencies
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
}

Android compilation error after upgrading to 'com.google.android.gms:play-services-ads:18.1.0'

I am upgrading my play-services-ads library from version 12 to version 18.1:
dependencies {
api 'com.google.android.gms:play-services-ads:18.1.0'
}
The problem is that the compilation fails with this error:
.gradle/caches/transforms-1/files-1.1/play-services-ads-identifier-17.0.0.aar/75b3c9fbdc51199269673bd2fa8b6cfe/jars/classes.jar(com/google/android/gms/ads/identifier/AdvertisingIdClient.class): warning: Cannot find annotation method 'value()' in type 'GuardedBy': class file for javax.annotation.concurrent.GuardedBy not found
I took away all the usages for AdvertisingIdClient and left only the import, but the problem persists:
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
Is there anything I am doing wrong?
com.google.android.gms:play-services-ads:18.1.0 depends upon com.google.android.gms:play-services-ads-identifier:17.0.0, among other libraries.
Your error indicates that com.google.android.gms:play-services-ads-identifier:17.0.0 references javax.annotation.concurrent.GuardedBy. However, that class is not in the Android SDK. The POM file for com.google.android.gms:play-services-ads-identifier:17.0.0 should be referencing a library that has an implementation of that class, but it does not seem to.
One library that has an implementation of that class is com.google.code.findbugs:jsr305. Adding a dependency on com.google.code.findbugs:jsr305 for a recent version (e.g., 3.0.2) gave you that class, satisfying the compiler.
So, there appears to be a bug in the Play Services SDK packaging, which my workaround resolves. You might want to add a comment in your module's build.gradle file to consider removing the com.google.code.findbugs:jsr305 if a future update to com.google.android.gms:play-services-ads fixes this bug.

Android Studio dependency incompatible

I just want to know how to fix this issue with incompatibility with dependencies on android studio. I am trying to follow the steps on setting up firebase but could not find any solution with this incompatibility. Please help
That is because Firebase libraries is using support libraries implicitly. You need to override the incompatible libraries by adding the clashed support libraries but with the same current version. For your case, you need to override support-media-compat. You can add the following dependencies to your dependencies block:
dependencies {
implementation "com.android-support:support-media-compat:28.0.0"
implementation "com.android-support:animated-vector-drawable:28.0.0"
implementation "com.android-support:support-v4:28.0.0"
...
}

RoboActionBarActivity for RoboGuice

I use Gradle to add the dependency org.roboguice:roboguice:3.0b-experimental to my project.
I found a post on the issue tracker, saying that there should be a RoboActionBarActivity class (to replace the ActionBarActivity I use for drawer navigation) in the latest beta. I can find the code on Github, but the class isn't in the jar. I found several implementations of the RoboActionBarActivity on the web, but those have other dependencies, and I don't want to resolve them all manually. Is there a way to get this class via Gradle?
Until the RoboActionBarActivity class is provided with the RoboGuice library you can make your own descendant class for ActionBar support. Or you can get the ActionBar4RoboGuice library, which contains the original RoboActionBarActivity, and use it with the latest stable release of RoboGuice.

Categories

Resources