{"id":2097,"date":"2021-11-29T16:14:42","date_gmt":"2021-11-29T15:14:42","guid":{"rendered":"https:\/\/blog.kodono.info\/wordpress\/?p=2097"},"modified":"2023-12-08T11:26:38","modified_gmt":"2023-12-08T10:26:38","slug":"debug-a-third-party-android-apk","status":"publish","type":"post","link":"https:\/\/blog.kodono.info\/wordpress\/2021\/11\/29\/debug-a-third-party-android-apk\/","title":{"rendered":"Debug a third party Android APK"},"content":{"rendered":"<p>(inspired by <a href=\"https:\/\/malacupa.com\/2018\/11\/11\/debug-decompiled-smali-code-in-android-studio-3.2.html\">this blog post<\/a>)<\/p>\n<h2>1) Install smalidea plugin<\/h2>\n<p>Download the <a href=\"https:\/\/bitbucket.org\/JesusFreke\/smalidea\/downloads\/\">smalidea plugin<\/a> (see also the related <a href=\"https:\/\/github.com\/JesusFreke\/smalidea\">Github Repository<\/a>).<\/p>\n<p>Open up <a href=\"https:\/\/developer.android.com\/studio\">Android Studio<\/a> and you should see the welcome screen like the one on screenshot below (if not, close your current project by selecting <code>File -> Close project<\/code>), go to the <code>Plugins<\/code> section, and from the wheel icon, select <code>Install Plugin from Disk...<\/code>. Select the smalidea plugin (ZIP file) you downloaded.<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture.png\" alt=\"Android Studio welcome screen\" width=\"812\" height=\"349\" class=\"aligncenter size-full wp-image-2098\" srcset=\"https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture.png 812w, https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-300x129.png 300w, https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-768x330.png 768w\" sizes=\"auto, (max-width: 812px) 100vw, 812px\" \/><\/p>\n<h2>2) Get the third party APK<\/h2>\n<p>You first need <strong>to know the type of platform<\/strong> where you&#8217;ll do your debug tests. To do so, make sure <b>your device is connected to your computer<\/b> (it could also be a virtual device started from the AVD Manager) with <code>adb devices<\/code>.<br \/>\nThen, use the command <code>adb shell getprop ro.product.cpu.abi<\/code> to find the type of processor you have. When I use my phone, I got <b>arm64-v8a<\/b>.<\/p>\n<p>Go to an APK platform, like <a href=\"https:\/\/apkcombo.com\/\">https:\/\/apkcombo.com\/<\/a> and search for the Android app you want to debug. Download the <b>APK version<\/b> that fits to the type you found before:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-1.png\" alt=\"screenshot of https:\/\/apkcombo.com\/\" width=\"992\" height=\"667\" class=\"aligncenter size-full wp-image-2100\" srcset=\"https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-1.png 992w, https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-1-300x202.png 300w, https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-1-768x516.png 768w\" sizes=\"auto, (max-width: 992px) 100vw, 992px\" \/><\/p>\n<h2>2bis) Have a look at the APK content<\/h2>\n<p>You can use <a href=\"https:\/\/github.com\/skylot\/jadx\">JADX<\/a> to open the APK and have a quick look at the code.<\/p>\n<h2>3) Decompile APK<\/h2>\n<p>With <a href=\"https:\/\/ibotpeaches.github.io\/Apktool\/\">APKTool<\/a>, we&#8217;ll use the command: <code>.\\apktool.bat d \".\\the_original_app_from_apkcombo.com.apk\" -o app_to_debug<\/code>.<br \/>\nA folder called <b>app_to_debug<\/b> is created with the decompiled version of the application.<\/p>\n<p>Next, we need to copy the source files: <b>create a folder called &#8220;src&#8221;<\/b> in the new <b>app_to_debug<\/b> folder, and type <code>cp -R smali*\/* src\/<\/code>.<\/p>\n<h2>4) Import project in Android Studio<\/h2>\n<p><b>Open an existing Android Studio project<\/b> and select the <code>app_to_debug<\/code> folder where you unpacked APK.<\/p>\n<p>Once the project loads, you need to tell the IDE where is your source code. Make sure you&#8217;re using the &#8220;Project view&#8221; in the left side panel:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-2.png\" alt=\"\" width=\"201\" height=\"143\" class=\"aligncenter size-full wp-image-2105\" \/><\/p>\n<p>Now you can see folder structure in your left panel. Find <code>src\/<\/code> subfolder right click it and select <code>Mark Directory as -> Sources Root<\/code>.<\/p>\n<h2>5) Prepare App for Debugging<\/h2>\n<p>Open <code>AndroidManifest.xml<\/code> from the <code>app_to_debug<\/code> and find the XML element <code>&lt;application&gt;<\/code>. Add the attribute <code>android:debuggable<\/code> with value <b>&#8220;true&#8221;<\/b>. Example:<\/p>\n<pre class=\"brush:xml\">\r\n&lt;application android:debuggable=\"true\" android:allowBackup=\"true\" android:icon=\"@mipmap\/ic_launcher\" android:label=\"@string\/app_name\" android:largeHeap=\"true\" android:name=\"org.horaapps.leafpic.App\" android:theme=\"@style\/Theme.AppCompat\"&gt;\r\n<\/pre>\n<h2>6) Repack to APK<\/h2>\n<p>You can now repack to APK with the command <code>.\\apktool.bat b -d \".\\app_to_debug\\\" -o app_unsigned.apk<\/code><\/p>\n<h2>7) Sign the APK<\/h2>\n<h3>7a) Create a keystore<\/h3>\n<p>You first need a keystore using <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/technotes\/tools\/unix\/keytool.html\">keytool<\/a> and type the below command:<br \/>\n<code>keytool -genkeypair -v -keystore mykey.keystore -alias mykey -keyalg RSA -keysize 2048 -validity 10000<\/code><\/p>\n<p>Several questions you&#8217;ll be asked, as well as a password. Make sure to remember the password for later.<\/p>\n<h3>7b) Validate the APK<\/h3>\n<p>You then need <code>zipalign<\/code> that can be found in the Android SDK folder (e.g. <em>C:\\Users\\USERNAME\\AppData\\Local\\Android\\Sdk\\build-tools\\31.0.0\\zipalign.exe<\/em>) to validate your APK:<br \/>\n<code>.\\Path\\to\\Android\\Sdk\\build-tools\\31.0.0\\zipalign.exe -f -v 4 .\\app_unsigned.apk .\\app_ready.apk<\/code><\/p>\n<h3>7c) Sign the APK<\/h3>\n<p>Finally you can sign the new created APK with <code>apksigner<\/code>:<br \/>\n<code>.\\Path\\to\\Android\\Sdk\\build-tools\\31.0.0\\apksigner.bat sign --ks .\\mykey.keystore --ks-key-alias app_to_debug --out .\\app_signed.apk .\\app_ready.apk<\/code><\/p>\n<h2>8) Install the APK<\/h2>\n<p>You can install it using <code>adb install app_signed.apk<\/code><\/p>\n<h2>9) Prepare the host<\/h2>\n<p>On your Android device, go to <code>Settings -> Developer options<\/code> and set <code>USB debugging<\/code> and <code>Wait for debugger options on<\/code>. The latter is optional but useful as it allows you wait for debugger connection and not to run app yet.<\/p>\n<p>Finally, you should tap on <code>Select debug app<\/code> and choose the app you just installed. After all of these, your Developer options menu should look somewhat like this:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-3.png\" alt=\"\" width=\"360\" height=\"619\" class=\"aligncenter size-full wp-image-2121\" srcset=\"https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-3.png 360w, https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-3-174x300.png 174w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><\/p>\n<p>Now, <b>launch the app<\/b> on the Android device, and you&#8217;ll get the below message:<br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-4.png\" alt=\"\" width=\"360\" height=\"234\" class=\"aligncenter size-full wp-image-2122\" srcset=\"https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-4.png 360w, https:\/\/blog.kodono.info\/wordpress\/wp-content\/uploads\/2021\/11\/Capture-4-300x195.png 300w\" sizes=\"auto, (max-width: 360px) 100vw, 360px\" \/><\/p>\n<h2>10) Forward debugger port<\/h2>\n<p>You can use the adb&#8217;s port forwarding feature and forward JDWP service where application&#8217;s debug interface is listening.<\/p>\n<p>Find the JDWP port with the command <code>adb jdwp<\/code>, then use this port with the command:<br \/>\n<code>adb forward tcp:5005 jdwp:JDWP_PORT<\/code><\/p>\n<h2>11) Connect Debugger<\/h2>\n<p>Go to Android Studio and from its top menu bar choose <code>Run -> Debug\u2026<\/code>, then a small message appears with one unique option that is <code>Edit Configurations...<\/code>. There, in the window, use a plus (+) button at the opt left, and add a new configuration of type <b>Remote<\/b>. Leave the default configuration as is. Click the Debug button and your app should be running with the attached debugger which means it will stop once a breakpoint is hit and you can investigate the content of app&#8217;s variables.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(inspired by this blog post) 1) Install smalidea plugin Download the smalidea plugin (see also the related Github Repository). Open up Android Studio and you should see the welcome screen like the one on screenshot below (if not, close your current project by selecting File -> Close project), go to the Plugins section, and from [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","hide_page_title":"","footnotes":""},"categories":[98,23,170,20],"tags":[99,156,123,155],"class_list":["post-2097","post","type-post","status-publish","format-standard","hentry","category-android","category-debug","category-english","category-niveau-expert","tag-android-2","tag-debug","tag-english","tag-niveau-expert"],"_links":{"self":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/2097","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/comments?post=2097"}],"version-history":[{"count":20,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/2097\/revisions"}],"predecessor-version":[{"id":2273,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/posts\/2097\/revisions\/2273"}],"wp:attachment":[{"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/media?parent=2097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/categories?post=2097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.kodono.info\/wordpress\/wp-json\/wp\/v2\/tags?post=2097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}