14

I am a student interested in Android Security. I was trying to modify a APK file using baksmali and smali. However, I am not able to run the repackaged app in my mobile. When I click on the icon it say "Unfortunately, test has stopped" and exists. (Even the icon of the app got changed, now I see default android icon instead of old real colorful icon of the app)

What could be the reason for this ? Actually I haven't even modified code of the apk file. I just unzipped apk to get dex file, then I converted it to smali using baksmali.jar, and then back to dex using smali.jar. Finally zipped and signed.

What I have done in detail:

  1. Decompress the apk file

    $   Unzip test.apk
    
  2. Convert 1classes.dex1 to smali

    $ baksmali -x classes.dex -o smaliClasses
    
  3. Converted the classes back to classes.dex (replaced old classes.dex, in fact I did not add any new code to smali file. I wanted to know whether this works first).

    $ smali smaliClasses -o classes.dex
    
  4. Zip all the files to test.zip

    $ zip test.zip AndroidManifest.xml classes.dex res META-INF resourses.arsc
    
  5. Rename test.zip to test.apk

    $ mv test.zip test.apk
    

Now I believe I have to sign the APK again, please correct me if am wrong here.

Edited:

  1. java -jar signapk.jar testkey.x509.pem testkey.pk8 test.apk test-patched.apk

  2. I tried to install the new repackaged APK. Using adb shell. Adb shell showed it successfully installed. However, I am not able to run the repackaged app in mobile. The App crashes when I click on it. It says "Unfortunately, test has stopped".

Why doesn't the repackaged app running ? I don't understand what I am missing here ?

Edited:

I tried to repackage the same app using apktool. I extracted the smali files using it and repackaged. But why repackaging is not working with baksmali, smali, zip and signapk. Is zipping the real problem in this procedure? I see the size of the app is reduced drastically when I zip it and rename it to .apk compared to the original apk file :|

6
  • You should check your logcat for more information, if possible please post the crash log too.
    – xDragonZ
    Jan 18, 2015 at 18:51
  • @xDragonZ,I have edited the post. This time I tried to install using "adb install" and signed using signapk.jar. This time app got installed in phone. However, when I click on the app it crashes saying ""Unfortunately, app has stopped". One thing I noticed is size of the repacked apk is lesser than the orginal, is it the zip tool reason for that change is size ? Any clue on why the app crashes ? How do I get a log for this? Thank you so much.
    – aMa
    Jan 21, 2015 at 10:36
  • 1
    perhaps you also need to zipalign the file?
    – evlncrn8
    Jan 22, 2015 at 19:29
  • Are you deodexing (baksmali -x option) on purpose? You are it would appear that since you are performing this option, without providing a "bootclasspath" you're going to get some odd smali code that might not be able to converted back into a dex file. Also, just remove the META-INF folder prior to zipping and trying doing zip -r unsigned.apk * inside the directory with all the contents. If this still crashes, try posting the logcat output
    – diff
    Mar 3, 2015 at 8:46
  • 1
    So out of five answers not a single one is acceptable?
    – 0xC0000022L
    Apr 13, 2016 at 15:07

8 Answers 8

21

I use apktool for this purpose, and a short little pair of shell scripts for decompiling and recompiling APKs:

  • decompile-apk

    #!/bin/bash -e
    if ! [ "$1" ]; then
        echo "usage: $0 <file.apk>"
        exit -1
    fi
    
    fn=${1%.apk}
    target_apk=$fn.apk
    apktool d -f "$target_apk" -o smali
    echo "Done."
    
  • compile-apk

    #!/bin/bash -e
    if ! [ "$1" ]; then
        echo "usage: $0 <original.apk>"
        exit -1
    fi
    
    fn=${1%.apk}
    
    rm -f $fn.unaligned.apk $fn.smali.apk
    rm -rf smali/build
    
    apktool b -f smali/ -o $fn.unaligned.apk
    jarsigner -verbose -sigalg MD5withRSA -digestalg SHA1 -keystore ~/.android/debug.keystore  -storepass android  $fn.unaligned.apk  androiddebugkey
    zipalign -v 4 $fn.unaligned.apk $fn.smali.apk
    rm -rf smali/build
    

Using apktool has the advantage of being able to view and edit all of the resources as well as the decoded manifest file.

1
  • 1
    This the only one method that works for me. I've tried dozens, none worked. Thanks a lot.
    – Noury
    Jan 1, 2017 at 16:03
2

After playing with smali/baksmali I got it working. I think you didn't use the recursive flag when zipping. This caused the res folder to be empty which caused the crash. Also I run zipalign after signing the apk.

Steps:

  1. Unzip
    $ unzip test.apk
  2. Baksmali
    $ baksmali classes.dex -o smaliClasses
  3. Smali
    $ smali smaliClasses -o classes.dex
  4. Zip -r
    $ zip -r test.apk AndroidManifest.xml classes.dex res/ resources.arsc
  5. Jarsign
    $ java -jar signapk.jar testkey.x509.pem testkey.pk8 test.apk test-patched.apk
  6. Zipalign
    $ zipalign -v 4 test-patched.apk final-apk.apk
  7. Profit :)
1

I can't comment due to reputation, so I will post it here.

Since the job is done correctly with the use of apktool, it seems to me that something is going wrong with the zip process.

After repackaging the app, can you verify that the contents of the new APK are identical (as far as file names and structure is concerned) with the contents of the original one?

EDIT: also, since the file is a .dex file, I don't think that you need the -x option in baksmali.

EDIT2: When repackaging, you don't need to include the META-INF directory in the zip file. It will be created when signing the file.

1

Using the deodex option "-x" is not required since you're not performing baksmali on an odex. Just run baksmali on the dex file. You can also use apktool to unpack/pack everything, which I think is easier.

1
  • Why File size reduced?

    Because apktool optimized it while recompiling.

  • Why it crashed ?

    Possible reasons for crash:

    1. You cannot perform all steps in proper order.

    2. Application may have a CRC check for file size.

Tool Fully automated with GUI.. It's updated with recent android framework so no resource decompile error and better handling.

https://www.dropbox.com/s/02ifm4veotiuik1/apkstudio-2.0.3b-windows-Updates-Framework.rar?dl=0

And this small Paper which includes Tuts on reversing android apps and all basic info regarding to modify/handling apk.

https://www.dropbox.com/s/nkkmp4ait71kjku/Android%20Application%20Reversing%20Via%20Android%20Mobile.pdf?dl=0

0

If you are interested in Anroid apps RE, then you shall find better tools to use regularly.

Well, one of the tools is Android Cracker Kit (which I developed), it gives you all what you need: Android Cracker Kit (ACK):

3
0

I use APK Editor Pro to edit APKs right from my Android device!

https://play.google.com/store/apps/details?id=com.gmail.heagoo.apkeditor.pro

1
  • Great tip. The generated apk does not install correctly with the above (de)compile script (why ?) but replacing the classes.dex did it.
    – Zekitez
    Mar 5, 2020 at 15:55
0

Step 4. You forgot to do it recursively. It won't copy the files in /res when you zip it. Add -r option.

zip -r test.zip AndroidManifest.xml classes.dex res META-INF resources.arsc

Step 6. You don't need to sign it again since you re-packed it with META-INF directory to your apk already. Signing info is inside META-INF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.