Shrinking your app with R8 (Android Dev Summit '19)

CmplxN·2021년 1월 13일
0

영상

What is R8 Shrinking?

  • Optimizing code for size, in android the dex file.
  • Tree Shaking, Optimization, Obfuscating, Reduce Debug Information
  • thrid party library를 사용할 때 안쓰는 코드, 그 안에서의 최적화 가능
buildTypes {
    release {
        minifyEnabled false
    }
}

How does the shrinking work?

class JavaHelloWorld {
    private void unused() { // tree shaking (unreachable)
        System.out.println("Unused");
    }
    
    private static void greeting() { // method names can be obfuscated
        System.out.println("Hello, world!");
    }
    
    public static void main(String[] args) { // entry point
        greeting(); // greeting() can be inlined as optimization
    }
}
  • Well-known Entry Points : Activities, Services, Content Providers, Broadcast Receivers
  • AATP Tool gathers informations from manifest file.
  • the developer is responsible for reflection. (need to set custom rule)
  • for example add keep rules for data classes when using GSON which uses reflection

Class Inlining

  • Removes classes that are only used locally
  • Builders, Lambdas..
  • Escape Analysis (is it locally used?)
  • Method Inlining
  • Field Access Removal (unnecessary field assignment)
  • Instance Removal (remove instance that are not used as result)

Example

private const val databaseName = "hello-db" // constant value => inline

fun buildDatabase(context: Context): RoomDatabase {
	return Room.databaseBuilder(context, databaseName) // can be inlined + remove always conditions 
        .fallbackToDestructiveMigration()
        .build()
}
fun buildDatabase(context: Context): RoomDatabase {
	return Room.databaseBuilder(context, "hello-db")
        .fallbackToDestructiveMigration()
        .build()
}
fun buildDatabase(context: Context): RoomDatabase {
	val db = Room.getGeneratedImplementation(...)
    db.init(DatabaseConfiguration(context, "hello-db", true, ...))
    return db
}
profile
Android Developer

0개의 댓글