Share

ProGuard Java Optimizer and Obfuscator

Tracker: Feature Requests

7 Combining multiple classes into one - ID: 1883005
Last Update: Settings changed ( lafortune )

would be great to have a new optimization that combines multiple classes
into one. there is a commercial tool that claims it can do it:
http://www.s-cradle.com/english/products/sophiacompress_java/scj_operation.
html
(though i haven't tried it personally).

there are conditions where multiple classes can be safely combined into
one. for example, if class A which has a single instance (say, "main")
contains only one instance of class B, and there is no other instance of
class B, (and there aren't any name collisions), then the members of class
B can be "merged" into class A, and class B can be eliminated.

this optimization is particularly useful for mobile apps where jar size is
very important. it can help keep the jar small, and the code tidy, since
developers can add classes more liberally.

thank you for ProGuard,
barak


Barak Naveh ( barak_naveh ) - 2008-01-30 21:27

7

Closed

None

Eric Lafortune

None

None

Public


Comments ( 5 )

Date: 2008-05-22 17:20
Sender: stuaxo


Well.. the simplest one has to be a class full of constants, with no
subclasses.

This could be combined with pretty much any other class, if the methods
don't clash.

In fact, classes like this could probably be merged at least with each
other first.



Date: 2008-02-07 12:04
Sender: barak_navehAccepting Donations


Ok, so it was indeed me who failed to see the complexities :)

Among the alternatives for J2ME developers are the use of a preprocessor
and writing /really/ long classes. None is attractive.

Do you have any idea of what can be done?
Any restricted form of class merging that could be safe to use under a
small set of limitation?


Date: 2008-02-04 17:41
Sender: lafortuneProject Admin


ProGuard 4.0 has changed a lot internally, so the extensions to ProGuard
3.7 will not be compatible.

I understand the use, but I can see many potential hurdles:

- How to detect reliably that exactly one instance is created?
(multiple constructors, chained constructors, exceptions,...).
- What if the field is volatile?
- What if the code synchronizes on instances of class A or
class B? How to detect this 100% reliably?
- Even if only one class A has only one field of type B,
class A will probably have code that uses it, passing it around
in local variables and even method parameters. What if the
field is added to a vector, which is then passed on?
(escape analysis)
- What if one instance of class A passes its instance of class B
to another instance of class A?
- What if one instance of class A retrieves the instance of
class B from another instance of class A?

Some problems are easy to solve, but quite a few are already difficult to
detect. If thinking about it for a few minutes brings up these pitfalls,
surely much more will pop up later on. I estimate that the other type of
class merging is simpler to handle automatically.


Date: 2008-02-02 21:33
Sender: barak_navehAccepting Donations


thanks for the references. does Torbjorn Soderstedt's extension is
supposed to work on the latest ProGuard?

it could be that class merging is indeed more complex than how i perceive
it. however, in some specific scenarios it appears to be quite simple, and
tremendously useful.

specifically, when coding for J2ME, it is necessary to limit the number of
classes in order to prevent jar size bloat. fewer classes leads to
difficulty in readability and maintainability of the code. this pain
intensifies as the application grows.

suppose we had limited class merging, only for all the singletons (or
classes that have up to one instance). not "a class that is referenced by a
field is inlined" but "a class that is ONLY referenced by ONLY ONE field IN
THE ENTIRE APP is inlined". this restricted merging appears to be simpler
to implement, and could be a great relief to a very painful problem.

or do i fail to see the complexities?


Date: 2008-01-31 19:02
Sender: lafortuneProject Admin


Class merging has already been suggested several times before (e.g.,
requests #1481773 and #1517957). This is also what the commercial tool
seems to do, judging by the web page. Torbjorn Soderstedt has written an
implementation for an older version of ProGuard.

What you suggest is a different and more complex variant, where a class
that is referenced by a field is inlined. I have thought about it before,
and it's a very interesting idea, but the constraints are pretty complex,
and not straightforward to implement. Please don't hold your breath for
it...


Attached File

No Files Currently Attached

Changes ( 4 )

Field Old Value Date By
status_id Open 2008-12-14 23:27 lafortune
close_date - 2008-12-14 23:27 lafortune
assigned_to nobody 2008-01-31 19:02 lafortune
priority 5 2008-01-30 21:28 barak_naveh