7

I have a special case where I need to remove an Activity which is not used in the code itself and not references in the Manifest. In other words it is dead code which is not obfuscated, so I want to get rid of it.

The normal constrains keep classes which extends Activity which is fine in general, but that keeps that dead code.

How can I exclude this special Activity from being kept?

Shubhank
  • 21,721
  • 8
  • 65
  • 83
rekire
  • 47,260
  • 30
  • 167
  • 264
  • Have you removed Activity from AndroidManifest file? – Vyacheslav Jun 07 '16 at 06:33
  • @Vyacheslav yes it is removed from the manifest as mentioned in the question. – rekire Jun 07 '16 at 06:36
  • what methods are shown inside Activity, if you open your classes in jar-reader? – Vyacheslav Jun 07 '16 at 07:06
  • I mean unpack apk > jar. And open it. – Vyacheslav Jun 07 '16 at 07:07
  • In this special case there is just the `onCreate()` method, the class is not removed because it is references in proguard rules (in fact all classes are kept which extends Activity). – rekire Jun 07 '16 at 09:42
  • You might want to check this [question](http://stackoverflow.com/questions/20619955/why-proguard-keeps-activity-class-in-android) why your activity is kept. – T. Neidhart Jun 09 '16 at 18:36
  • @tome I know that the class is kept because it extends the Activity class, but this does not explain how to exclude such a class. – rekire Jun 09 '16 at 18:40
  • You did not read the answers. Maybe your activity is accessed via reflection? Maybe it is still referenced in some resource? Check also the output of aapt which analyzes resources and generates additional proguard rules. – T. Neidhart Jun 09 '16 at 18:42
  • Well I did not check the output of aapt. I might check that. – rekire Jun 09 '16 at 18:44
  • You can also add the following to your configuration: -printconfiguration test.pro This will print the whole configuration as used by Proguard into the file test.pro in your app folder. Check this for any additional rules that were added, e.g. from 3rd party libraries or aapt. – T. Neidhart Jun 09 '16 at 18:46
  • A useful tool is also the option '-whyareyoukeeping class xxx.your.activity.class' which will output the reason why a certain class is being kept. Did you also verify that you did not enable -dontshrink by accident? – T. Neidhart Jun 09 '16 at 18:50
  • I know that `-whyareyoukeeping`. However there was an output something like *kept by directive* or something similar, that was not very helpful for me. – rekire Jun 09 '16 at 18:53
  • btw. you could also try to add an exclusion rule like that: -keep class !your.activity.class . The important thing here is the order of the rules, so this rule has to come first before any other rule. – T. Neidhart Jun 10 '16 at 06:13
  • Good to know I'll check it soonish and inform you if it worked – rekire Jun 10 '16 at 09:08

1 Answers1

0

The aapt process will generate ProGuard rules that will be automatically appended and contains any referenced classes in resource files.

The rules are created in the following directory:

build/intermediates/proguard-rules/<variant>/aapt_rules.txt

The rules in this file also reference the origin:

# view AndroidManifest.xml #generated:14
-keep class com.example.HelloWorldActivity { <init>(...); }

In this example, the class com.example.HelloWorldActivity is referenced in the AndroidManifest.xml.

T. Neidhart
  • 6,060
  • 2
  • 15
  • 38