mesibo
mesibo

Reputation: 4323

Android Studio - Implement Methods and parameter names

Java compiler does not preserve parameter names for any interface, unless newer compiler option -parameter is used (I am not sure how to use it with android studio) - refer example below.

Since java compiler does not preserve parameters names, Android Studio "code -> implement methods" is not able to generate code with original parameter names.

The question is, how to implement a library module so that Android Studio Menu, Code->Implement Methods correctly generates code with all the original parameter names.

For example, following is a simple class and an interface. This class is in a separate aar module. When application uses this AAR, implements TablaListener and asks AndroidStudio to generate interface methods stubs, the parameter names are not preserved.

Please note that proguard is NOT used.

Any ideas?

public class TablaCore {

    public interface TablaListener {
        /**
         * @param params
         * @param data
         * @return
         */
        boolean TablaCore_onAction(String params, byte[] data);
    }

    private static TablaListener mListener = null;
    public static void setListener(TablaListener myListener) {
        mListener = myListener;
    }

    public TablaListener getListener() {
        return mListener;
    }

}

It is easy to demonstrate by compiling and decompiling above class. This is decompiled version

public class TablaCore
{
  private static TablaListener mListener = null;

  public static void setListener(TablaListener myListener)
  {
    mListener = myListener;
  }

  public TablaListener getListener()
  {
    return mListener;
  }

  public static abstract interface TablaListener
  {
    public abstract boolean TablaCore_onAction(String paramMessageParams, byte[] paramArrayOfByte);

  }
}

Upvotes: 1

Views: 1381

Answers (1)

faramir
faramir

Reputation: 291

You have to include Android SDK source code.

Go to: File > Settings... > Apperance & Behavior > System Settings > Android SDK

On the tab SDK Platforms select Show Package Details and find and select appropriate Sources for Android XX. Unfortunately, currently for Android API 27, there is no sources, but there is for Sources for Android 26. Apply changes - the download window should appear automatically.

Android SDK screenshot

After downloading and restarting implementing methods should use proper names for method parameters.

Upvotes: 1

Related Questions