hbb20 / CountryCodePickerProject

Country Code Picker (CCP) is an android library which provides an easy way to search and select country or international phone code. Also supports Android EditText phone mask and international phone validation.
Apache License 2.0
1.52k stars 505 forks source link

NoSuchMethodError: No virtual method parseAndKeepRawInput #592

Closed zeroDivider closed 1 year ago

zeroDivider commented 1 year ago

Significant crash had appeared after adding your library in my project:

Fatal Exception: java.lang.NoSuchMethodError: No virtual method parseAndKeepRawInput(Ljava/lang/String;Ljava/lang/String;)Lcom/google/i18n/phonenumbers/Phonenumber$PhoneNumber; in class Lcom/google/i18n/phonenumbers/PhoneNumberUtil; or its super classes (declaration of 'com.google.i18n.phonenumbers.PhoneNumberUtil' appears in base.apk!classes3.dex)

What is weird about it is that I am using PhoneInputLayout on totally different screen from where I am using your library. On PhoneNumberFragmet:

        phoneNumberButton.setOnClickListener(new OnOneClickListener() {
            @Override
            public void onOneClick(View v) {
                String pattern = "^\\+[1-9]\\d{5,14}$";
                if (phoneInputLayout.getPhoneNumber().matches(pattern)) { // <-------------
                     // doing something unrelated here
                } else {
                    phoneInputLayout.setError(getActivity().getResources().getString(R.string.bad_phone_number_format_error));
                }
            }
        });

and it seems problem is on phoneInputLayout.getPhoneNumber() since it is implemented as:

  private Phonenumber.PhoneNumber parsePhoneNumber(String number) throws NumberParseException {
    String defaultRegion = mCountry != null ? mCountry.getCode().toUpperCase() : "";
    return mPhoneUtil.parseAndKeepRawInput(number, defaultRegion);
  }

  /**
   * Gets phone number.
   *
   * @return the phone number
   */
  public String getPhoneNumber() {
    try {
      Phonenumber.PhoneNumber number = parsePhoneNumber(getRawInput());
      return mPhoneUtil.format(number, PhoneNumberUtil.PhoneNumberFormat.E164);
    } catch (NumberParseException ignored) {
    }
    return getRawInput();
  }

(getPhoneNumber() is calling the method above and it crashes on mPhoneUtil.parseAndKeepRawInput)

We did not do anything with PhoneNumberFragmet for ages and crash appears there after adding:

implementation 'com.hbb20:ccp:2.7.1'

My question is if I should exclude PhoneUtills from your library or put your library or PhoneUtils in proguard-rules.txt since it looks like my release build is missing PhoneUtils (?!) after adding your library to my code, or do anything else?

zeroDivider commented 1 year ago

I will close this since it is inside other lib.. I was wrong to open it here.