Can't find ViewModelProviders class, only ViewModelProvider - kotlin

I am trying to create lifecycle-aware view models. But I can't find ViewModelProviders class in my Android project, only ViewModelProvider. There seems to be no android.arch.lifecycle.ViewModelProviders package for me to import as well. What's happening

You probably have this dependency included in your project:
implementation "android.arch.lifecycle:viewmodel:$lifecycle_version"
That contains ViewModelProvider (and just 4 other classes), but ViewModelProviders is in a different package:
implementation "android.arch.lifecycle:extensions:$lifecycle_version"
Here are the contents of these packages for reference (as of version 1.1.1):
For the record, you can find this out yourself by looking up the docs for the ViewModelProviders class, where it says up top:
added in version 1.1.0
belongs to Maven artifact android.arch.lifecycle:extensions:1.1.1

Also, just for future reference.
ViewModelProviders class is now deprecated.
According to Android Developers Documentation: ViewModelProviders
This class is deprecated. Use the constructors for ViewModelProvider
directly.

Related

How to use #SerializedName in Kotlin Multiplatform project?

I am using Kotlin multiplatform. I am getting object name in Json response.
I want to map object name with other name. So I found to use #SerializedName.
But it's not working.
It says Unresolved reference: SerializedName.
You can use official kotlin native serialization library for multiplatform. How to add and apply the plugin to your project based on which gradle syntax you are using (either "apply plugin" or "plugin{..}") is described here. After adding dependency you can use #SerialName annotation instead of #SerializedName to override object names.

Deprecating an entire package in Kotlin

According to this answer package-level deprecation is possible in Java via package-info.java like so:
/**
* #deprecated As of release 2.0, replaced by {#link com.acme.new.package}
*/
#Deprecated
package com.acme.old.package;
Is there something similar to this mechanism in Kotlin? The Deprecated page documentation doesn't seem to offer guidance on this and I'd prefer not to annotate every function, field, and class separately.
You can use the same package-info.java; Kotlin should recognize the deprecations specified in it, and if it doesn't, it's a bug. Kotlin has no syntax of its own for specifying package-level annotations.

What packages/functions are imported by default in Kotlin?

In Java the java.lang package is imported by default.
In kotlin a number of functions and classes are available without being imported, like println and kotlins Array, Int, etc types.
What else is imported by default and where is it defined?
Kotlin stdlib has kotlin root package and its subpackages (see the full list with the content).
It seems not to be documented anywhere which of them are imported by default, but a peek into Kotlin Github sources suggests that these packages are imported for JVM target platform:
java.lang.*
kotlin.*
kotlin.annotation.*
kotlin.jvm.*
kotlin.collections.*
kotlin.ranges.*
kotlin.sequences.*
kotlin.text.*
kotlin.io.*
kotlin.coroutines.* (to be added in Kotlin 1.1, not present in 1.0.4)
I've manually tested them, and the list above is true for Kotlin 1.0.4. And these stdlib packages are not imported by default:
kotlin.comparisons.*
kotlin.concurrent.*
kotlin.properties.*
kotlin.reflect.*
kotlin.reflect.jvm.*
kotlin.system.*
As #Magnus noted, the default imports for JS platform are different.
The official documentation for the list of Kotlin's default imports (which is likely to be change with new versions of the language) is here:
https://kotlinlang.org/docs/reference/packages.html#default-imports
As of 2018-02-11 it includes the following:
kotlin.*
kotlin.annotation.*
kotlin.collections.*
kotlin.comparisons.* (since 1.1)
kotlin.io.*
kotlin.ranges.*
kotlin.sequences.*
kotlin.text.*
Additional packages are imported depending on the target platform:
JVM:
java.lang.*
kotlin.jvm.*
JS:
kotlin.js.*

Warnings while using a plugin and static library in a cocoa project

I have a scenario where I need to use a plugin as well as a static library into my xcode project. The plugin will be dynamically loaded into the system. Now, the static library is also getting used in creation of the plugin.
While executing my project I am getting a warning saying :
Class A is getting referenced from /staticLibraryPath and plugin. One of them will be used.
Please let me know, how to resolve the warning or a better way of implementing the scenario.
The issue is a name class of the two ClassA types found in both plugin and library
I assume you have control over the source of either plugin / library.
.. rename Class A in one instance to make the names not clash -- I don't think there is another way to get rid of the warning/error

Save and Load instances of objects created earlier via the Eclipse registry

I am currently experiencing a problem in my RCP application and wanted to ask, if someone stumbled over the same problem and can give me some valuable hints:
My RCP application allows plugins to provide implementations of a specific abstract class of my model (singleton) to extend my model during runtime via the update manager. I instantiate these classes via
extensionPointImplementation.createExecutableExtension(..)
after parsing the Eclipse registry. I can serialize the created instances using the default Java serialization API.
Now to the problem: The plugin trying to deserialize the objects cannot find the class implementations of the model extensions due to the fact, that there is no plugin dependency between the plugins. Nevertheless, it is not possible for me to create such a dependency which would make the idea of extending the model during runtime obsolete.
Is it possible to solve this problem by using the default Java serialization API or do I have to implement my own serialization (which parses the Eclipse registry and creates the instances via the line shown above if all necessary plugins are available, otherwise throw an exception) which might be based on the default Java serialization API (if possible I do not want to create the serialization completely by myself)?
Thanks.
You need to define a so called buddy policy.
In the bundle trying to instantiate the class add
Eclipse-BuddyPolicy: registered
to the manifest.mf.
In the bundle providing the class add
Eclipse-RegisterBuddy: <symbolic name of the bundle instantiating the class>
to the manifest.mf.