23

When I let Android Studio generate override method it will generate the method with strange parameter names.

For instance according to documentation onCheckedChanged should look like this:

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){}

but i got

public void onCheckedChanged(CompoundButton compoundButton, boolean b){}

or onDateSet in DatePickerDialog i got:

onDateSet(DatePicker datePicker, int i, int i1, int i2)

instead of

onDateSet(DatePicker view, int year, int month, int dayOfMonth)

I got Android SDK set up in a project and Sources for Android 27 installed.

Any ideas?

Thanks

aminography
  • 21,986
  • 13
  • 70
  • 74
Saintan
  • 408
  • 3
  • 9
  • 2
    it`s variable Names , does it`s Matter ?! – Elsunhoty Mar 11 '18 at 11:26
  • possible duplicate of [this](https://stackoverflow.com/questions/48008103/inappropriate-names-of-parameter-in-implemented-overrided-methods) – Jaydip Kalkani Mar 11 '18 at 11:35
  • 1
    @Elsunhoty it REALLY does matter. If you take a good look at the example `onDateSet`, you can see that 3 integers mean **nothing** while meaningful names do tell you precisely what each parameter represents. I'm very curios to find a solution for this, cause I've had it bother me for a while! I know some methods used to have good parameter names, and they just tend to stop. – Vucko Oct 22 '18 at 23:18
  • Yes , Your are Right – Elsunhoty Oct 23 '18 at 12:33

2 Answers2

26

It is related to the compileSdkVersion which is defined in your build.gradle file. You should install Sources for Android SDK for the API you used as compileSdkVersion. So try to install sources version equal to compileSdkVersion in SDK Manager.

I've set the compileSdkVersion 28 in build.gradle file. Here is the result before and after installing sources version 28 (Notice: You should restart AndroidStudio after that):

Before:

val textWatcher = object: TextWatcher {

    override fun afterTextChanged(p0: Editable?) {
    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }
}

After:

val textWatcher = object: TextWatcher {

    override fun afterTextChanged(s: Editable?) {
    }

    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
    }

    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
    }
}

UPDATE: 10/23/2018

The problem about AppCompat-v28 libraries such as RecyclerView class, comes from their aar artifacts. If you use version 27.1.1 libraries, the issue will gone. It doesn't matter what distribution of version 28 you are using (Such as 28.0.0, 28.0.0-alpha1, 28.0.0-alpha3, 28.0.0-rc1, 28.0.0-rc2, etc.) In all of version 28 distributions, the parameter names for abstract methods are obfuscated.

public abstract static class Adapter<VH extends RecyclerView.ViewHolder> {

    public Adapter() {
    }

    @NonNull
    public abstract VH onCreateViewHolder(@NonNull ViewGroup var1, int var2);

    public abstract void onBindViewHolder(@NonNull VH var1, int var2);

    ...
}

So it seems that there is no way to solve, until it would be fixed in the next distributions.

aminography
  • 21,986
  • 13
  • 70
  • 74
6

As aminography said, this is because you have not installed sources for compileSdk you are using. For installing sources follow this steps:

  1. check your project's compileSdk version.(from app level build.gradle file). As you can see in below image my compileSdk version is 26. So, i have to install sources for platform 26.

enter image description here

2. Go to sdk manager and in SDK Platforms tab check show package details option if not checked. Then install sources for your compileSdk version(In my case it's 26). That's it. Now, you will get appropriate parameters name.

enter image description here

Chirag Talaviya
  • 171
  • 2
  • 6