javaandroidminifytry-catch-finallyverifyerror

What would cause "class verified rejected" in Android developing with Java, when I turn on minifyEnable?


I have a class using try-catch-finally, and it works well in debug package. But when I turn on MinifyEnable, the app crashed with class verified rejected:

java.lang.VerifyError: Verifier rejected class A: ......: [0xFB6] copy1 v28<-v1 type=Imprecise Constant: 1 cat=3.

Turn off the minifyEnable can solve the error, but it is not the real reason absolutely. Moreover, a diffusion case happens when I just remove the finally part: the code works again! I couldn't understand what happened. And what I do in the finally part be like:enter image description here

Is there anyone can help me to figure out the real reason? Thanks a lot.


Solution

  • The java.lang.VerifyError: Verifier rejected class gennerally occurs when the bytecode does pass the verification of Android Runtime. It can be also caused due to optimization issues.

    1.Add specific Progaurd/R8 rules to keep the class unchanged at the time of minification.

    -keep class com.example.Test {
        *;
    }
    

    2.Optimizing the code inside the finally block.

    public class Test {
    
        public Result test() {
            Result result = new Result();
            int b = 0;
            try {
    
                b = 1;
                return result;
            } 
            catch (Exception e) {
                b = 2;
                return result;
            } 
            finally {
    
                setResultA(result, b);  //Some optimization here.
            }
        }
    
        private void setResultA(Result result, int value) {
            result.a = value;
        }
    
        class Result {
            int a;
        }
    }
    

    Hope this helps