0
0
Android Kotlinmobile~20 mins

ProGuard and R8 optimization in Android Kotlin - Mini App: Build & Ship

Choose your learning style9 modes available
Build: ProGuardR8OptimizationScreen
This screen demonstrates how to enable and configure ProGuard and R8 optimizations in an Android Kotlin project. It shows a simple UI with a toggle to enable minification and a button to display the current optimization status.
Target UI
-------------------------
| ProGuard & R8 Setup   |
|-----------------------|
| Minify Enabled: [ ]   |
|                       |
| [Check Status]         |
-------------------------
Add a Switch to enable or disable code minification (ProGuard/R8).
Add a Button labeled 'Check Status' that shows a Toast with current minify status.
Use Kotlin and Android best practices.
Show how to configure build.gradle for enabling minification.
Starter Code
Android Kotlin
package com.example.proguardr8

import android.os.Bundle
import android.widget.Button
import android.widget.Switch
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class ProGuardR8OptimizationScreen : AppCompatActivity() {
    private lateinit var minifySwitch: Switch
    private lateinit var checkStatusButton: Button

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_proguard_r8_optimization)

        minifySwitch = findViewById(R.id.switch_minify)
        checkStatusButton = findViewById(R.id.button_check_status)

        // TODO: Add listener to minifySwitch to update build config
        // TODO: Add click listener to checkStatusButton to show minify status
    }
}
Task 1
Task 2
Task 3
Solution
Android Kotlin
package com.example.proguardr8

import android.os.Bundle
import android.widget.Button
import android.widget.Switch
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity

class ProGuardR8OptimizationScreen : AppCompatActivity() {
    private lateinit var minifySwitch: Switch
    private lateinit var checkStatusButton: Button
    private var isMinifyEnabled = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_proguard_r8_optimization)

        minifySwitch = findViewById(R.id.switch_minify)
        checkStatusButton = findViewById(R.id.button_check_status)

        minifySwitch.setOnCheckedChangeListener { _, isChecked ->
            isMinifyEnabled = isChecked
            Toast.makeText(this, if (isChecked) "Minify Enabled" else "Minify Disabled", Toast.LENGTH_SHORT).show()
        }

        checkStatusButton.setOnClickListener {
            val status = if (isMinifyEnabled) "Minification is ON" else "Minification is OFF"
            Toast.makeText(this, status, Toast.LENGTH_SHORT).show()
        }
    }
}

/*
In your module-level build.gradle file, add or update the following inside the android block:

android {
    buildTypes {
        release {
            minifyEnabled true // or false to disable
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

This enables R8/ProGuard minification and optimization for release builds.
*/

This screen uses a Switch to simulate enabling or disabling code minification, which is controlled in the build.gradle file for real projects. The Button shows the current minify status using a Toast message.

The key part of enabling ProGuard and R8 optimization is in the build.gradle file where minifyEnabled true turns on code shrinking and obfuscation. The app UI here helps beginners understand the concept interactively.

Final Result
Completed Screen
-------------------------
| ProGuard & R8 Setup   |
|-----------------------|
| Minify Enabled: [x]   |
|                       |
| [Check Status]         |
-------------------------
Tapping the switch toggles minify enabled state and shows a Toast 'Minify Enabled' or 'Minify Disabled'.
Tapping 'Check Status' button shows a Toast with current minify status: 'Minification is ON' or 'Minification is OFF'.
Stretch Goal
Add a TextView that displays the current build.gradle minifyEnabled setting dynamically.
💡 Hint
Use a ViewModel or saved state to keep track of minifyEnabled and update the TextView when the switch changes.