Skip to content

Instantly share code, notes, and snippets.

@Guang1234567
Last active December 23, 2022 02:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Guang1234567/dc6d9aa196d8dd8ef07fc1802817be81 to your computer and use it in GitHub Desktop.
Save Guang1234567/dc6d9aa196d8dd8ef07fc1802817be81 to your computer and use it in GitHub Desktop.
Inheritance from an interface with '@JvmDefault' members is only allowed with -Xjvm-default option
android {
kotlinOptions {
//freeCompilerArgs += "-Xjvm-default=all"
// or
freeCompilerArgs += [
"-Xjvm-default=all",
]
}
}

Factory implementation before 2.5.0:

public interface Factory {
        /**
         * Creates a new instance of the given {@code Class}.
         * <p>
         *
         * @param modelClass a {@code Class} whose instance is requested
         * @param <T>        The type parameter for the ViewModel.
         * @return a newly created ViewModel
         */
        @NonNull
        <T extends ViewModel> T create(@NonNull Class<T> modelClass);
    }

Note one method, create, without implementation.

Here is the rewrite they did in 2.5.0

public interface Factory {
        /**
         * Creates a new instance of the given `Class`.
         *
         * Default implementation throws [UnsupportedOperationException].
         *
         * @param modelClass a `Class` whose instance is requested
         * @return a newly created ViewModel
         */
        public fun <T : ViewModel> create(modelClass: Class<T>): T {
            throw UnsupportedOperationException(
                "Factory.create(String) is unsupported.  This Factory requires " +
                    "`CreationExtras` to be passed into `create` method."
            )
        }

        /**
         * Creates a new instance of the given `Class`.
         *
         * @param modelClass a `Class` whose instance is requested
         * @param extras an additional information for this creation request
         * @return a newly created ViewModel
         */
        public fun <T : ViewModel> create(modelClass: Class<T>, extras: CreationExtras): T =
            create(modelClass)

       
          ...
        }

So that explains it, this interface is now a default implementation interface, and to inherit from it, need to add the compiler args, as suggested by the compiler (freeCompilerArgs += "-Xjvm-default=all").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment