Android 打正式包报错:Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'.

Android studio 打包时,打Debug包没有问题,直接运行也可以,但是每次打正式release包的时候就会报错:

Error:Execution failed for task ‘:app:transformClassesAndResourcesWithProguardForRelease’.
Job failed, see logs for details

并且在‘message’窗体中还打印了很多的警告信息,例如:

Warning:freemarker.ext.servlet.ServletContextHashModel: can't find referenced class javax.servlet.GenericServlet
Warning:freemarker.ext.servlet.ServletContextHashModel: can't find referenced class javax.servlet.ServletContext
Warning:freemarker.ext.servlet.ServletContextHashModel: can't find referenced class javax.servlet.GenericServlet
Warning:freemarker.ext.servlet.ServletContextHashModel: can't find referenced class javax.servlet.ServletContext
Warning:freemarker.ext.servlet.ServletContextHashModel: can't find referenced class javax.servlet.GenericServlet
Warning:freemarker.ext.servlet.ServletContextHashModel: can't find referenced class javax.servlet.ServletContext
Warning:freemarker.ext.servlet.ServletContextHashModel: can't find referenced class javax.servlet.GenericServlet
Warning:freemarker.ext.servlet.ServletContextHashModel: can't find referenced class javax.servlet.ServletContext
Warning:freemarker.ext.servlet.ServletContextHashModel: can't find referenced class javax.servlet.GenericServlet

百度了很多博客,都没有解决,后来仔细想了想,release包和debug包的区别就是签名文件和混淆配置不一样,感觉就是release包的混淆没有做好,然后就借鉴了混淆打包报错这篇文章,把所有的警告信息都解决和消除,然后再去打包就没问题,就可以直接编译成功了。

当然,如果你不想一个一个就添加混淆,也可以去偷懒的修改Gradle的配置信息,下面是正常打包配置信息

buildTypes {
        release {
            minifyEnabled true
            debuggable true
            buildConfigField ("boolean", "MY_DEBUG", "false")
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled false
            buildConfigField ("boolean", "MY_DEBUG", "true")
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

其中,minifyEnabled 主要用来控制是否运行混淆的,将其改为false,不进行混淆,就可以直接打包了,但是不建议如此做,这样很不规范,热切对于公司项目和代码来说很不安全。

最后博主在这建议大家,每次引用第三方开源包或者开源项目时,将混淆配置也加入进去,这个对于打release包很正要,不要去忽视这个,省的到时候出现错误时,还要回头挨个去配置混淆信息。

你可能感兴趣的:(Android)