[Shell Script]GetJava (get java code from apks instantly)

Search This thread

broodplank1337

Inactive Recognized Developer
Nov 24, 2011
4,992
10,155
Nijmegen
www.broodplank.net
I wrote this small shell script that basically extracts classes.dex from an apk/jar, then decompiles the classes.dex to jar classes, extracts the classes and converts them to java code.

Script got posted on the portal :D (http://www.xda-developers.com/android/getjava-helps-you-convert-apks-into-java-projects/)

Installation: (dont skip this step)
place it in ~/bin, give execute rights and then run
Code:
getjava --getdeps

Usage:
Code:
getjava file.apk [--debug]

--debug param is optional and gives extra output

when entering "getjava apkname.apk" you will get as output a folder called:

apkname.apk_java

which contains the decompiled classes.
obfuscation may scramble the code


Script:
Code:
#!/bin/bash
# getjava v1.0
# to get java code from apks instantly
# by broodplank
# 
# Credits: 
# Dex2Jar - Google
# JAD - Pavel Kouznetsov

if [[ ( $1 = "--getdeps" ) || ( $2 = "--getdeps" ) ]]; then
	echo "Installing required dependencies..."
        echo
        sudo apt-get install p7zip unzip wget -y
        wget -P ~ http://www.broodplank.net/files/getjava_dep.zip
        unzip -o ~/getjava_dep.zip -d ~/bin
	rm -f ~/getjava_dep.zip
        echo "Done, please restart the script"
	exit
fi;  


if [[ $2 = "--debug" ]]; then
	export DEBUGPARAM1="-d -v"
	export DEBUGPARAM2="-v"
else
	export DEBUGPARAM1=""
	export DEBUGPARAM2=""
fi;  


if [[ ( $1 = "" ) || ( $1 = "--help" ) || ( $1 = "-help" ) ]]; then
        echo "getjava -- get java from apk or jar instantly"
	echo "usage: getjava [apk/jar path] [options]"
        echo 
        echo "options:"
        echo " --getdeps		Installs all required dependencies automatically"
        echo " --debug		Show additional debugging information"
	echo
	echo "by broodplank"
	exit
else
	DIR=${1%/*}
	FILE=${1##*/}
        EXTENSION=${1##*.}
        OUTDIR="${PWD}/${FILE}_out"
fi;

rm -Rf ${OUTDIR}
rm -Rf ${PWD}/${FILE}_java
mkdir -p ${OUTDIR} 
7za x -yr -o${OUTDIR} ${1} classes.dex
d2j-dex2jar.sh ${DEBUGPARAM1} -os -ts ${OUTDIR}/classes.dex -o ${OUTDIR}/classes_dex2jar.jar
7za x -yr -o${OUTDIR}/out ${OUTDIR}/classes_dex2jar.jar
find -iname '*.class' -execdir jad ${DEBUGPARAM2} -o -r -s .java -d ${OUTDIR}/java -o {} \;
rm ${OUTDIR}/classes.dex ${OUTDIR}/classes_dex2jar.jar 
mv -f ${OUTDIR}/java ${PWD}/${FILE}_java
rm -Rf ${OUTDIR}
 
Last edited:

z720

Senior Member
Jan 4, 2009
138
11
After converted apk to java, the most important question is "did it run on java enabled phone" ? :eek:
 

defcomg

Senior Member
Dec 10, 2006
3,854
3,302
33
After converted apk to java, the most important question is "did it run on java enabled phone" ? :eek:

1w58.jpg
 

broodplank1337

Inactive Recognized Developer
Nov 24, 2011
4,992
10,155
Nijmegen
www.broodplank.net
After converted apk to java, the most important question is "did it run on java enabled phone" ? :eek:

Wat :silly:

So you're actually saying if the java code itself did run on a java enabled phone? which are all android devices. no it does not of course.
or if you mean if you're able to recompile the apk with the decompiled sources? if so, then in some cases you can but mostly on small apps.
the script is made for getting an insight on how the code is built.


+1:good:
 
  • Like
Reactions: 10tacleBoy

Jarmezrocks

Senior Member
Mar 25, 2011
960
495
Gold Coast
tinyurl.com
Wat :silly:

So you're actually saying if the java code itself did run on a java enabled phone? which are all android devices. no it does not of course.
or if you mean if you're able to recompile the apk with the decompiled sources? if so, then in some cases you can but mostly on small apps.
the script is made for getting an insight on how the code is built.



+1:good:
Hey dude,

Congrats! nice work. Have you done a side by side comparison of your script against DED or Android DARE? When I get home tonight I will give it a run and feed you back my results. Have you got a common APK that I could use as a control?

I will send you the resulting decompiled Java classes from DARE for you to look at :)
 
  • Like
Reactions: broodplank1337

broodplank1337

Inactive Recognized Developer
Nov 24, 2011
4,992
10,155
Nijmegen
www.broodplank.net
Hey dude,

Congrats! nice work. Have you done a side by side comparison of your script against DED or Android DARE? When I get home tonight I will give it a run and feed you back my results. Have you got a common APK that I could use as a control?

I will send you the resulting decompiled Java classes from DARE for you to look at :)

Thanks! No I haven't done any comparison actually, I just wrote it and shared it on Dev forums, I never expected it to hit the portal so it's still a really simple script, will extend it and make it better. If anyone would like to contribute to the script, take look at github (just made it) https://github.com/broodplank/GetJava

Attached an apk that is not obfuscated and has very decent java output

noice great work

any work making a repacker version?

Thanks, well implementing a repacker is not really the idea. it's because almost all apks will not repack successfully. What I can create tho is an option to export it to an eclipse project such as in ApktoJava (my windows version of this tool)
 

Attachments

  • GalaxyS4Settings.apk
    138.3 KB · Views: 157
  • Like
Reactions: justlovejoy

broodplank1337

Inactive Recognized Developer
Nov 24, 2011
4,992
10,155
Nijmegen
www.broodplank.net
@broodplank1337

Thx for this great script. This is what I were looking for some years ago. Good work :good:.

You're welcome :p it's always handy to have ;p

Quick question, how's this different from dex2jar?

Well if you read the OP you will see that it does more then just convert the dex to a jar file. sine thats the only thing dex2jar can do

I wrote this small shell script that basically extracts classes.dex from an apk/jar, then decompiles the classes.dex to jar classes, extracts the classes and converts them to java code.
 

elesbb

Senior Member
Jun 20, 2010
7,883
5,324
Well if you read the OP you will see that it does more then just convert the dex to a jar file. sine thats the only thing dex2jar can do

I wrote this small shell script that basically extracts classes.dex from an apk/jar, then decompiles the classes.dex to jar classes, extracts the classes and converts them to java code.

-.- yes i get that. Thats obvious from the thread title.. but you can use JDGui or other tools to get the java source from the jar files.. In terms of conversion, how is this different from dex2jar?
 

broodplank1337

Inactive Recognized Developer
Nov 24, 2011
4,992
10,155
Nijmegen
www.broodplank.net
-.- yes i get that. Thats obvious from the thread title.. but you can use JDGui or other tools to get the java source from the jar files.. In terms of conversion, how is this different from dex2jar?

well. JAD and JDgui (which I use in ApktoJava) are class to java decompilers, but dex2jar converts the dalvik byte code (classes.dex) to java classes. so they are very different, also dex2jar always does it's job correctly all the time, while java decompiler tend to fail now and then (to give you a reasonably readable code) :p
 
  • Like
Reactions: elesbb

arpitkh96

Senior Member
Feb 17, 2012
1,156
645
27
panipat
well. JAD and JDgui (which I use in ApktoJava) are class to java decompilers, but dex2jar converts the dalvik byte code (classes.dex) to java classes. so they are very different, also dex2jar always does it's job correctly all the time, while java decompiler tend to fail now and then (to give you a reasonably readable code) :p
If i use your script to decompile(dex2jar) systemui.apk.would i able to recompile it

Sent from my GT-S5570 using XDA Premium 4 mobile app
 

Fenny

Inactive Recognized Developer
Jun 28, 2007
1,386
2,073
F(x)tec Pro1-X
Google Pixel 6 Pro
One thing that dex2jar has a hard time with is switch/case statements, and big if/then/else ladders. When compiling down to dalvik code, these often get broken out to seemingly random parts of the function with just an if-eqz :labelname and a return statement. This often means that the code for the case statement shows up outside of the switch statement when converted back to .class files.

TLDR: No, most of the time the resulting java code will not recompile without some tweaking.
 
  • Like
Reactions: broodplank1337

elesbb

Senior Member
Jun 20, 2010
7,883
5,324
One thing that dex2jar has a hard time with is switch/case statements, and big if/then/else ladders. When compiling down to dalvik code, these often get broken out to seemingly random parts of the function with just an if-eqz :labelname and a return statement. This often means that the code for the case statement shows up outside of the switch statement when converted back to .class files.

TLDR: No, most of the time the resulting java code will not recompile without some tweaking.

Its impossible to recompile any java decompiled apk due to resource ids.


Im wondering if this scrip will return the //Internal error// in a lot of apps when decompiling.
 
  • Like
Reactions: joliroma

kelogs

Member
Jun 9, 2010
25
0
Not the best way to recover your lost work. Tried it on an APK I have written 2 years ago and I can barely recognize my code. Better than nothing I guess, thanks.
 

Top Liked Posts

  • There are no posts matching your filters.
  • 17
    I wrote this small shell script that basically extracts classes.dex from an apk/jar, then decompiles the classes.dex to jar classes, extracts the classes and converts them to java code.

    Script got posted on the portal :D (http://www.xda-developers.com/android/getjava-helps-you-convert-apks-into-java-projects/)

    Installation: (dont skip this step)
    place it in ~/bin, give execute rights and then run
    Code:
    getjava --getdeps

    Usage:
    Code:
    getjava file.apk [--debug]

    --debug param is optional and gives extra output

    when entering "getjava apkname.apk" you will get as output a folder called:

    apkname.apk_java

    which contains the decompiled classes.
    obfuscation may scramble the code


    Script:
    Code:
    #!/bin/bash
    # getjava v1.0
    # to get java code from apks instantly
    # by broodplank
    # 
    # Credits: 
    # Dex2Jar - Google
    # JAD - Pavel Kouznetsov
    
    if [[ ( $1 = "--getdeps" ) || ( $2 = "--getdeps" ) ]]; then
    	echo "Installing required dependencies..."
            echo
            sudo apt-get install p7zip unzip wget -y
            wget -P ~ http://www.broodplank.net/files/getjava_dep.zip
            unzip -o ~/getjava_dep.zip -d ~/bin
    	rm -f ~/getjava_dep.zip
            echo "Done, please restart the script"
    	exit
    fi;  
    
    
    if [[ $2 = "--debug" ]]; then
    	export DEBUGPARAM1="-d -v"
    	export DEBUGPARAM2="-v"
    else
    	export DEBUGPARAM1=""
    	export DEBUGPARAM2=""
    fi;  
    
    
    if [[ ( $1 = "" ) || ( $1 = "--help" ) || ( $1 = "-help" ) ]]; then
            echo "getjava -- get java from apk or jar instantly"
    	echo "usage: getjava [apk/jar path] [options]"
            echo 
            echo "options:"
            echo " --getdeps		Installs all required dependencies automatically"
            echo " --debug		Show additional debugging information"
    	echo
    	echo "by broodplank"
    	exit
    else
    	DIR=${1%/*}
    	FILE=${1##*/}
            EXTENSION=${1##*.}
            OUTDIR="${PWD}/${FILE}_out"
    fi;
    
    rm -Rf ${OUTDIR}
    rm -Rf ${PWD}/${FILE}_java
    mkdir -p ${OUTDIR} 
    7za x -yr -o${OUTDIR} ${1} classes.dex
    d2j-dex2jar.sh ${DEBUGPARAM1} -os -ts ${OUTDIR}/classes.dex -o ${OUTDIR}/classes_dex2jar.jar
    7za x -yr -o${OUTDIR}/out ${OUTDIR}/classes_dex2jar.jar
    find -iname '*.class' -execdir jad ${DEBUGPARAM2} -o -r -s .java -d ${OUTDIR}/java -o {} \;
    rm ${OUTDIR}/classes.dex ${OUTDIR}/classes_dex2jar.jar 
    mv -f ${OUTDIR}/java ${PWD}/${FILE}_java
    rm -Rf ${OUTDIR}
    4
    After converted apk to java, the most important question is "did it run on java enabled phone" ? :eek:

    1w58.jpg
    1
    After converted apk to java, the most important question is "did it run on java enabled phone" ? :eek:

    Wat :silly:

    So you're actually saying if the java code itself did run on a java enabled phone? which are all android devices. no it does not of course.
    or if you mean if you're able to recompile the apk with the decompiled sources? if so, then in some cases you can but mostly on small apps.
    the script is made for getting an insight on how the code is built.


    +1:good:
    1
    Wat :silly:

    So you're actually saying if the java code itself did run on a java enabled phone? which are all android devices. no it does not of course.
    or if you mean if you're able to recompile the apk with the decompiled sources? if so, then in some cases you can but mostly on small apps.
    the script is made for getting an insight on how the code is built.



    +1:good:
    Hey dude,

    Congrats! nice work. Have you done a side by side comparison of your script against DED or Android DARE? When I get home tonight I will give it a run and feed you back my results. Have you got a common APK that I could use as a control?

    I will send you the resulting decompiled Java classes from DARE for you to look at :)
    1
    noice great work

    any work making a repacker version?