Search code examples
androidlinuxbashshellapktool

Running apktool in a bash script


I am trying to write a bash script that decompiles several .apk files using apktool. Each apk file is located in a subdirectory of the sample folder.

#!bin/bash


for item in $(ls samples);
do
        for apk in $(ls "samples/$item");
        do 
            echo ./apktool/apktool d "./samples/$item$apk" 
            $(./apktool/apktool d "./samples/$item$apk")
        done    
done

When I run the script I get the following output:

./apktool/apktool d ./samples/ADRD/53dc.apk*
Input file (./samples/ADRD/53dc.apk*) was not found or was not readable.

The input file error message is the standard for when apktool cannot find a file. However, if I run the following command in the terminal the apktool will work correctly.

./apktool/apktool d ./samples/ADRD/53dc.apk*

I have changed the permissions of all the files located in the samples folder to rw for all users. I also have tried using sudo with the shell script, but this causes the script to hang. However, when I use sudo with the apktool in the command line it also hangs. Therefore, I am not sure if using sudo with apktool is doable.

Any help is appreciated, thanks.


Solution

  • So it looks like this ls gives you an output with an asterisk * appended at the end of the apk filename, because the file is executable.

        for apk in $(ls "samples/$item");
    

    This is not the default behaviour of ls, you are getting this probably because you have aliased ls to ls -F or similar. To bypass the alias, rewrite the line this way:

        for apk in $(\ls "samples/$item");
    

    Notice the \ I added there.

    BTW, is it normal that an apk file is executable? Perhaps you can remove the executable bit:

    find samples -name '*.apk' -exec chmod -x {} \;
    

    Also, possibly your script can be replaced with this one liner:

    find samples -name '*.apk' -exec ./apktool/apktool d {} \;
    

    Mind you, this is not exactly the same thing, because it may go deeper than two directories. If you need to limit the depth, that's possible too, see man find