Home » Android » android – Convert class file to dex file

android – Convert class file to dex file

Posted by: admin June 15, 2020 Leave a comment

Questions:

How to convert class file to dex file in android?
Is there any way?

How to&Answers:

Use dx.

Answer:

Invoke “dx” command with “–dex” option like the following.

dx --dex --output=<output-file> <input-file>

“dx” command is contained in Android SDK. The location of the command varies. Try to search:

<SDK-HOME>/platform-tools/
<SDK-HOME>/platforms/<platform>/tools/

“dx –dex” can accept some options. For example, “–no-strict” option will skip checking whether path names of input class files match the declared package/class names, so you’ll get a different result for WebService.class if you invoke dx with this option. Probably, “–keep-classes” option is useful if you want to use jar files on both Android and Java SE. Without “–keep-classes” option, dx command replaces all .class files in an input jar file with one file named “classes.dex”, but with “–keep-classes” option, input .class files also go into the ouput file as well as classes.dex.

ex:
  dx --dex --keep-classes --output=output.jar input.jar

I read the source code of dx and listed up the command line options with some explanation at the following page.

Usage of dx –dex:

http://darutk-oboegaki.blogspot.com/2011/03/usage-of-dx-dex-dx-dex.html

Answer:

The dalvik docs area includes a trivial example.

Edit: Link is dead. You can find the source tree copy here, and a formatted version in a mirror here.

Answer:

For ANT automatic building:

 <property name="dx" value="${android_sdk}/build_tools/18.1.0/dx" />
 <target name="convert_jar_to_dex">
    <apply executable="${dx}" failonerror="true" parallel="true">   
            <arg value="-JXms1024m" />  
            <arg value="-JXss1024k" />  
            <arg value="--dex" />   
            <arg value="--verbose" />   
            <arg value="--no-strict" /> 
            <arg value="--num-threads=4" /> 
            <arg value="--output=${basedir}/bin/output.dex" />  
            <fileset file="${basedir}/input.jar"/>  
    </apply>
 </target>

This is my experience. May it be helpful.

Answer:

adding more info for darutk answer:

dx command is contained in Android SDK

example: D:\Android\AndroidSDK\build-tools\23.0.1\

with 23.0.1 is android version