Android: Simplifying AGP updation with Proguard rules

Ayushi Khandelwal
3 min readMay 25

--

Keeping up with the latest tools and technologies is vital in the world of Android app development. Among these components, the Android Gradle Plugin (AGP) holds significant importance. Updating AGP offers several benefits, including improved performance, compatibility, security, and access to the latest technologies. Let’s explore why updating AGP is crucial for Android developers.

  1. Enhanced Performance: Updating AGP brings performance improvements, bug fixes, and optimizations. This results in faster compilation times, reduced memory usage, and overall improved app performance. Upgrading AGP ensures smooth app operation and a better user experience.
  2. Compatibility with Latest Tools: AGP updates align with new versions of Android Studio, Android SDKs, and other development tools. By updating AGP, developers ensure compatibility with the latest features, APIs, and libraries. This compatibility enables them to leverage advanced functionalities and maximize the capabilities of the Android platform.
  3. Reinforced Security: AGP updates address known security vulnerabilities, providing a more secure foundation for Android apps. By updating AGP promptly, developers protect user data and ensure the safety of their applications against potential threats.
  4. Access to Latest Technologies: AGP updates introduce support for new technologies, frameworks, and features in the Android ecosystem. By updating AGP, developers gain access to these advancements, enabling them to incorporate the latest tools and capabilities into their app development process. This ensures they stay up to date with emerging technologies and remain competitive.

That being said, there is a shortcut to kickstarting this process using

Tools > AGP Upgarde Assistant…

But but but… it is not as simple as it looks. The red lines are yet to follow…

When you start the upgrade using the Android Studio Tool, it will make the minimum required changes for you. The problems that you may encounter in this process and their solutions are listed below:

Problem #1 : jvm target compatibility

Solution : Make sure you update the sourceCompatibility, targetCompatibility and jvmTarget too inside your build.gradle to the latest stable version. The AGP assistant doesn’t do this for you. You need to do it yourself.

For example : You can change values from JavaVersion.VERSION_1_8 to VERSION_17

Problem #2 : Error in com.google.protobuf

Solution : To fix the “Unresolved reference” issue in build.gradle, you need to change imports in build.gradle from

import com.google.protobuf.gradle.builtins
import com.google.protobuf.gradle.generateProtoTasks
import com.google.protobuf.gradle.protobuf
import com.google.protobuf.gradle.protoc

to

import com.google.protobuf.gradle.*

Problem #3 : Error: ‘val Project.libs: LibrariesForLibs’ can’t be called in this context by implicit receiver

Solution : It is an issue that has not yet been resolved by the JetBrains team. Hence, to avoid this warning, use

@Suppress("DSL_SCOPE_VIOLATION")
plugins {
...
}

Also, in some cases, you may get the “ Expecting an expression” error.

This can be solved by adding println(“”) after the plugins block.

Problem #4 : Missing classes detected while running R8

Solution : Search for the “missing_rules.txt” file in your project and copy and paste its content to the “proguard-rules.pro” file. This will remove the warning, and your code will start running.

Problem #5 : java.lang.ClassCastException: java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

Solution : It seems to be an issue with Retrofit. https://issuetracker.google.com/issues/284177365

But you can fix it by adding the “-don’t warn” class name you got from missing_rules.txt and adding “**” at the end.

-dontwarn com.google.protobuf.java_com_google_android_gmscore_sdk_target_granule__proguard_group_gtm_N1281923064GeneratedExtensionRegistryLite**

-keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation

The need for using -keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation points to some kind of reflection going on and the error in ClassCastException between Class and ParameterizedType indicates that there is indeed a reflective use of some types.

Updating the Android Gradle Plugin (AGP) is crucial for Android developers aiming for optimal app performance, compatibility, security, and innovation. By keeping AGP up to date, developers can enhance app performance, leverage new technologies, ensure compatibility with the latest Android features, and strengthen app security. AGP updates empower developers to deliver high-quality Android apps that offer exceptional user experiences while staying ahead in the ever-evolving Android ecosystem.

--

--