Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are unreferenced methods included in the final executable?

When building and deploying an executable on Android without running ProGuard, are unreferenced methods included in the final executable?

Are unreferenced methods from external library jars included as well?

Is this behavior dependent upon the Java compiler, or does dex do all the trimming, if any?

like image 504
Jeff Axelrod Avatar asked Jun 29 '12 13:06

Jeff Axelrod


1 Answers

I tested with a simple class (all these methods are unreferenced) :

public class Test
{
    private void privateMethod()
    {
        System.out.println("private");
    }

    protected void protectedMethod()
    {
        System.out.println("protected");
    }

    public void publicMethod()
    {
        System.out.println("public");
    }

    void method()
    {
        System.out.println("method");
    }
}

I compiled the APK, extracted Test.class and decompiled it (with javap -c). I got the following results. I also tested with a jar instead of an APK, and the result is exactly the same. I used Java 1.6.0_29.

  protected void protectedMethod();
    Code:
       0: getstatic     #44             // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #47             // String protected
       5: invokevirtual #46             // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return        

  public void publicMethod();
    Code:
       0: getstatic     #44             // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #48             // String public
       5: invokevirtual #46             // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return        

  void method();
    Code:
       0: getstatic     #44             // Field java/lang/System.out:Ljava/io/PrintStream;
       3: ldc           #49             // String method
       5: invokevirtual #46             // Method java/io/PrintStream.println:(Ljava/lang/String;)V
       8: return        

Which means only private functions are excluded at compile time.

I also tried to declare the class final, but the result was the same.

like image 70
Dalmas Avatar answered Oct 01 '22 04:10

Dalmas