Warning: can't write resource [META-INF/MANIFEST.MF] (Duplicate zip entry [yyy.jar:META-INF/MANIFEST

转自:http://www.dancartoon.com/2012/01/14/fixing-proguard-warning-cant-write-resource-meta-infmanifest-mf/转自:


We're using ProGuard in our Android build and were getting a number of warnings regarding duplicate entries in the output jar. When ProGuard encounters resources in an input jar, it will by default copy the resources into the output jar. This is fine as long as you don't have multiple jars that contain resources with the same name. All jars(that I'm aware of) have manifest files, so attempting to use ProGuard with multiple input jars will cause warnings that look like: "Warning: can't write resource [META-INF/MANIFEST.MF] (Duplicate zip entry [yyy.jar:META-INF/MANIFEST.MF])".

The warnings are harmless, but a clean build is a happy build, so we might as well try and get rid of them. The ProGuard Manual actually describes what needs to be done, but it only provides a solution for the case where input jars are specified individually:

123
         
         
-injars in1.jar
-injars in2.jar(!META-INF/MANIFEST.MF)
-injars in3.jar(!META-INF/MANIFEST.MF)
view raw gistfile1.txt hosted with ❤ by  GitHub

When using ProGuard in the Android build process, the default behavior is to pass in all input jars in a giant delimited(semi-colon on Windows) blob. I have done some experimentation, but was unable to determine how an input filter can be used when multiple jars(having absolute paths) are passed in this way. Fortunately, it's not too difficult to change the build process to pass in a list of input jars individually and with individual filters.

This is the relevant section of the standard Android build.xml(starting around line 700):

123456789101112131415161718192021222324252627282930313233
         
         
<!-- Build a path object with all the jar files that must be obfuscated.
This include the project compiled source code and any 3rd party jar
files. -->
<path id= "project.jars.ref" >
<pathelement location= "${preobfuscate.jar.file}" />
<path refid= "jar.libs.ref" />
</path>
<!-- Set the project jar files Path object into a single property. It'll be
all the jar files separated by a platform path-separator.
Each path must be quoted if it contains spaces.
-->
<pathconvert property= "project.jars" refid= "project.jars.ref" >
<firstmatchmapper>
<regexpmapper from= '^([^ ]*)( .*)$$' to= '"\1\2"' />
<identitymapper/>
</firstmatchmapper>
</pathconvert>
 
<mkdir dir= "${obfuscate.absolute.dir}" />
<delete file= "${preobfuscate.jar.file}" />
<delete file= "${obfuscated.jar.file}" />
<jar basedir= "${out.classes.absolute.dir}"
destfile= "${preobfuscate.jar.file}" />
<proguard>
@${proguard.config}
-injars ${project.jars}
-outjars "${obfuscated.jar.file}"
-libraryjars ${android.libraryjars}
-dump "${obfuscate.absolute.dir}/dump.txt"
-printseeds "${obfuscate.absolute.dir}/seeds.txt"
-printusage "${obfuscate.absolute.dir}/usage.txt"
-printmapping "${obfuscate.absolute.dir}/mapping.txt"
</proguard>
view raw gistfile1.xml hosted with ❤ by  GitHub

The directives above create a new object that combines the location of the compiled jar along with other jars that were used during compilation. These locations are then joined into a string by the pathconvert task, which also does some transformation to ensure that paths containing spaces are quoted properly. Finally, the string from pathconvert(stored in project.jars) is passed into proguard via the statement: "-injars ${project.jars}"

Update(2013/02/11): paulpv has provided an updated config since the Android build system has changed since my original post. I have included his config here:

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
         
         
<?xml version="1.0" encoding="UTF-8"?>
<project name= "custom_rules" >
<!--
Suppress "Warning: can't write resource [META-INF/MANIFEST.MF] (Duplicate zip entry [classes.jar:META-INF/MANIFEST.MF])".
Per http://www.dancartoon.com/2012/01/14/fixing-proguard-warning-cant-write-resource-meta-infmanifest-mf/
Target "-obfuscate" copied from ${sdk.dir}/tools/ant/build.xml v21.
-->
<target name= "-obfuscate" >
<if condition= "${proguard.enabled}" >
<then>
<property name= "obfuscate.absolute.dir" location= "${out.absolute.dir}/proguard" />
<property name= "preobfuscate.jar.file" value= "${obfuscate.absolute.dir}/original.jar" />
<property name= "obfuscated.jar.file" value= "${obfuscate.absolute.dir}/obfuscated.jar" />
<!-- input for dex will be proguard's output -->
<property name= "out.dex.input.absolute.dir" value= "${obfuscated.jar.file}" />
<!-- Add Proguard Tasks -->
<property name= "proguard.jar" location= "${android.tools.dir}/proguard/lib/proguard.jar" />
<taskdef name= "proguard" classname= "proguard.ant.ProGuardTask" classpath= "${proguard.jar}" />
<!-- Set the android classpath Path object into a single property. It'll be
all the jar files separated by a platform path-separator.
Each path must be quoted if it contains spaces.
-->
<pathconvert property= "project.target.classpath.value" refid= "project.target.class.path" >
<firstmatchmapper>
<regexpmapper from= '^([^ ]*)( .*)$$' to= '"\1\2"' />
<identitymapper/>
</firstmatchmapper>
</pathconvert>
<!-- Build a path object with all the jar files that must be obfuscated.
This include the project compiled source code and any 3rd party jar
files. -->
<path id= "project.all.classes.path" >
<pathelement location= "${preobfuscate.jar.file}" />
<path refid= "project.all.jars.path" />
</path>
<!-- Set the project jar files Path object into a single property. It'll be
all the jar files separated by a platform path-separator.
Each path must be quoted if it contains spaces.
-->
<!--
Old:
<pathconvert property="project.all.classes.value" refid="project.all.classes.path">
New:
-->
<pathconvert property= "project.all.classes.value" refid= "project.all.jars.path" pathsep= " " >
<firstmatchmapper>
<!--
Old:
<regexpmapper from='^([^ ]*)( .*)$$' to='"\1\2"'/>
<identitymapper/>
New:
-->
<regexpmapper from= '^([^ ]*)( .*)$$' to= '-injars "\1\2"(!META-INF/MANIFEST.MF)' />
<regexpmapper from= '(.*)' to= '-injars \1(!META-INF/MANIFEST.MF)' />
</firstmatchmapper>
</pathconvert>
<!-- Turn the path property ${proguard.config} from an A:B:C property
into a series of includes: -include A -include B -include C
suitable for processing by the ProGuard task. Note - this does
not include the leading '-include "' or the closing '"'; those
are added under the <proguard> call below.
-->
<path id= "proguard.configpath" >
<pathelement path= "${proguard.config}" />
</path>
<pathconvert pathsep= '" -include "' property= "proguard.configcmd" refid= "proguard.configpath" />
<echo>proguard: $${project.all.classes.value}=${project.all.classes.value} </echo>
<mkdir dir= "${obfuscate.absolute.dir}" />
<delete file= "${preobfuscate.jar.file}" />
<delete file= "${obfuscated.jar.file}" />
<jar basedir= "${out.classes.absolute.dir}"
destfile= "${preobfuscate.jar.file}" />
<proguard>
-include "${proguard.configcmd}"
-include "${out.absolute.dir}/proguard.txt"
-injars ${preobfuscate.jar.file}
${project.all.classes.value}
-outjars "${obfuscated.jar.file}"
-libraryjars ${project.target.classpath.value}
-dump "${obfuscate.absolute.dir}/dump.txt"
-printseeds "${obfuscate.absolute.dir}/seeds.txt"
-printusage "${obfuscate.absolute.dir}/usage.txt"
-printmapping "${obfuscate.absolute.dir}/mapping.txt"
</proguard>
</then>
</if>
</target>
</project>
view raw custom_rules.xml hosted with ❤ by  GitHub

The goal of my changes is to preserve the manifest files from the original jar("preobfuscate.jar.file") and ignore(filter) the manifest files from the remaining input jar files. Since I haven't figured out how to filter input jars when passed in as a blob, I instead use the pathconvert task to transform each input jar location into a separate "-injars" statement with its own manifest filter. The property containing these locations is then passed into ProGuard directly via: "${project.jars}"

The solution isn't as clean as I would have liked, but it does fix the warnings.


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值