How-to Obfuscate a Virtual Device Application with ProGuard

ProGuard is an open source command-line tool that shrinks, optimizes and obfuscates Java code.

This tutorial describes how to obfuscate the main Application of a Virtual Device.

This tutorial has been tested on:

  • MICROEJ DIST. 21.11 (MICROEJ SDK plug-in 5.8.2) running with JDK8.
  • Standalone Application project with build-firmware-singleapp version 2.2.+.

See the Proguard-Get-Started tutorial to learn more about ProGuard usage.

:warning: The obfuscation will only apply on the sources located in the src/main/java folder of the Application project.
If needed, the dependencies used by the Application project have to be obfuscated separately.

1) Enable ProGuard in the Application Project

  • Open the module.ivy file of the Application project.

  • Copy the following code inside <ea:build organisation=...></ea:build>:

    <ea:plugin organisation="com.is2t.easyant.plugins" module="obf-proguard" revision="3.0.0"/>
    

2) Obfuscate the Virtual Device Application JAR Binary

A custom script needs to be added in the Application project to update the Virtual Device Application JAR with the obfuscated one.

In the application project:

  • Create a module.ant file at the root of the project.
  • Add the following content inside:
    <project name="MyApplication" xmlns:ea="antlib:org.apache.easyant"  xmlns:ivy="antlib:org.apache.ivy.ant">
    
        <target name="update-player-firmware-jar" extensionOf="ivy-publication:publish-ready">
            <!-- Copy the obfuscated firwmare binary to the Virtual Device folder -->
            <copy file="${target}/artifacts/${module.name}.jar" tofile="${virtual.device.firmware.javalibs.dir}/${ivy.organisation}.${module.name}.jar"/>
            <!-- Delete the existing Virtual Device archive and build a new one -->
            <delete file="${target.artifacts}/${virtual.device.name}.zip"/>
            <zip destfile="${target.artifacts}/${virtual.device.name}.zip">
                <fileset dir="${player.dir}" includes="**/*" />
            </zip>
        </target>
    
    </project>
    

NOTE:

  • The module.pro file is optional in this case. The ProGuard plug-in is already configured to keep the entry point of the project.
  • If the application code is using reflexion, consider keeping the types and the default constructors listed in src/main/resources/*.types.list.

3) Check the Obfuscation

  • Build the application using Build Module.
  • Verify that ProGuard is called during the build, look for the following traces in the console:
    obf-proguard:obfuscate:
       [move] Moving 1 file to C:\Users\Documents\Work\a\target~\build-env
       [copy] Copying 1 file to C:\Users\Documents\Work\a\target~\proguard
    [proguard] ProGuard, version 5.1
    ...
    
  • Open the generated Virtual Device archive: target~/artifacts/{ApplicationName}.zip.
  • Extract the .jar binary located in platform/firmware/javaLibs/.
  • Use a Java decompiler to check that the code is actually obfuscated:

Alex for MicroEJ

1 Like