16

After having converted my Android project to Kotlin I am unable to build a release build, the error appears to be related to Proguard.

I see 155 warnings like

Warning:com.example.app.activity.MainActivity$1: can't find referenced field 'android.view.View decorView' in program class com.example.app.activity.MainActivity
Warning:com.example.app.activity.MainActivity$2: can't find referenced field 'android.os.Handler handler' in program class com.example.app.activity.MainActivity

and the build fails with

Error:Execution failed for task ':MyApp:transformClassesAndResourcesWithProguardForFreeRelease'.
> Job failed, see logs for details

If I change these directives in build.grade from true to false the build succeeds.

minifyEnabled true
shrinkResources true

Any suggestions on what the issue might be?

Edit:

The proguard files are below:

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

# If you want to enable optimization, you should include the
# following:
# -optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
# -optimizationpasses 5
# -allowaccessmodification
#
# Note that you cannot just include these flags in your own
# configuration file; if you are including this file, optimization
# will be turned off. You'll need to either edit this file, or
# duplicate the contents of this file and remove the include of this
# file from your project's proguard.config path property.

-keepattributes *Annotation*
-keep public class * extends android.app.Activity
-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
-keep public class * extends android.app.backup.BackupAgent
-keep public class * extends android.preference.Preference
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.app.Fragment
-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 public class * extends android.view.View {
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
    public void set*(...);
}

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

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

-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);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$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.**
-dontwarn io.codetail.animation.**

proguard-rules.txt

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/francesc/droids/android-sdk-linux/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

-keep class com.nineoldandroids.** { *; }
-dontwarn io.codetail.animation.**

The signing config is defined as shown below

signingConfigs {
    myConfig {
        Properties keyProps = new Properties()
        keyProps.load(new FileInputStream(file('../release.properties')))
        storeFile file(keyProps["store"])
        keyAlias keyProps["alias"]
        storePassword keyProps["storePass"]
        keyPassword keyProps["keyPass"]
    }
}

buildTypes {
    release {
        proguardFiles 'proguard-android.txt', 'proguard-rules.txt'
        minifyEnabled true
        shrinkResources true
        signingConfig signingConfigs.myConfig
    }
}
SAurabh
  • 87
  • 17
Francesc
  • 25,014
  • 10
  • 66
  • 84
  • might be useful https://stackoverflow.com/questions/33547643/how-to-use-kotlin-with-proguard/34159813#34159813 – Manohar May 30 '17 at 04:49
  • is there any custom view in your activity? – Sarthak Mittal May 30 '17 at 05:31
  • Probably, you can leave shrinkResources to "true" since it affects only resources, but not the code – cyanide May 30 '17 at 05:47
  • Are fields decorView and handler present at your activity? They could be removed as a result of optimization. – cyanide May 30 '17 at 05:49
  • Could you add all relevant configurations for Proguard? – tynn May 30 '17 at 14:44
  • @Sarthak Mittal: yes, there are several custom views in the project. – Francesc May 30 '17 at 15:20
  • @cyanide: yes, both decorView and Handler are used in the MainActivity – Francesc May 30 '17 at 15:20
  • @Redman thanks, I came across that same post yesterday and tried that, with no success. – Francesc May 30 '17 at 15:21
  • We would need to see the source code of MainActivity in order to see how you use these decorView and handler variables, and maybe your full build.gradle file as well. These warnings usually mean that the fields are not declared in the source library jars. – BladeCoder Jul 16 '17 at 14:44
  • What is the cause of the Error specified in the logs? Is it failing because of too many Warnings? You only posted 2 lines of warnings, I would ask you to read over the full log to spot any clear specifications why it fails. Also, the keep rule you have in your `proguard-rules.txt` file is a bit too broad: ``` -keep class com.nineoldandroids.** { *; } ``` I would suggest you narrow it down a bit. – anthonymonori Jan 02 '18 at 10:38

2 Answers2

1

Try to add @Keep annotation to the class in question (e.g. MainActivity) and see if it helps. If it does, it means that minify is renaming some resource, that shouldn't be renamed because some other resource expects it by it's original name.

Simon
  • 1,657
  • 11
  • 16
0

Try to make build without proguard to undestand who is responsible for that issue.