Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifecycle annotations seem to have wrong annotation javaName #6

Closed
daividssilverio opened this issue Sep 20, 2019 · 13 comments
Closed
Assignees

Comments

@daividssilverio
Copy link

Hello there,

It seems that the [Lifecycle.Event.OnStart], [Lifecycle.Event.OnStop] and other from the AndroidX.Lifecycle namespace have their java annotation counterparts set to the old libraries.

Found this while testing the migration in the Toggl app, we are using the old [Lifecycle.Event.OnStart], [Lifecycle.Event.OnStop] here:
https://github.com/toggl/mobileapp/blob/d79af0ab5522a325046baf19b4616cb086b1bc71/Toggl.Droid/Startup/TogglApplication.cs#L83-L99

Test branch: https://github.com/toggl/mobileapp/tree/doivid/androidx-migration-test

After migrating, we get this error while building:

TogglApplication.java(34, 25):  error: package android.arch.lifecycle does not exist
@android.arch.lifecycle.OnLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_START)
TogglApplication.java(43, 25):  error: package android.arch.lifecycle does not exist
@android.arch.lifecycle.OnLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_STOP)

Checking the generated source code, I found this:

namespace AndroidX.Lifecycle
{
  [Register("androidx/lifecycle/Lifecycle", DoNotGenerateAcw = true)]
  public abstract class Lifecycle : Java.Lang.Object
  {
 ...
     [Register("androidx/lifecycle/Lifecycle$Event", DoNotGenerateAcw = true)]
    public sealed class Event : Java.Lang.Enum
    {
      internal new static readonly JniPeerMembers _members = (JniPeerMembers) new XAPeerMembers("androidx/lifecycle/Lifecycle$Event", typeof (AndroidX.Lifecycle.Lifecycle.Event));

      [Register("ON_ANY")]
      public static AndroidX.Lifecycle.Lifecycle.Event OnAny
      {
        get
        {
          return Java.Lang.Object.GetObject<AndroidX.Lifecycle.Lifecycle.Event>(AndroidX.Lifecycle.Lifecycle.Event._members.StaticFields.GetObjectValue("ON_ANY.Landroidx/lifecycle/Lifecycle$Event;").Handle, JniHandleOwnership.TransferLocalRef);
        }
      }

      [Register("ON_CREATE")]
      public static AndroidX.Lifecycle.Lifecycle.Event OnCreate
      {
        get
        {
          return Java.Lang.Object.GetObject<AndroidX.Lifecycle.Lifecycle.Event>(AndroidX.Lifecycle.Lifecycle.Event._members.StaticFields.GetObjectValue("ON_CREATE.Landroidx/lifecycle/Lifecycle$Event;").Handle, JniHandleOwnership.TransferLocalRef);
        }
      }

      [Register("ON_DESTROY")]
      public static AndroidX.Lifecycle.Lifecycle.Event OnDestroy
      {
        get
        {
          return Java.Lang.Object.GetObject<AndroidX.Lifecycle.Lifecycle.Event>(AndroidX.Lifecycle.Lifecycle.Event._members.StaticFields.GetObjectValue("ON_DESTROY.Landroidx/lifecycle/Lifecycle$Event;").Handle, JniHandleOwnership.TransferLocalRef);
        }
      }

      [Register("ON_PAUSE")]
      public static AndroidX.Lifecycle.Lifecycle.Event OnPause
      {
        get
        {
          return Java.Lang.Object.GetObject<AndroidX.Lifecycle.Lifecycle.Event>(AndroidX.Lifecycle.Lifecycle.Event._members.StaticFields.GetObjectValue("ON_PAUSE.Landroidx/lifecycle/Lifecycle$Event;").Handle, JniHandleOwnership.TransferLocalRef);
        }
      }

      [Register("ON_RESUME")]
      public static AndroidX.Lifecycle.Lifecycle.Event OnResume
      {
        get
        {
          return Java.Lang.Object.GetObject<AndroidX.Lifecycle.Lifecycle.Event>(AndroidX.Lifecycle.Lifecycle.Event._members.StaticFields.GetObjectValue("ON_RESUME.Landroidx/lifecycle/Lifecycle$Event;").Handle, JniHandleOwnership.TransferLocalRef);
        }
      }

      [Register("ON_START")]
      public static AndroidX.Lifecycle.Lifecycle.Event OnStart
      {
        get
        {
          return Java.Lang.Object.GetObject<AndroidX.Lifecycle.Lifecycle.Event>(AndroidX.Lifecycle.Lifecycle.Event._members.StaticFields.GetObjectValue("ON_START.Landroidx/lifecycle/Lifecycle$Event;").Handle, JniHandleOwnership.TransferLocalRef);
        }
      }

      [Register("ON_STOP")]
      public static AndroidX.Lifecycle.Lifecycle.Event OnStop
      {
        get
        {
          return Java.Lang.Object.GetObject<AndroidX.Lifecycle.Lifecycle.Event>(AndroidX.Lifecycle.Lifecycle.Event._members.StaticFields.GetObjectValue("ON_STOP.Landroidx/lifecycle/Lifecycle$Event;").Handle, JniHandleOwnership.TransferLocalRef);
        }
      }

      internal new static IntPtr class_ref
      {
        get
        {
          return AndroidX.Lifecycle.Lifecycle.Event._members.JniPeerType.PeerReference.Handle;
        }
      }

      public override JniPeerMembers JniPeerMembers
      {
        get
        {
          return AndroidX.Lifecycle.Lifecycle.Event._members;
        }
      }

      protected override IntPtr ThresholdClass
      {
        get
        {
          return AndroidX.Lifecycle.Lifecycle.Event._members.JniPeerType.PeerReference.Handle;
        }
      }

      protected override System.Type ThresholdType
      {
        get
        {
          return AndroidX.Lifecycle.Lifecycle.Event._members.ManagedPeerType;
        }
      }

      internal Event(IntPtr javaReference, JniHandleOwnership transfer)
        : base(javaReference, transfer)
      {
      }

      [Register("valueOf", "(Ljava/lang/String;)Landroidx/lifecycle/Lifecycle$Event;", "")]
      public static unsafe AndroidX.Lifecycle.Lifecycle.Event ValueOf(string name)
      {
        IntPtr jobject = JNIEnv.NewString(name);
        try
        {
          JniArgumentValue* parameters = stackalloc JniArgumentValue[1];
          parameters[0] = new JniArgumentValue(jobject);
          return Java.Lang.Object.GetObject<AndroidX.Lifecycle.Lifecycle.Event>(AndroidX.Lifecycle.Lifecycle.Event._members.StaticMethods.InvokeObjectMethod("valueOf.(Ljava/lang/String;)Landroidx/lifecycle/Lifecycle$Event;", parameters).Handle, JniHandleOwnership.TransferLocalRef);
        }
        finally
        {
          JNIEnv.DeleteLocalRef(jobject);
        }
      }

      [Register("values", "()[Landroidx/lifecycle/Lifecycle$Event;", "")]
      public static unsafe AndroidX.Lifecycle.Lifecycle.Event[] Values()
      {
        return (AndroidX.Lifecycle.Lifecycle.Event[]) JNIEnv.GetArray(AndroidX.Lifecycle.Lifecycle.Event._members.StaticMethods.InvokeObjectMethod("values.()[Landroidx/lifecycle/Lifecycle$Event;", (JniArgumentValue*) null).Handle, JniHandleOwnership.TransferLocalRef, typeof (AndroidX.Lifecycle.Lifecycle.Event));
      }

      >>>>>> This package is from the old lib, right? <<<<<<<<<      
[Annotation("android.arch.lifecycle.OnLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_CREATE)")]
      public class OnCreateAttribute : Attribute
      {
      }

      >>>>>> This package is from the old lib, right? <<<<<<<<<      
[Annotation("android.arch.lifecycle.OnLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_START)")]
      public class OnStartAttribute : Attribute
      {
      }

      >>>>>> This package is from the old lib, right? <<<<<<<<<      
[Annotation("android.arch.lifecycle.OnLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_RESUME)")]
      public class OnResumeAttribute : Attribute
      {
      }

      
      >>>>>> This package is from the old lib, right? <<<<<<<<<
[Annotation("android.arch.lifecycle.OnLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_PAUSE)")]
      public class OnPauseAttribute : Attribute
      {
      }

      >>>>>> This package is from the old lib, right? <<<<<<<<<
[Annotation("android.arch.lifecycle.OnLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_STOP)")]
      public class OnStopAttribute : Attribute
      {
      }

      >>>>>> This package is from the old lib, right? <<<<<<<<<      
[Annotation("android.arch.lifecycle.OnLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_DESTROY)")]
      public class OnDestroyAttribute : Attribute
      {
      }

      >>>>>> This package is from the old lib, right? <<<<<<<<<
[Annotation("android.arch.lifecycle.OnLifecycleEvent(android.arch.lifecycle.Lifecycle.Event.ON_ANY)")]
      public class OnAnyAttribute : Attribute
      {
      }
    }
  }
}
@semanticer
Copy link

If this is possible under Xamarin rule (running java 8 underneath) we should simply switch from annotation processing based lifecycle to java8 variant that uses default method implementations. We need to move from androidx.lifecycle:lifecycle-compiler: package to androidx.lifecycle:lifecycle-common-java8: and replace all LifecycleObserver with DefaultLifecycleObserver remove the annotations and only implement lifecycle methods that we need. No need for annotation magic.

@daividssilverio
Copy link
Author

Hey, I was able to use the annotations by simply adding a custom OnStart and OnStop annotation that references the updated androidX package. It works! Anyone facing the same problem can check the workaround here: https://github.com/toggl/mobileapp/commit/ec70d8c00dd3e1d8b067e81f2eec21a5fc167416

@Redth
Copy link
Member

Redth commented Nov 25, 2019

@Redth Redth transferred this issue from xamarin/XamarinAndroidXMigration Nov 25, 2019
@moljac
Copy link
Member

moljac commented Nov 26, 2019

Fixed.
Tagging for new preview!

@moljac
Copy link
Member

moljac commented Nov 28, 2019

@daividssilverio
Please can you provide feedback?

@daividssilverio
Copy link
Author

Sure thing, I'll try it later today.

@daividssilverio
Copy link
Author

@moljac I've updated our repo to target the preview06 version of the androidX packages and the correct reference seems to be there for the annotations. But I can't build the application anymore on that version.

I'm currently getting this error during build time:

  Xamarin.AndroidX.Migration.targets(131, 9): Could not find 2 Android X assemblies, make sure to install the following NuGet packages:
 - Xamarin.AndroidX.Arch.Core.Runtime
 - Xamarin.AndroidX.Collection
You can also copy-and-paste the following snippet into your .csproj file:
    <PackageReference Include="Xamarin.AndroidX.Arch.Core.Runtime" Version="2.0.1-preview02" />
    <PackageReference Include="Xamarin.AndroidX.Collection" Version="1.0.0-preview02" />

The instructions don't make sense to me... why would I use -preview02 only for those two libs?

Here's the test branch that produces that error: https://github.com/toggl/mobileapp/tree/doivid/test-updated-androidx-nuget-packages

@moljac
Copy link
Member

moljac commented Nov 29, 2019

@daividssilverio

The instructions don't make sense to me... why would I use -preview02 only for those two libs?

Spot on. Not sure why -preview02!?!!??

Checking sample. Thanks

@moljac
Copy link
Member

moljac commented Nov 29, 2019

@daividssilverio
Regarding the sample app:

  1. Why mixture of Android.Support and AndroidX?
  2. Which version of Xamarin.AndroidX.Migration do you use?
    Never mind. I'll find that out.

@daividssilverio
Copy link
Author

daividssilverio commented Nov 29, 2019

@moljac

Glad you asked. We've followed the guide on https://devblogs.microsoft.com/xamarin/androidx-for-xamarin/

And, yes, I've tried removing the Android.Support libs, but the GoogleServices/Firebase related libs adds them all back!

The Xamarin.AndroidX.Migration does some "magic", right? It should remove the support libs and others during the build. We are using preview-06 in the test branch I've sent you.

In our develop branch we are using preview-02.

@moljac
Copy link
Member

moljac commented Nov 29, 2019

@daividssilverio

but the GoogleServices/Firebase related libs adds them all back!

That's the problem. We haven't published GPS-FB stuff that depend on AndroidX. Not even preview.

OK. Alles klar (all clear).

@Redth Redth added this to the Stable Release milestone Dec 5, 2019
@moljac
Copy link
Member

moljac commented Dec 6, 2019

@daividssilverio Feedback would really be nice and appreciated. Thanks

@Redth
Copy link
Member

Redth commented Dec 6, 2019

I believe this is fixed and released in preview. If you still have problems with this, please open a new issue.

@Redth Redth closed this as completed Dec 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants