centsol / androidsvg

Automatically exported from code.google.com/p/androidsvg
0 stars 0 forks source link

Proguard breaks reflective CSS selectors #61

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Parse and render the attached SVG

What is the expected output? What do you see instead?
Expected is a paper with lines. (open in Chrome for test)
Actual is a paper without lines.

What version of the product are you using? On what operating system?
1.2.1, Android 4.4

Please provide any additional information below.
After some investigation I found the culprit. I'm using CSS in those SVG files 
and the element selectors (line { ... }) use reflection to match tag names:
    if (!sel.tag.equals(obj.getClass().getSimpleName().toLowerCase(Locale.US)))

This is happy until you shrink and obfuscate your code with ProGuard which 
renames the classes.

It would be nice if as an alternative to reflection you used hardcoded Strings 
which are more stable and it's probably faster as well:
class SvgElementBase {
   protected String tag;
   ...
}
class Line extends SvgElementBase {
   {tag = "line";} // this has the benefit of not needing to repeat the class name
   // or
   Line() { tag = "line";}
   ...
}

or any other method think fits.

Original issue reported on code.google.com by papp.robert.s on 7 May 2015 at 11:40

Attachments:

GoogleCodeExporter commented 8 years ago
I found two workarounds for the issue in case anyone finds this:

1. Keep affected classes AND inner class attributes
    # See com.caverock.androidsvg.CSSParser.selectorMatch
    -keepnames class * extends com.caverock.androidsvg.SVG$SvgElementBase
    # Needed for Java to be able to decipher "SVG$Line" into "Line" when calling getSimpleName()
    -keepattributes InnerClasses

2. Use a mapping file to explicitly rename classes
This option doesn't require InnerClasses, but not futureproof at the same time.
    # See com.caverock.androidsvg.CSSParser.selectorMatch
    # Rename classes extending SvgElementBase explicitly to be directly in a package instead of an inner class
    -applymapping mapping.txt

Original comment by papp.robert.s on 7 May 2015 at 11:46

Attachments: