규칙을 사용하여 최적화 문제 해결

일반적인 보관 규칙, 추가 보관 규칙 유형, 전역 옵션 외에도 특정 규칙을 사용하여 최적화 문제를 해결할 수 있습니다.

-checkdiscard

-checkdiscard 규칙을 사용하면 삭제될 것으로 예상한 클래스 또는 멤버가 R8에 의해 성공적으로 삭제되었는지 확인할 수 있습니다. 지정된 클래스 또는 멤버가 삭제되지 않으면 빌드가 실패합니다.

-checkdiscard의 문법은 다음과 같습니다.


-checkdiscard <class_specification>

다음 예에서는 com.example.models.User 클래스의 userId 필드 또는 setLabel() 메서드가 유지되면 빌드가 실패합니다.

-checkdiscard class com.example.models.User{
  private java.lang.String userId;
  public void setLabel(java.lang.String);
}

메서드가 다른 클래스로 인라인 처리된 경우 클래스의 코드가 앱에 계속 있을 수 있습니다. 코드가 인라인 처리되지 않고 완전히 삭제되었는지 확인하려면 -checkdiscard-keep,allowshrinking 규칙과 결합하여 R8이 타겟 클래스에서 최적화를 실행하지 못하도록 하는 해당 규칙을 추가하세요. 이렇게 하면 클래스 병합 및 인라인 처리와 같은 최적화가 금지됩니다. -checkdiscard 규칙이 통과되면 일치하는 클래스의 콘텐츠가 최적화된 앱에 없습니다.

다음 예는 이 사용법을 보여줍니다.

# Either keep or remove the class, don't rename or otherwise optimize it
-keep,allowshrinking class com.example.foo { *; }

# Verify that the class and all of its fields and methods are removed.
-checkdiscard class com.example.foo

-whyareyoukeeping

-whyareyoukeeping 규칙을 사용하여 R8이 앱 빌드에서 특정 클래스, 필드 또는 메서드를 유지한 이유를 확인합니다. 항목은 여러 이유로 보관될 수 있지만 이 규칙은 보관된 항목에서 항목으로 가는 가장 짧은 경로를 설명하는 이유만 제공합니다. 코드에서 이 경로를 삭제해도 항목이 유지되지만 그 이유는 다를 수 있습니다.

가능한 이유는 다음과 같습니다.

  • 유지 규칙: 유지 규칙은 앱, 사용된 라이브러리 또는 AAPT (Android Asset Packaging Tool)에서 생성된 규칙에서 가져올 수 있습니다.

  • 보관된 코드 또는 리소스의 전이적 참조: R8에 의해 코드 또는 XML (예: 레이아웃)이 보관되면 정적으로 참조하는 모든 항목이 보관됩니다.

구문은 다음과 같습니다.


-whyareyoukeeping <class_specification>

예를 들면 다음과 같습니다.

-whyareyoukeeping class com.example.foo.MainActivity {
  private void setLabel(...);
}

출력이 콘솔에 출력됩니다.

setLabel()를 유지하는 규칙이 없으면 출력은 다음과 같습니다.

com.example.foo.MainActivity
|- is referenced in keep rule:
|  /app/build/intermediates/aapt_proguard_file/release/processReleaseResources/aapt_rules.txt:4:1
Nothing is keeping void com.example.foo.MainActivity.setLabel()

setLabel()를 타겟팅하는 유지 규칙이 있는 경우 출력은 다음과 비슷합니다.

com.example.foo.MainActivity
|- is referenced in keep rule:
| /app/proguard-rules.pro:23:1
void com.example.foo.MainActivity.setLabel()
|- is referenced in keep rule:
|  /app/proguard-rules.pro:23:1