簡體   English   中英

ProGuard導致應用程序滯后

[英]ProGuard causes lag of application

我正在嘗試使用proguard混淆代碼,因此在發布版本類型中啟用了minify

buildTypes {
    debug {
        minifyEnabled false
    }
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt')
    }
}

但是,當我生成“ release apk”並安裝后,應用程序運行緩慢(滯后)..為什么啟用minify會發生這種情況? 這是我的依賴項:

dependencies {
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:palette-v7:25.3.1'
    compile 'com.google.protobuf.nano:protobuf-javanano:3.0.0-alpha-2'
    compile 'com.jrummyapps:colorpicker:2.1.6'
    compile 'org.apache.commons:commons-lang3:3.4'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:design:25.3.1'
    compile project(':library')

    testCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    androidTestCompile 'com.android.support:support-annotations:25.3.1'
}

這是我的proguard-android.txt

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

我嘗試添加:

-keep class com.mylibrary.**
-keep interface com.mylibrary.**
-keep enum com.mylibrary.**

我的庫依賴項:

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:palette-v7:25.3.1'
    testCompile 'junit:junit:4.12'
    compile 'com.google.code.gson:gson:2.8.0'
    compile 'org.jetbrains:annotations-java5:15.0'
}

盡管proguard-android.txt的注釋指定了某些原因,但令我感到驚訝的是,由於某些默認的自動生成的proguard-android.txt文件在創建新的Android項目時不包含某些優化,因此禁用了某些優化。 嘗試刪除以下幾行,看看是否有改進:

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontoptimize
-dontpreverify

該文件的其余部分似乎還不錯,盡管我尚未簽出所有正在使用的庫,但我已經簽了幾個,並意識到您已經為它們添加了必要的例外。

如果仍然無法解決問題,則必須確保正確添加了正在使用的庫的異常,並且還必須檢查library模塊中的proguard-android.txt因為在依賴項中已引用了該文件。

Java總是在第一次運行時運行緩慢。 因為它必須被加載和解釋。 再次運行測試。 看這個問題。

當注釋掉從未執行的代碼時,Java程序的運行速度變慢

ProGuard在運行時不會導致應用程序延遲。 這可能是由於gradle文件中包含了您的一個庫。

我強烈建議您使用Android Studio Profiler來查看應用程序的功能和行為。 我的意思是內存,CPU,網絡等。保存結果以進行比較,然后僅使用proguard更改嘗試相同的應用程序版本。 性能影響極不可能來自於proguard。 如果可能,將結果和您的觀察結果通過此處,以進行進一步分析。 還要注意,構建類型也有所不同,您在調試/發行版本中是否使用不同的代碼?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM