How to fix proguard warning 'cannot find reference method' for existing methods 'clone' and 'finalize' of class java.lang.Object

I am trying to compress an android application that consumes ical4j.jar .

When I create apk with proguard using gradle proguardDebug I get

  • Warning: net.fortuna.ical4j.model.CalendarFactory: cannot find the reference method 'void finalize ()' in the java.lang.Object library class
    • 6 additional similar warnings for finalize()
  • Warning: net.fortuna.ical4j.model.CalendarFactory: cannot find the reference method 'java.lang.Object clone ()' in the java.lang.Object library class
    • 6 additional similar warnings for clone()

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

Do you have an idea how to fix this?

Note: this is not a duplicate of the other β€œproguard cannot find the reference method” questions, because in my particular case I believe that the missing method should be there.

I use

  • proguard-4.11 s
  • gradle -1.11
    • Groovy: 1.8.6
    • Ant: Apache Ant (TM) version 1.9.2, compiled 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

this is the proguard configurator proguard proguard-rules.txt , which probably needs some correction:

 # 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

  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]

I added my working configuration as an answer below.

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

Example

  android { buildTypes { release { minifyEnabled true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt' } } 
+7
java android gradle proguard ical4j
source share
3 answers

net.fortuna.ical4j.model.CalendarFactory extends groovy.util.AbstractFactory , which continues with java.lang.Object . However, the middle class is missing from your input (you suppress the corresponding warnings with -dontwarn). If part of the class hierarchy is missing, ProGuard does not realize that CalendarFactory can access the protected clone and finalize methods, and it generates these warnings.

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

 -dontwarn net.fortuna.ical4j.model.CalendarFactory 

Or to cover all such classes:

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

You should not add any -keep options for this problem; The Android SDK already sets the basic Android-keep options for you.

+12
source share

This is my proguard configuration file. Try copying it to a folder

 -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 shown below: When using

 -dontwarn groovy.** 

also add

 -keep class groovy.** { *; } 

Do this for all external libraries.

0
source share

[update 2014-05-30 reformulated this text]

Thanks for the answer from @EricLafortune, which helped me understand and solve the problem.

For those who want to compress android ical4j apps, here is my working problem:

all classes with a problem in library class java.lang.Object - from namespace

  net.fortuna.ical4j.model.** 

so I added these lines to 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:

  • no obfuscation: 932 classes; apk 911kb.
  • with obfuscation: 365 classes; apk 505kb
0
source share

All Articles