簡體   English   中英

如何修復proguard警告'找不到引用的方法'用於類java.lang.Object的現有方法'clone'和'finalize'

[英]how to fix proguard warning 'can't find referenced method' for existing methods 'clone' and 'finalize' of class java.lang.Object

我嘗試壓縮消耗ical4j.jar的Android應用程序。

當我使用gradle proguardDebug構建帶有proguard的apk時,我得到了

  • 警告:net.fortuna.ical4j.model.CalendarFactory:在庫類java.lang.Object中找不到引用的方法'void finalize()'
    • finalize()另外6個類似警告
  • 警告:net.fortuna.ical4j.model.CalendarFactory:在庫類java.lang.Object中找不到引用的方法'java.lang.Object clone()'
    • 另外6個類似的clone()警告

我已經驗證android-7支持finalize()clone() :“... \\ Android ... \\ sdk \\ platforms \\ android-7 \\ android.jar”在類java.lang.Object有方法finalize()clone() java.lang.Object

你知道如何解決這個問題嗎?

注意:這不是其他'proguard找不到引用方法'的重復問題,因為在我的具體情況下我認為缺少的方法應該存在。

我在用

  • proguard-4.11 with
  • gradle產出1.11
    • Groovy:1.8.6
    • Ant:2013年7月8日編譯的Apache Ant(TM)版本1.9.2
    • 常春藤:2.2.0
    • JVM:1.7.0_25(Oracle Corporation 23.25-b01)
    • 操作系統:Windows 7 6.1 amd64
  • ical4j.jar 1.0.5

這是proguard配置proguard-rules.txt ,可能需要一些修復:

#  proguard-rules.txt
## ical4j also contains groovy code which is not used in android 
-dontwarn groovy.**
-dontwarn org.codehaus.groovy.**
-dontwarn org.apache.commons.logging.**
-dontwarn sun.misc.Perf

-dontnote com.google.vending.**
-dontnote com.android.vending.licensing.**

這是我的build.gradle

    buildscript {
        repositories {
            mavenCentral()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.9.+'
            classpath 'net.sf.proguard:proguard-gradle:4.11'
        }
    }

    allprojects {
        repositories {
            mavenCentral()
        }
    }

    apply plugin: 'android'

    configurations {
        compile.exclude group: 'commons-logging' // referenced in some portable lib. use androids internal instead
    }

    android {
        compileSdkVersion 19
        buildToolsVersion '19.0.3'

        packagingOptions {
            exclude 'META-INF/LICENSE.txt'
            exclude 'META-INF/NOTICE.txt'
        }

        defaultConfig {
            minSdkVersion 7
            targetSdkVersion 19
        }

        buildTypes {
            release {
                runProguard false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }

            // used for testing. remove if it works as expected
            debug {
                runProguard true
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            }
        }
    }

    dependencies {
        compile 'org.mnode.ical4j:ical4j:1.0.5'
        compile 'backport-util-concurrent:backport-util-concurrent:3.1'
        compile 'commons-codec:commons-codec:1.8'
        compile 'commons-lang:commons-lang:2.6'
    }

[更新2014-12-20]

我在下面添加了我的工作配置作為答案。

注:當前的Android工作室1.0(android.buildToolsVersion> = '20'),則必須更換minifyEnabled runProguard

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

net.fortuna.ical4j.model.CalendarFactory擴展了groovy.util.AbstractFactory ,它擴展了java.lang.Object 但是,輸入中缺少中產階級(您使用-dontwarn抑制了相應的警告)。 由於缺少部分類層次結構,ProGuard沒有意識到CalendarFactory可以訪問受保護的方法clonefinalize ,並且它會打印出這些警告。

由於您的代碼可能根本不使用該類,因此您可以禁止顯示警告:

-dontwarn net.fortuna.ical4j.model.CalendarFactory

或覆蓋所有類似的類:

-dontwarn net.fortuna.ical4j.model.**

您不應為此問題添加任何-keep選項; Android SDK已經為您指定了與Android相關的基本選項。

這是我的proguard配置文件。 嘗試復制粘貼它

-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose    

# standard, except v4.app.Fragment, its required when app uses Fragments

-keep public class * extends android.app.Activity
-keep public class * extends android.support.v7.app.ActionBarActivity
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider

-keepclasseswithmembers class * {
    native <methods>;
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

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

-keepclassmembers class * extends android.app.Activity { 
       public void *(android.view.View); 
}

然后添加您的代碼,如下所示:使用時

-dontwarn groovy.**

還添加

-keep class groovy.** { *; }

為所有外部庫做它。

[更新2014-05-30重新制定本文]

謝謝@EricLafortune回答,幫助我理解並解決了這個問題。

對於其他想要壓縮android ical4j應用程序的人來說,這是我的工作解決方案:

所有具有in library class java.lang.Object - 問題都來自命名空間

 net.fortuna.ical4j.model.**

所以我將這些行添加到proguard-rules.txt

###################
# Get rid of #can't find referenced method in library class java.lang.Object# warnings for clone() and finalize()
# for details see http://stackoverflow.com/questions/23883028/how-to-fix-proguard-warning-cant-find-referenced-method-for-existing-methods
-dontwarn net.fortuna.ical4j.model.**

###############
# I use proguard only to remove unused stuff and to keep the app small.
# I donot want to obfuscate (rename packages, classes, methods, ...) since this is open source
-keepnames class ** { *; }
-keepnames interface ** { *; }
-keepnames enum ** { *; }

統計:

  • 沒有混淆:932個班級; apk 911kb。
  • 混淆:365個類; apk 505kb

暫無
暫無

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

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