How to convert .jar or .class to .dex file?
To avoid downloading the entire Android Studio:
You can go to command-tools download and extract the folder. Then go into it and find your sdkmanager
tool. Then on your command-line type sdkmanager "build-tools;28.0.1"
. You can select which version of build-tools is appropriate depending on the java version you are working with. This will download it into the root directory of your command-line tools. Go into this folder and you should see the dx
tool. Then you can type dx --dex --output="nameoffile.dex" nameoffile.jar
.
A little extra note: If you are trying to convert a massive jar you might get a OUT OF MEMORY
error. In this case you just pass the JVM the increase heap option dx -JXmx256m ...
.
Here is a solution that was helpful in my case...
Suppose .jar file sits in "c:\temp\in.jar". In command prompt window cd to ..\android-sdk\platform-tools. To get .apk execute:
dx --dex --output="c:\temp\app.apk" "c:\temp\in.jar"
To get .dex file execute:
dx --dex --output="c:\temp\dx.dex" "c:\temp\in.jar"
For mac, the dx command tool is stored in different directory as said in the top answer. Here is how you do it in mac:
exampleName$ /Users/exampleName/Library/Android/sdk/build-tools/25.0.2/dx --dex --output="/Users/exampleName/Downloads/classes-dex2jar.jar" "/Users/exampleName/Downloads/name.dex"
More detailed explanation:
- The dx tool is stored in the android SDK, but in a different directory on macs, the path for a mac is
/Users/exampleName/Library/Android/sdk/build-tools/25.0.2/
- The command is the same for windows and mac, the path is just different
- The command for both platforms is
dx --dex--output="c:\temp\app.apk" "c:\temp\in.jar"
- The tool may take a few minutes to produce the dex file
If it's still not working for some reason, make sure you are following these requirements:
I know this is late, but I had to figure this out on my own after hours of trial and error—hope it helps some other mac user