1

I'm not able to make proguard work in my app. When I do minify enabled as true, textinputlayout in layout screens are working fine but I have an alertdialog which contains one inflated XML , that XML is not getting inflated (that XML contains one textinputlayout. Please help guys. Here is code snippet.

PS: I'm posting using mobile app, please don't go bashing for not proper alignment. I really need help.

Build.gradle entries-->

buildTypes { 
           release {        
           minifyEnabled true 
           proguardFiles getDefaultProguardFile('proguardandroid.txt'),    'proguard-rules.pro'         signingConfig
           signingConfigs.release   
                  } 
          }

       dependencies { compile fileTree(include: ['*.jar'], dir: 'libs')      
       compile 'com.android.support:appcompat-v7:25.1.0'
       compile 'com.google.code.gson:gson:2.2.4'
       compile 'com.android.support:design:25.1.0' 
       compile 'com.android.support:support-v4:25.1.0'
       compile 'com.android.support:cardview-v7:25.1.0'
       compile project(':volley-1.0.0') 
       compile project(':photoview-1.2.4') 
       compile project(':calligraphy-2.2.0') }

TextInputLayout in XML screen -->

    <android.support.design.widget.TextInputLayout    
    android:id="@+id/pin_login_wrapper" 
    android:layout_width="match_parent"     
    android:layout_height="wrap_content">   

    <android.support.design.widget.TextInputEditText    
    android:layout_width="match_parent"         
    android:layout_height="wrap_content"        
    android:gravity="center_horizontal"     
    android:hint="Please enter PIN"         
    android:inputType="numberPassword"
    android:maxLength="4" 
    android:textSize="24sp"/>  
    </android.support.design.widget.TextInputLayout>

Alert dialog code -->

AlertDialog.Builder builder = new AlertDialog.Builder(getContext(), R.style.AlertDialogStyle); 
final TextInputLayout otpWrapper = (TextInputLayout) (LayoutInflater.from(getContext())).inflate(R.layout.single_edit_text_material, null);
otpWrapper.setHint("Please enter OTP"); 
otpWrapper.getEditText().setSingleLine(true); 
otpWrapper.getEditText().setInputType(InputType.TYPE_CLASS_NUMBER); 
setEditTextMaxLength(otpWrapper.getEditText(), 6); 
builder.setView(otpWrapper); builder.setNegativeButton("Cancel", null); 
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener(){
    @Override   public void onClick(DialogInterface dialog, int which) {
    } 
 });
 builder.setCancelable(false); final AlertDialog d = builder.create(); 
 d.setCanceledOnTouchOutside(false); d.show();

single_edit_text_material.xml -->

    <android.support.design.widget.TextInputLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/wrapper" android:layout_width="match_parent" 
    android:layout_height="wrap_content" android:paddingEnd="8dp" 
    android:paddingStart="8dp" android:paddingTop="8dp"> 
    <android.support.design.widget.TextInputEditText 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"/> 
    </android.support.design.widget.TextInputLayout>

Edit : The dialog is getting popped up with textinputlayout in working condition when proguard is not enabled but when I enable the proguard the dialog is getting popped up but textinputlayout is not there only ok button is there. There is no error in android monitor

screen when proguard is off screen when proguard is off

screen when proguard is on screen when proguard is on

Paul Chu
  • 1,249
  • 3
  • 19
  • 27
Ashish0294
  • 101
  • 2
  • 8

3 Answers3

0

Try the code below.

For your single_edit_text_material.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.TextInputLayout
        android:id="@+id/textInput"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Please enter OTP">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLength="6" />

    </android.support.design.widget.TextInputLayout>

</LinearLayout>

In your activity where you want to show the alert dialog, use the code below:

AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.Theme_AppCompat_Dialog_Alert);
View view = (LayoutInflater.from(this)).inflate(R.layout.single_edit_text_material, null);
TextInputLayout otpWrapper = view.findViewById(R.id.textInput);
EditText editText = view.findViewById(R.id.editText);
builder.setView(view);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
    }
});
builder.setCancelable(false);
final AlertDialog d = builder.create();
d.setCanceledOnTouchOutside(false);
d.show();
}

You will get the output below:

enter image description here

Pang
  • 9,564
  • 146
  • 81
  • 122
Pankaj Mundra
  • 1,401
  • 9
  • 25
  • Same thing is happening. The dialog is popped up with textinputlayout in working condition when proguard is not enabled but when I enable the proguard the dialog is getting popped up but textinputlayout is not there only ok button is there. There is no error in android monitor – Ashish0294 Nov 24 '17 at 08:17
  • @Ashish0294 which gradle version you are using? – Pankaj Mundra Nov 24 '17 at 09:46
0

In proguard-rules ignore TextInputLayout and AlertDialog Packages and Classes.

Take a Look on the following examples

How to keep/exclude a particular package path when using proguard?

How keep my class from obfuscate by proguard

How to exclude a classes from being kept by proguard

Tomin B Azhakathu
  • 2,656
  • 1
  • 19
  • 28
0

I was not configuring proguard properly and was not keeping the model which was used by gson library. So my object was returning values different alltogether. And as proguard was enabled so i was not able to debug which led me to think that may be we need to make exception for UI elements. But Pankaj's help made me realize that is was not UI so a random search got me that we need to keep model classes which are used by gson library

Ashish0294
  • 101
  • 2
  • 8