Why is kotlin not creating a Required empty public constructor for the fragment as required by android?

Viewed 717

Example: Create this file in a sample Android Studio project: Now copy this to a kotlin file and let it convert to kotlin.

import androidx.fragment.app.Fragment;

public class TestJavaFragment extends Fragment {

    public TestJavaFragment() {
        // Required empty public constructor!!!
    }
}

Kotlin converted code:

import androidx.fragment.app.Fragment

class TestJavaFragment : Fragment()

Tools > Code > Show Kotlin Bytecode > Decompile. Results in this thing WITHOUT the required empty public constructor for android fragments:

public final class TestJavaFragment extends Fragment {
   private HashMap _$_findViewCache;

   public View _$_findCachedViewById(int var1) {
      if (this._$_findViewCache == null) {
         this._$_findViewCache = new HashMap();
      }

      View var2 = (View)this._$_findViewCache.get(var1);
      if (var2 == null) {
         View var10000 = this.getView();
         if (var10000 == null) {
            return null;
         }

         var2 = var10000.findViewById(var1);
         this._$_findViewCache.put(var1, var2);
      }

      return var2;
   }

   public void _$_clearFindViewByIdCache() {
      if (this._$_findViewCache != null) {
         this._$_findViewCache.clear();
      }

   }

   // $FF: synthetic method
   public void onDestroyView() {
      super.onDestroyView();
      this._$_clearFindViewByIdCache();
   }
}

This is making some devices crash as it appears in crashytics:

Fatal Exception: java.lang.RuntimeException Unable to start activity ComponentInfo{the.activity.launching.the.fragment}: androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment OnboardingPage: could not find Fragment constructor

Also tried with a static newInstance where it creates the fragment with argments from the activity, and it crashes too on some devices.

So what's going on, what am I missing here?

Thanks.

0 Answers
Related