【问题标题】:Not able to make proguard work in my android app无法在我的 android 应用程序中使 proguard 工作
【发布时间】:2018-05-08 03:51:23
【问题描述】:

我无法让 proguard 在我的应用中工作。当我将 minify 启用为 true 时,布局屏幕中的 textinputlayout 工作正常,但我有一个包含一个膨胀 XML 的警报对话框,该 XML 没有膨胀(该 XML 包含一个 textinputlayout。请帮助大家。这是代码 sn-p。

PS:我正在使用移动应用发布,请不要因为对齐不正确而抨击。我真的需要帮助。

Build.gradle 条目-->

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') }

XML 屏幕中的TextInputLayout -->

    <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>

警报对话框代码-->

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>

编辑:当 proguard 未启用时,对话框会在工作状态下弹出 textinputlayout,但当我启用 proguard 时,会弹出对话框但 textinputlayout 不存在,只有确定按钮存在。 android监视器没有错误

proguard 关闭时的屏幕

proguard 开启时的屏幕

【问题讨论】:

  • 您无法将 TextInputLayout 设置为视图,甚至无法对其进行充气。看我的回答你就明白了。
  • 你能发布错误消息/堆栈跟踪吗?
  • @AesSedai101 Android 显示器没有错误。

标签: android android-layout android-proguard


【解决方案1】:

【讨论】:

    【解决方案2】:

    我没有正确配置 proguard,也没有保留 gson 库使用的模型。所以我的对象返回的值完全不同。由于启用了 proguard,所以我无法调试,这让我认为我们可能需要对 UI 元素进行例外处理。但是 Pankaj 的帮助让我意识到这不是 UI,所以随机搜索让我知道我们需要保留 gson 库使用的模型类

    【讨论】:

    • 很高兴间接听到,我帮助您解决了您的问题。
    【解决方案3】:

    试试下面的代码。

    对于您的 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>
    

    在您要显示警报对话框的活动中,使用以下代码:

    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();
    }
    

    你会得到下面的输出:

    【讨论】:

    • 同样的事情正在发生。当 proguard 未启用时,对话框在工作状态下弹出 textinputlayout,但是当我启用 proguard 时,对话框正在弹出,但 textinputlayout 不存在,只有 ok 按钮。 android监视器没有错误
    • @Ashish0294 您使用的是哪个 gradle 版本?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-28
    • 2021-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    相关资源
    最近更新 更多