当minifyEnable设置为true时,应用在特定布局上崩溃

时间:2018-12-16 05:48:31

标签: android minify

错误消息:

  

2018-12-16 11:08:28.339 7163-7163 /? E / AndroidRuntime:致命异常:main
      流程:com.appz.abhi.shudhbatter,PID:7163
      java.lang.NumberFormatException:null
          在java.lang.Long.parseLong(Long.java:406)
          在java.lang.Long.parseLong(Long.java:485)
          在com.appz.abhi.shudhbatter.view.user.main.a.b.b.a(未知来源)
          在androidx.recyclerview.widget.RecyclerView $ a.a(未知来源)
          在androidx.recyclerview.widget.RecyclerView $ o.a(未知来源)
          在androidx.recyclerview.widget.RecyclerView $ o.a(未知来源)
          在androidx.recyclerview.widget.RecyclerView $ o.b(未知来源)
          在androidx.recyclerview.widget.LinearLayoutManager $ c.a(未知来源)
          在androidx.recyclerview.widget.LinearLayoutManager.a(未知来源)
          在androidx.recyclerview.widget.LinearLayoutManager.a(未知来源)
          在androidx.recyclerview.widget.LinearLayoutManager.c(未知来源)
          在androidx.recyclerview.widget.RecyclerView.D(未知来源)
          在androidx.recyclerview.widget.RecyclerView.y(未知来源)
          在androidx.recyclerview.widget.RecyclerView.onLayout(未知来源)
          在android.view.View.layout(View.java:17666)
          在android.view.ViewGroup.layout(ViewGroup.java:5577)
          在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
          在android.widget.FrameLayout.onLayout(FrameLayout.java:261)
          在android.view.View.layout(View.java:17666)
          在android.view.ViewGroup.layout(ViewGroup.java:5577)
          在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
          在android.widget.FrameLayout.onLayout(FrameLayout.java:261)
          在android.view.View.layout(View.java:17666)
          在android.view.ViewGroup.layout(ViewGroup.java:5577)
          在androidx.constraintlayout.widget.ConstraintLayout.onLayout(未知   来源)
          在android.view.View.layout(View.java:17666)           在android.view.ViewGroup.layout(ViewGroup.java:5577)           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)           在android.widget.FrameLayout.onLayout(FrameLayout.java:261)           在android.view.View.layout(View.java:17666)           在android.view.ViewGroup.layout(ViewGroup.java:5577)           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)           在android.widget.FrameLayout.onLayout(FrameLayout.java:261)           在android.view.View.layout(View.java:17666)           在android.view.ViewGroup.layout(ViewGroup.java:5577)           在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)           在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)           在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)           在android.view.View.layout(View.java:17666)           在android.view.ViewGroup.layout(ViewGroup.java:5577)           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)           在android.widget.FrameLayout.onLayout(FrameLayout.java:261)           在android.view.View.layout(View.java:17666)           在android.view.ViewGroup.layout(ViewGroup.java:5577)           在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741)           在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)           在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)           在android.view.View.layout(View.java:17666)           在android.view.ViewGroup.layout(ViewGroup.java:5577)           在android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)           在android.widget.FrameLayout.onLayout(FrameLayout.java:261)           在com.android.internal.policy.DecorView.onLayout(DecorView.java:730)           在android.view.View.layout(View.java:17666)           在android.view.ViewGroup.layout(ViewGroup.java:5577)           在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2390)           在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2112)           在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1298)           在android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6437)           在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:876)           在android.view.Choreographer.doCallbacks(Choreographer.java:688)           在android.view.Choreographer.doFrame(Choreographer.java:623)           在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:862)           在android.os.Handler.handleCallback(Handler.java:754)

在发布版本中使用minifyEnabled=true时出现上述错误。
使用minifyEnabled=false在发布版本中构建相同的项目不会产生错误。

该错误不会在应用启动时出现,而是显示在一个片段上。

proguard-rules.pro:

-assumenosideeffects class android.util.Log {
    public static *** d(...);
}

build.gradle(app)(使用的库):

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "my.package.id"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 3
        versionName "0.3"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
        vectorDrawables.useSupportLibrary = true
    }

    buildTypes {
        debug {
            minifyEnabled true
            debuggable true
        }
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),
                    'proguard-rules.pro'
        }
    }
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    //  Testing
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'

    //  Support library
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
    implementation 'com.google.android.material:material:1.1.0-alpha01'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    //  Architecture components
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'

    //  Ads
    implementation 'com.google.android.gms:play-services-ads:17.1.2'

    //  Firebase
    implementation 'com.google.firebase:firebase-core:16.0.6'
    implementation 'com.google.firebase:firebase-firestore:17.1.3'
    implementation 'com.google.firebase:firebase-auth:16.0.5'
    implementation 'com.google.firebase:firebase-invites:16.0.6'
    implementation 'com.google.firebase:firebase-dynamic-links:16.1.5'

    //  FirebaseUI for Firebase Auth
    implementation 'com.firebaseui:firebase-ui-auth:4.2.0'

    //  Circular imageView
    implementation 'com.mikhaellopez:circularimageview:3.2.0'
    implementation 'de.hdodenhof:circleimageview:2.2.0'

    //  About libraries
    implementation 'com.mikepenz:aboutlibraries:6.2.0'
}

编辑1: 在崩溃的片段中,我有一个RecyclerView,它使用一个自定义适配器,其中使用了以下代码行:

viewHolder.userOrderRecyclerViewItemDateTv.setText(MyDateUtils.getDateStringFromLong(Long.parseLong(order.getOrderDate())));

MyDateUtils.java:

public class MyDateUtils {

    private static String calenderToDateString(Calendar calendar) {

        //  Convert calender object to date string
        return String.format(Locale.ENGLISH, "%02d - %02d - %04d",
                (calendar.get(Calendar.DAY_OF_MONTH)), (calendar.get(Calendar.MONTH) + 1),
                calendar.get(Calendar.YEAR));
    }

    public static String getDateStringFromLong(long dateLong) {

        //  Create a new calender instance
        Calendar calendar = Calendar.getInstance();

        //  Set calender time to given value
        calendar.setTimeInMillis(dateLong);

        //  Return calender as date in string format
        return calenderToDateString(calendar);
    }
}

2 个答案:

答案 0 :(得分:0)

可以通过对任何构建类型使用minifyEnabled选项来启用

ProGuard。如果打算将其用于生产,则强烈建议您在进行测试之前在开发中一次启用它。如果未在开发版本上全面测试ProGuard,则可能会遇到意外崩溃或应用程序无法按预期运行的情况。

似乎您没有为build.gradle文件中使用的库添加规则

https://guides.codepath.com/android/Configuring-ProGuard

答案 1 :(得分:0)

    buildTypes {
      debug {
minifyEnabled true  // shrink
useProguard false   // don't obfuscate

proguardFiles getDefaultProguardFile('proguard- 
android.txt'), 'proguard-rules.pro'
 }

 release {
   minifyEnabled true  // shrink
  useProguard true    // obfuscate

   proguardFiles getDefaultProguardFile('proguard- 
  android.txt'), 'proguard-rules.pro'
 }
  }