如何修复类 java.lang.Object 的现有方法“clone”和“finalize”的 proguard 警告“找不到引用的方法”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23883028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 08:49:19  来源:igfitidea点击:

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

javaandroidgradleproguardical4j

提问by k3b

I try to compress an android app that consumes ical4j.jar.

我尝试压缩一个消耗ical4j.jar.

When i build the apk with proguard using gradle proguardDebugi get

当我使用 proguard 构建 apk 时,gradle proguardDebug我得到

  • Warning: net.fortuna.ical4j.model.CalendarFactory: can't find referenced method 'void finalize()' in library class java.lang.Object
    • 6 additional similar warnings for finalize()
  • Warning: net.fortuna.ical4j.model.CalendarFactory: can't find referenced method 'java.lang.Object clone()' in library class java.lang.Object
    • 6 additional similar warnings for clone()
  • 警告:net.fortuna.ical4j.model.CalendarFactory:在库类 java.lang.Object 中找不到引用的方法“void finalize()”
    • 6 个额外的类似警告 finalize()
  • 警告:net.fortuna.ical4j.model.CalendarFactory:在库类 java.lang.Object 中找不到引用的方法“java.lang.Object clone()”
    • 6 个额外的类似警告 clone()

I already verified that android-7 supports finalize()and clone(): "...\Android...\sdk\platforms\android-7\android.jar" has methods finalize()and clone()in class java.lang.Object.

我已经验证了 android-7 支持finalize()clone(): "...\Android...\sdk\platforms\android-7\android.jar" has methods finalize()and clone()in class java.lang.Object

Do you have any idea how to fix this?

你知道如何解决这个问题吗?

Note: this is not a duplicate of other 'proguard can't find referenced method' questions because in my specific case i think that the missing method should be there.

注意:这不是其他 'proguard can't find referenced method' 问题的重复,因为在我的特定情况下,我认为缺少的方法应该在那里。

I am using

我在用

  • proguard-4.11 with
  • gradle-1.11
    • Groovy: 1.8.6
    • Ant: Apache Ant(TM) version 1.9.2 compiled on July 8 2013
    • Ivy: 2.2.0
    • JVM: 1.7.0_25 (Oracle Corporation 23.25-b01)
    • OS: Windows 7 6.1 amd64
  • ical4j.jar 1.0.5
  • proguard-4.11 与
  • gradle-1.11
    • 常规:1.8.6
    • Ant:Apache Ant(TM) version 1.9.2 2013 年 7 月 8 日编译
    • 常春藤:2.2.0
    • JVM:1.7.0_25(甲骨文公司 23.25-b01)
    • 操作系统:Windows 7 6.1 amd64
  • ical4j.jar 1.0.5

this is proguard config proguard-rules.txtthat probably need some fix:

这是 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.**

This is my build.gradle

这是我的 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'
    }

[Update 2014-12-20]

[更新 2014-12-20]

I have added my working configuration as answer below.

我已将我的工作配置添加为下面的答案。

Note: with Current Android Studio 1.0 (android.buildToolsVersion >= '20') you must replace runProguardwith minifyEnabled

注:与当前的Android工作室1.0(android.buildToolsVersion> = '20'),则必须更换 runProguardminifyEnabled

Example

例子

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

采纳答案by Eric Lafortune

net.fortuna.ical4j.model.CalendarFactoryextends groovy.util.AbstractFactorywhich extends java.lang.Object. However, the middle class is missing from your input (you're suppressing the corresponding warnings with -dontwarn). With part of the class hierarchy missing, ProGuard doesn't realize that CalendarFactory may access the protected methods cloneand finalize, and it prints out these warnings.

net.fortuna.ical4j.model.CalendarFactoryextendsgroovy.util.AbstractFactory延伸java.lang.Object. 但是,您的输入中缺少中产阶级(您使用 -dontwarn 抑制了相应的警告)。由于缺少部分类层次结构,ProGuard 没有意识到 CalendarFactory 可能会访问受保护的方法clonefinalize,并且会打印出这些警告。

Since your code probably doesn't use the class at all, you can suppress the warnings:

由于您的代码可能根本不使用该类,您可以取消警告:

-dontwarn net.fortuna.ical4j.model.CalendarFactory

Or for covering all similar classes:

或者覆盖所有类似的类:

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

You shouldn't add any -keep options for this issue; the Android SDK already specifies the basic Android-related -keep options for you.

您不应该为此问题添加任何 -keep 选项;Android SDK 已经为您指定了基本的 Android 相关 -keep 选项。

回答by Dawid C

This is my proguard config-file. Try copy-paste it

这是我的 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); 
}

Then add your code, as following: When using

然后添加您的代码,如下所示:使用时

-dontwarn groovy.**

also add

还添加

-keep class groovy.** { *; }

Do it for all external libraries.

为所有外部库执行此操作。

回答by k3b

[update 2014-05-30 reformulated this text]

[更新 2014-05-30 改写本文]

Thankyou to @EricLafortune answer that helped me to understand and solve the issue.

感谢@EricLafortune 的回答帮助我理解并解决了问题。

For others who want to compress android ical4j apps here is my working soluntion:

对于想要压缩 android ical4j 应用程序的其他人,这是我的工作解决方案:

all classes with the in library class java.lang.Object- issue are from namespace

所有带有in library class java.lang.Object- 问题的类都来自命名空间

 net.fortuna.ical4j.model.**

so i added these lines to proguard-rules.txt

所以我将这些行添加到 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 ** { *; }

statistics:

统计数据:

  • without obfuscation: 932 classes; apk 911kb.
  • with obfuscation: 365 classes;apk 505kb
  • 无混淆:932 个类;apk 911kb。
  • 混淆:365 个类;apk 505kb