proguard Android项目时出错

时间:2012-01-04 06:49:16

标签: android ant proguard

我有项目A,它使用项目B作为库项目。他们都使用android-support-v4.jar作为引用库。当我尝试使用Android SDK提供的ant脚本构建项目A时,我在-obfuscate步骤中收到以下错误:

[proguard] Preparing output jar [E:\Workspace\GuosenAndroid1.1\bin\proguard\obfuscated.jar]
[proguard]   Copying resources from program jar [E:\Workspace\GuosenAndroid1.1\bin\proguard\original.jar]
[proguard]   Copying resources from program jar [E:\Workspace\GuosenAndroid1.1\libs\android-support-v4.jar]
[proguard] Warning: can't write resource [META-INF/MANIFEST.MF] (Duplicate zip entry [android-support-v4.jar:META-INF/MANIFEST.MF])
[proguard]   Copying resources from program jar [E:\Workspace\com_viewpagerindicator\bin\classes.jar]
[proguard] Warning: can't write resource [META-INF/MANIFEST.MF] (Duplicate zip entry [classes.jar:META-INF/MANIFEST.MF])
[proguard]   Copying resources from program jar [E:\Workspace\com_viewpagerindicator\libs\android-support-v4.jar]
[proguard] Warning: can't write resource [META-INF/MANIFEST.MF] (Duplicate zip entry [android-support-v4.jar:META-INF/MANIFEST.MF])

建立失败 E:\ Workspace \ GuosenAndroid1.1 \ basicbuild.xml:723:无法写入[E:\ Workspace \ GuosenAndroid1.1 \ bin \ proguard \ obfuscated.jar](无法读取[E:\ Workspace \ com_viewpagerindicator \ libs \ android-support-v4.jar](重复的zip条目[android-support-v4.jar:android / support / v4 / content / ModernAsyncTask $ WorkerRunnable.class]))

这是我的proguard配置:

-libraryjars libs/android-support-v4.jar
-dontwarn android.support.v4.**
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

-keep class android.support.** { *; }
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService

-keepclasseswithmembernames class * {
    native <methods>;
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembernames class * {
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

这是使用Proguard执行-obfuscate步骤的ant脚本:

<target name="-obfuscate">
    <echo>proguard enabled: ${proguard.enabled}</echo>
    <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="android.libraryjars" refid="android.target.classpath">
                <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.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>
        </then>
    </if>

我该怎么做才能摆脱这个错误?感谢。

2 个答案:

答案 0 :(得分:2)

android-support-v4.jar被包含在Proguard正在优化的代码集中两次,这是不允许的。你有两个地方包含android支持jar吗?这就是你的错误信息的真实情况。我认为你错过了一条线,它说明了你项目中另一个事件的位置。

答案 1 :(得分:0)

我刚解决了类似的问题。我正在使用viewpagerindicator库,它依赖于android支持库v4,我也使用android appcompat,它具有相同的依赖性。

所以我在Gradle中从viewpagerindicator中排除支持库依赖项,如下所示:

compile ('fr.avianey.com.viewpagerindicator:library:2.4.1') {
    // exclude redundant transitive dependency
    exclude (group: 'com.google.android', module: 'support-v4')
}