Java:如何使用自定义 Ant build.xml 将 ProGuard 集成到 Jar 项目中

Posted

技术标签:

【中文标题】Java:如何使用自定义 Ant build.xml 将 ProGuard 集成到 Jar 项目中【英文标题】:Java: How to integrate ProGuard in Jar Project using custom Ant build.xml 【发布时间】:2011-11-18 15:32:54 【问题描述】:

我有一个简单的 java 项目,它引用了两 (2) 个库 jar 文件。想要集成 ProGuard。

这是我当前的 build.xml:

<?xml version="1.0" ?>
<project name="Samples" default="dist" basedir=".">
    <!-- generate JAR START -->
    <description>Samples Library</description>
    <!-- Setting global properties for this build -->
    <property name="src" location="src" />
    <property name="bin" location="bin" />

    <target name="dist">
        <jar destfile="Samples.jar" basedir="bin/">
            <!-- Use ** to include the directory recursively -->
            <include name="com/samples/**" />
        </jar>
    </target>
</project>

【问题讨论】:

你提到的两个库jar文件在哪里?到目前为止,您尝试过什么? 【参考方案1】:

我找到了几种集成方法,但这是最简单的方法。

<?xml version="1.0" ?>
<project name="Samples" default="dist" basedir=".">
    <!-- generate JAR START -->
    <description>Samples Library</description>
    <!-- Setting global properties for this build -->
    <property name="src" location="src" />
    <property name="bin" location="bin" />

    <target name="dist">
        <jar destfile="Samples.jar" basedir="bin/">
            <!-- Use ** to include the directory recursively -->
            <include name="com/samples/**" />
        </jar>
    </target>
    <!-- generate JAR END -->

    <!-- The local.properties file is created and updated by the 'android'
         tool.
         It contains the path to the SDK. It should *NOT* be checked into
         Version Control Systems. -->
    <property file="local.properties" />

    <!-- The build.properties file can be created by you and is never touched
         by the 'android' tool. This is the place to change some of the
         default property values used by the Ant rules.
         Here are some properties you may want to change/update:

         source.dir
             The name of the source directory. Default is 'src'.
         out.dir
             The name of the output directory. Default is 'bin'.

         Properties related to the SDK location or the project target should
         be updated using the 'android' tool with the 'update' action.

         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems.

         -->
    <property file="build.properties" />

    <!-- The default.properties file is created and updated by the 'android'
         tool, as well as ADT.
         This file is an integral part of the build system for your
         application and should be checked into Version Control Systems. -->
    <property file="default.properties" />

    <!-- Add Proguard Tasks -->
    <property name="proguard.jar" location="proguard/lib/proguard.jar" />

    <taskdef resource="proguard/ant/task.properties"
         classpath="$proguard.jar" />
    <proguard configuration="proguard.cfg"/>

</project>

这是 proguard.cfg

-injars       Samples.jar
-outjars      Samples-out.jar
-libraryjars  libs/android.jar
-printmapping out.map

-renamesourcefileattribute SourceFile
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,
                SourceFile,LineNumberTable,*Annotation*,EnclosingMethod

-keep public class * 
    public protected *;


-keepclassmembernames class * 
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String, boolean);


-keepclasseswithmembernames class * 
    native <methods>;


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


-keepclassmembers class * implements java.io.Serializable 
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();

【讨论】:

此配置更适合处理库,因为它保留了所有公共类、字段和方法。对于您通常希望保留最少数量的入口点并混淆和优化其他所有内容的应用程序来说,它是次优的。 @EricLafortune,你能分享更适合某个应用程序的配置吗?【参考方案2】:

ProGuard 发行版包含manual page for the Ant task 以及目录examples/ant 中的一些完整示例构建文件。您可以指定任意数量的库 jar。

您的后续回答表明您正在开发一个 Android 应用程序。然后你应该看看Android example。

【讨论】:

以上是关于Java:如何使用自定义 Ant build.xml 将 ProGuard 集成到 Jar 项目中的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 ant 命令行来选择我的自定义 build.xml

如何自定义 Ant Design 的表单?

java如何打包

如何自定义 Ant.design 样式

如何使用 CodeSandbox 在 LESS 中为 Ant Design (React) 创建自定义主题?

如何在 Next.js 中使用自定义主题配置 Ant Design 并支持 CSS 模块功能