1

I have my own developed Android app in kotlin. I lost my all source code (hard disk crashed) after that I pull my apk from my physical android device using below adb commands -

c:\> adb shell pm path com.digi

Response of this command is full path of apk. after that I used below command to get actual apk-

c:\>adb pull /data/app/com.digi-ZF6WfmctELhsLvm4oICrAQ==/base.apk Destination\folder

After that I used android studio and using build -> analyze APK This is giving some folder structure but did not get any kotlin class what I developed.

Is there any way to get at least kotlin source code?

Pravind Kumar
  • 809
  • 1
  • 11
  • 10
  • https://stackoverflow.com/questions/1249973/decompiling-dex-into-java-sourcecode You cannot retrieve the original Kotlin code, because the APK only has compiled Java bytecode in it. You can use a decompiler to generate Java code from the bytecode. It may be difficult to read in this state, especially if you used R8 minification. For future projects, use a VCS, and you can freely backup personal projects privately on Github or Bitbucket. – Tenfour04 Jul 12 '21 at 19:11
  • Yes, thats correct. Now I am doing the same. :) – Pravind Kumar Jul 15 '21 at 04:29

1 Answers1

4

Well, yes and no. You can't recover your source code fully, but you can at least get some of it. It will be partially gibberish and partially almost fine, you'll lose local variable names, comments, formatting, etc., so you will need to go through all files and fix them or even rewrite some of them entirely. But still it could be better than starting from scratch.

I did not decompile Android apps for a long time, so my knowledge may be outdated, but the standard procedure is:

  1. Convert the code from dex to jar.
  2. Decompile to Java.
  3. In your case: convert Java to Kotlin.

Ad.1. AFAIK there are two tools to do this: dex2jar and enjarify. I suggest using enjarify, it always gave me better results.

Ad.2. There are several Java decompilers and some of them will work better with some code, others will work better with another. I suggest trying at least Fernflower and JD-CORE/JD-GUI, maybe Krakatau.

I guess the results will be far from perfect, because the application is written in Kotlin, not in Java. Suspend functions and other features specific to Kotlin will be even worse.

You can also use ByteCodeViewer which is a GUI applications that simplifies the process of 1. and 2. It contains all above tools and more. You can also switch the decompiler dynamically to see results of different ones.

Ad.3. IntelliJ has some utils for converting Java to Kotlin. I never tried this with decompiled code and I guess it will be problematic

If you would need to recover the resources (XML files, etc.), you can try to use apktool.

broot
  • 21,588
  • 3
  • 30
  • 35
  • Thank you for this suggestions. However I have already performed some of steps but not got appropriate results. I got layout and and assets files but this is not exact as my code, like all reference name (id, and other attributes) has been changed and going through all elements. For, kotlin class I am reworking on this. :( – Pravind Kumar Jul 15 '21 at 04:24
  • 1
    Much better tan commandline tools like apktool is JADX which has also a GUI. It decompiles an APK file and shows the code with colors and allows to search inside. Very comfortable. Sadly a Kotlin plugin is missing which also would decode the Kotlin stuff. However it is much batter than the here recommended apktool. https://github.com/skylot/jadx – Elmue Dec 07 '21 at 00:35
  • The king is the APK Studio which gathered all of these tools together. – MSS Oct 14 '22 at 07:48