Android Proguard 混淆的 ANT 构建

发布于 2024-10-10 13:58:05 字数 98 浏览 7 评论 0原文

任何人都可以分享 Android 的示例/简单混淆 ANT 任务吗?假设我有完整的 APK,我只需要通过 *class hru Proguard,然后准备 *.dex 来构建 APK

Can anyone share with sample/simple obfuscation ANT task for Android? Provided that I do have complete APK and I need just pass *class hru Proguard and then prepare *.dex to build APK

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

望她远 2024-10-17 13:58:05

我找到了解决方案:

  1. 获取 Proguard - 将 proguard.jar 复制到已知目录(例如 MyProject/proguard)
  2. 准备 proguard.cfg - 描述内容和如何优化/混淆。 Proguard 的手册中详细描述了此过程,
  3. 准备遵循 ANT 的 build.xml(或类似这样的文件) - 非常感谢这家伙

更新完成build.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project name="MyProject" default="zipalign" basedir=".">
    <property name="target" value="android-8"/>
    <!--property file="default.properties" /-->
    <property name="encoding" value="UTF-8"/>

    <!-- dirs -->
    <property name="sdk.dir" location="Location of Android SDK"/>
    <property name="proguard.dir" value="proguard" />
    <property name="src.dir" value="src"/>
    <property name="gen.dir" value="gen"/>
    <property name="res.dir" value="res"/>
    <property name="assets.dir" value="assets"/>
    <property name="libs.dir" value="libs"/>
    <property name="out.classes.unoptimized.dir" value="out"/>
    <property name="out.classes.optimized.dir" value="out/optimized"/>

    <!-- files -->
    <property name="manifest.file" value="AndroidManifest.xml"/>
    <property name="signed.apk" value="${ant.project.name}-signed.apk"/>
    <property name="unsigned.apk" value="${ant.project.name}-unsigned.apk"/>
    <property name="final.apk" value="${ant.project.name}.apk"/>
    <property name="android.jar" value="${sdk.dir}/tools/platforms/${target}/android.jar"/>
    <property name="unoptimized" value="unoptimized.jar" />
    <property name="optimized" value="optimized.jar" />
    <property name="proguard.config" value="${proguard.dir}/proguard.cfg"/>

    <!-- tools -->
    <property name="dx.jar" value="${sdk.dir}/platform-tools/lib/dx.jar"/>
    <property name="aapt" value="${sdk.dir}/platforms/${target}/tools/aapt.exe"/>
    <property name="zipalign" value="${sdk.dir}/tools/zipalign.exe"/>
    <property name="jarsign" value="jarsigner.exe location is here"/>
    <property name="keystore" value="Your key store is here"/>
    <property name="keyalias" value="Your key alias is here"/>


    <path id="android.antlibs">
        <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
        <pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
        <pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
        <pathelement path="${sdk.dir}/tools/lib/apkbuilder.jar" />
        <pathelement path="${sdk.dir}/tools/lib/jarutils.jar" />
    </path>

    <taskdef name="setup"
        classname="com.android.ant.SetupTask"
        classpathref="android.antlibs" />
    <setup import="false"/>

    <!--taskdef name="aaptexec"
            classname="com.android.ant.AaptExecLoopTask"
            classpathref="android.antlibs" /-->

    <target name="clean" description="Removes output files created by other targets.">
        <echo>Cleaning...</echo>
        <delete dir="${out.classes.unoptimized.dir}" verbose="true" />
        <delete dir="${out.classes.optimized.dir}" verbose="true" />
    </target>

    <target name="dirs">
        <echo>Creating output directories if needed...</echo>
        <mkdir dir="${out.classes.unoptimized.dir}" />
        <mkdir dir="${out.classes.optimized.dir}" />
    </target>

    <!-- Compiles this project's .java files into .class files. -->
    <target name="compile" depends="dirs"
            description="Compiles project's .java files into .class files">
        <echo>Compiling sources...</echo>
        <javac encoding="${encoding}" target="1.6" debug="true" extdirs=""
                destdir="${out.classes.unoptimized.dir}"
                bootclasspathref="android.target.classpath"
                includeantruntime="true">
            <src path="${src.dir}" />
            <src path="${gen.dir}" />
            <classpath>
                <fileset dir="${libs.dir}" includes="*.jar" />
            </classpath>
        </javac>
    </target>

    <target name="preobfuscate" depends="compile">
        <echo>Preparing to obfuscation...</echo>
    <jar destfile="${unoptimized}"
       basedir="${out.classes.unoptimized.dir}"
       includes="**/**"
       excludes="optimized/**"
       />
    </target>

    <!--  Obfuscation with ProGuard   -->
    <target name="optimize" unless="nooptimize" depends="preobfuscate">
        <echo>Proguard obfuscation...</echo>
        <java jar="${proguard.dir}/proguard.jar" fork="true" failonerror="true">
            <jvmarg value="-Dmaximum.inlined.code.length=16" />
            <arg value="@${proguard.dir}/proguard.cfg" />
            <arg value="-injars ${unoptimized}" />
            <arg value="-outjars ${optimized}" />
            <arg value="-libraryjars ${android.jar}" />
        </java>
        <unzip src="${optimized}" dest="${out.classes.optimized.dir}" />
        <!-- Delete optimized jar (now unzipped into bin directory)  -->
        <delete file="${optimized}"/>
        <delete file="${unoptimized}"/>
    </target>

    <target name="dex" description="Converting JVM bytecodes into Dalvik bytecodes" depends="optimize">
        <echo>Converting bytecodes to Dalvik VM bytecodes...</echo>
        <java jar="${dx.jar}" fork="true">
            <arg line="--dex --verbose --output=${out.classes.optimized.dir}/classes.dex ${out.classes.optimized.dir}"/>
        </java>
    </target>

    <target name="aapt" depends="dex" description="compile resources">
        <echo>Packing resources...</echo>
        <exec executable="${aapt}" logerror="true" osfamily="windows">
            <arg line="p
            -f
            -M ${manifest.file}
            -I ${android.jar}
            -S ${res.dir}
            -A ${assets.dir}
            -F ${out.classes.optimized.dir}/${unsigned.apk}
            -m -J ${gen.dir}"/>
        </exec>
    </target>

    <target name="sign" depends="aapt" description="sign apk">
        <input message="Please enter keystore password (store:${keystore}):"
                       addproperty="keystore.password" />
        <echo>Signing apk...</echo>
        <exec executable="${jarsign}" logerror="true" osfamily="windows">
            <arg line="-verbose
            -keystore ${keystore}
            -storepass ${keystore.password}
            -signedjar ${out.classes.optimized.dir}/${signed.apk}
            ${out.classes.optimized.dir}/${unsigned.apk} ${keyalias}"/>
        </exec>
    </target>

    <target name="zipalign" depends="sign" description="zip align">
        <echo>Aligning apk...</echo>
        <exec executable="${zipalign}" logerror="true" osfamily="windows">
            <arg line="-f
            -v
            4
            ${out.classes.optimized.dir}/${signed.apk}
            ${final.apk}"/>
        </exec>
    </target>

</project>
  1. 此 ANT 任务必须添加到 Eclipse 的构建器(属性/构建器)任务中,位于 Java 构建器之后、Android 包构建器之前。

  2. 按“Build All”(最好在 Eclipse 菜单中关闭“自动构建”检查)

I have found solution:

  1. Get Proguard - copy proguard.jar into known directory (say MyProject/proguard)
  2. Prepare proguard.cfg -describing what and how optimize/obfuscate. This process thoroughly described in Proguard's manual
  3. Prepare following ANT's build.xml (or smth like this one) - great thanx to this guy

UPDATE complete build.xml

<?xml version="1.0" encoding="UTF-8"?> 
<project name="MyProject" default="zipalign" basedir=".">
    <property name="target" value="android-8"/>
    <!--property file="default.properties" /-->
    <property name="encoding" value="UTF-8"/>

    <!-- dirs -->
    <property name="sdk.dir" location="Location of Android SDK"/>
    <property name="proguard.dir" value="proguard" />
    <property name="src.dir" value="src"/>
    <property name="gen.dir" value="gen"/>
    <property name="res.dir" value="res"/>
    <property name="assets.dir" value="assets"/>
    <property name="libs.dir" value="libs"/>
    <property name="out.classes.unoptimized.dir" value="out"/>
    <property name="out.classes.optimized.dir" value="out/optimized"/>

    <!-- files -->
    <property name="manifest.file" value="AndroidManifest.xml"/>
    <property name="signed.apk" value="${ant.project.name}-signed.apk"/>
    <property name="unsigned.apk" value="${ant.project.name}-unsigned.apk"/>
    <property name="final.apk" value="${ant.project.name}.apk"/>
    <property name="android.jar" value="${sdk.dir}/tools/platforms/${target}/android.jar"/>
    <property name="unoptimized" value="unoptimized.jar" />
    <property name="optimized" value="optimized.jar" />
    <property name="proguard.config" value="${proguard.dir}/proguard.cfg"/>

    <!-- tools -->
    <property name="dx.jar" value="${sdk.dir}/platform-tools/lib/dx.jar"/>
    <property name="aapt" value="${sdk.dir}/platforms/${target}/tools/aapt.exe"/>
    <property name="zipalign" value="${sdk.dir}/tools/zipalign.exe"/>
    <property name="jarsign" value="jarsigner.exe location is here"/>
    <property name="keystore" value="Your key store is here"/>
    <property name="keyalias" value="Your key alias is here"/>


    <path id="android.antlibs">
        <pathelement path="${sdk.dir}/tools/lib/anttasks.jar" />
        <pathelement path="${sdk.dir}/tools/lib/sdklib.jar" />
        <pathelement path="${sdk.dir}/tools/lib/androidprefs.jar" />
        <pathelement path="${sdk.dir}/tools/lib/apkbuilder.jar" />
        <pathelement path="${sdk.dir}/tools/lib/jarutils.jar" />
    </path>

    <taskdef name="setup"
        classname="com.android.ant.SetupTask"
        classpathref="android.antlibs" />
    <setup import="false"/>

    <!--taskdef name="aaptexec"
            classname="com.android.ant.AaptExecLoopTask"
            classpathref="android.antlibs" /-->

    <target name="clean" description="Removes output files created by other targets.">
        <echo>Cleaning...</echo>
        <delete dir="${out.classes.unoptimized.dir}" verbose="true" />
        <delete dir="${out.classes.optimized.dir}" verbose="true" />
    </target>

    <target name="dirs">
        <echo>Creating output directories if needed...</echo>
        <mkdir dir="${out.classes.unoptimized.dir}" />
        <mkdir dir="${out.classes.optimized.dir}" />
    </target>

    <!-- Compiles this project's .java files into .class files. -->
    <target name="compile" depends="dirs"
            description="Compiles project's .java files into .class files">
        <echo>Compiling sources...</echo>
        <javac encoding="${encoding}" target="1.6" debug="true" extdirs=""
                destdir="${out.classes.unoptimized.dir}"
                bootclasspathref="android.target.classpath"
                includeantruntime="true">
            <src path="${src.dir}" />
            <src path="${gen.dir}" />
            <classpath>
                <fileset dir="${libs.dir}" includes="*.jar" />
            </classpath>
        </javac>
    </target>

    <target name="preobfuscate" depends="compile">
        <echo>Preparing to obfuscation...</echo>
    <jar destfile="${unoptimized}"
       basedir="${out.classes.unoptimized.dir}"
       includes="**/**"
       excludes="optimized/**"
       />
    </target>

    <!--  Obfuscation with ProGuard   -->
    <target name="optimize" unless="nooptimize" depends="preobfuscate">
        <echo>Proguard obfuscation...</echo>
        <java jar="${proguard.dir}/proguard.jar" fork="true" failonerror="true">
            <jvmarg value="-Dmaximum.inlined.code.length=16" />
            <arg value="@${proguard.dir}/proguard.cfg" />
            <arg value="-injars ${unoptimized}" />
            <arg value="-outjars ${optimized}" />
            <arg value="-libraryjars ${android.jar}" />
        </java>
        <unzip src="${optimized}" dest="${out.classes.optimized.dir}" />
        <!-- Delete optimized jar (now unzipped into bin directory)  -->
        <delete file="${optimized}"/>
        <delete file="${unoptimized}"/>
    </target>

    <target name="dex" description="Converting JVM bytecodes into Dalvik bytecodes" depends="optimize">
        <echo>Converting bytecodes to Dalvik VM bytecodes...</echo>
        <java jar="${dx.jar}" fork="true">
            <arg line="--dex --verbose --output=${out.classes.optimized.dir}/classes.dex ${out.classes.optimized.dir}"/>
        </java>
    </target>

    <target name="aapt" depends="dex" description="compile resources">
        <echo>Packing resources...</echo>
        <exec executable="${aapt}" logerror="true" osfamily="windows">
            <arg line="p
            -f
            -M ${manifest.file}
            -I ${android.jar}
            -S ${res.dir}
            -A ${assets.dir}
            -F ${out.classes.optimized.dir}/${unsigned.apk}
            -m -J ${gen.dir}"/>
        </exec>
    </target>

    <target name="sign" depends="aapt" description="sign apk">
        <input message="Please enter keystore password (store:${keystore}):"
                       addproperty="keystore.password" />
        <echo>Signing apk...</echo>
        <exec executable="${jarsign}" logerror="true" osfamily="windows">
            <arg line="-verbose
            -keystore ${keystore}
            -storepass ${keystore.password}
            -signedjar ${out.classes.optimized.dir}/${signed.apk}
            ${out.classes.optimized.dir}/${unsigned.apk} ${keyalias}"/>
        </exec>
    </target>

    <target name="zipalign" depends="sign" description="zip align">
        <echo>Aligning apk...</echo>
        <exec executable="${zipalign}" logerror="true" osfamily="windows">
            <arg line="-f
            -v
            4
            ${out.classes.optimized.dir}/${signed.apk}
            ${final.apk}"/>
        </exec>
    </target>

</project>
  1. This ANT task has to be added to Eclipse's builders (Properties/Builders) tasks after Java builder and before Android package builder.

  2. Press "Build All" (it's better to off Automatic Build check in Eclipse menu)

傲影 2024-10-17 13:58:05

Android 构建过程首先将 Java 源文件 (.java) 编译为 Java 类文件 (.class),然后将这些类文件转换为 Dalvik 代码 (classes.dex),最后将此 Dalvik 代码打包到 APK 文件中。

ProGuard 读取和写入 Java 类文件,因此必须在编译步骤和转换步骤之间将其插入到此管道中。它本身不会读取或写入 Dalvik 代码,因此无法处理 APK 文件。

ProGuard 上的 Android SDK 文档 讨论了如何在 Android 的 Ant 构建中启用混淆步骤 - 9.简而言之,您必须在文件 default.properties 中添加一行“proguard.config=proguard.cfg”,然后运行“ant release”。

The Android build process first compiles Java source files (.java) to Java class files (.class), then converts these class files into Dalvik code (classes.dex), and finally packages this Dalvik code in an APK file.

ProGuard reads and writes Java class files, so it has to be inserted into this pipeline between the compilation step and the conversion step. It doesn't read or write Dalvik code itself, so it can't work on the APK file.

The Android SDK documentation on ProGuard discusses how to enable the obfuscation step in the Ant build for android-9. In short, you have to add a line "proguard.config=proguard.cfg" to the file default.properties, and then run "ant release".

征﹌骨岁月お 2024-10-17 13:58:05

注意:barmaley的回复是2011年的,似乎对Android SDK Tools版本8或10有效。

我尝试使用Android SDK Tools版本18.1.1来适应这个解决方案,但一直失败错误:
taskdef class com.android.ant.SetupTask 找不到

最终,我所做的是:

rm build.xml
android update project -p .
  • 如果您的 PATH 中没有 SDK 工具,则需要使用 android 工具的完整路径,例如在 Windows 上:C:\Android\sdk\tools

这创建了一个新的 build.xml,它与当前的 SDK 工具兼容,并且似乎自动化了巴马利的回复中描述了很多手工工作。

之后,我能够运行 ant release,它负责构建和混淆开箱即用的结果 .apk 文件。

为了通过 ant 自动进行混淆,您需要:

  1. 启用 Proguard 混淆器< /a> (显然)
  2. 创建一个 ant.properties 文件并用适当的 key.store 参数填充它(请参阅 此回复了解详细信息)。

Attention: barmaley's reply is from year 2011, and seem to be valid for Android SDK Tools version either 8 or 10.

I tried adapting this solution using Android SDK Tools version 18.1.1, but kept failing on the error:
taskdef class com.android.ant.SetupTask cannot be found

Eventually, what I did was this:

rm build.xml
android update project -p .
  • If you don't have the SDK Tools in your PATH, you'll need to use the full path to the android tool, for example on Windows: C:\Android\sdk\tools

This created a fresh build.xml which is compliant with the current SDK Tools, and seem to automate a lot of the manual work that is described in barmaley's reply.

After that I was able to run ant release, which took care of building and obfuscating the result .apk file out of the box.

In order to automate obfuscation via ant, you'll need to:

  1. Enable Proguard obfuscator (obviously)
  2. Create an ant.properties file and fill it with the appropriate key.store params (see this SO reply for details).
梦在深巷 2024-10-17 13:58:05

proGuard 混淆过程需要 .class 文件,因此您无法在 IDE 构建 (.java) 之前或之后(.dex 打包)启动 Ant。

看一下这篇文章,其中解释了如何在全局 Ant 构建中添加 proGuard 步骤:

http://www.androidengineer.com/2010/07/optimizing-obfuscating-and-shrinking.html

如果你确实想使用IDEA构建,你可以尝试以下操作。

  • IDEA 构建完成后,使用 apktool 解压 apk。
  • 使用 dex2jar 将 .dex 文件转换为 .class
  • 运行 proGuard,如上一篇文章所示

抱歉,我没有附加 apktool 和 dexjar 的链接,但由于我是新手,我无法发布多个超链接。

The proGuard obfuscation process needs .class files so you can't launch an Ant before IDE build (.java) or after (.dex packed).

Have a look on this post where it's explained how add the proGuard step in your global Ant build:

http://www.androidengineer.com/2010/07/optimizing-obfuscating-and-shrinking.html

If you really want to use the IDEA build, you can try the following.

  • After the IDEA build unpack the apk with apktool.
  • Convert the .dex files to .class with dex2jar
  • Run proGuard as the previous post show you

Sorry that I don't attach you the links of apktool and dexjar but as I'm newbie I can't post more than one hyperlink.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文