javaandroidscalaproguardsbt-android-plugin

How to make ProGuard keep entire (whole) classes (with fields and methods) without using "-keep"


If I have a Scala Android project and I use these methods in my apk...

scala.collection.immutable.List#::
scala.collection.immutable.List#:::

..., after I use ProGuard on it, it will keep scala.collection.immutable.List class with only 2 methods, :: and :::.

Is there an option in ProGuard to keep ALL methods and fields inside a class, even though they aren't currently used, but without sending ProGuard this: -keep class scala.collection.immutable.List {*;}


Solution

  • I found out that the only way for me to do that is to determine which classes and methods are being used in my compiled classes.

    I mapped them via Map[String, Set[String]] data structure, where the key is the class name and the corresponding Set value contains all the methods used from that class.

    After that, it's just a matter of creating lots of -keep argumetns:

    def getProGuardKeepArgs(allDeps: MMap[String, MSet[String]]): String = {
        var sb = new StringBuilder()
        for ((clazz, methods) <- allDeps) {
            sb.append("-keep class " + clazz + " {\n")
    
            for (method <- methods) {
                sb.append(" *** " + method + "(...);\n")
            }
    
            sb.append("}\n\n")
        }
    
        sb.toString
    }