Failed to recompile the instrumented code

Mobile Testing, Android App Testing.
bojan.zdravkovski
Posts: 1
Joined: Thu Feb 07, 2019 2:29 pm

Failed to recompile the instrumented code

Post by bojan.zdravkovski » Thu Feb 07, 2019 2:39 pm

I am on a trial version and getting to know Ranorex Studio, but I've hit a wall while trying to recompile the application.

I'm using the Ranorex Instrumentation Wizard and the log that comes back with the error is the following:
System.Exception: Failed to recompile the instrumented code. See details for more information: S: WARNING: Could not write to (C:\Users\bzdravkovski\AppData\Local\apktool\framework), using C:\Users\BZDRAV~1\AppData\Local\Temp\ instead...
S: Please be aware this is a volatile directory and frameworks could go missing, please utilize --frame-path if the default storage directory is unavailable
W: C:\Users\bzdravkovski\AppData\Local\RanorexStudio8\RxEnv\Android\temp\decomp\res\values\public.xml:20: error: Public symbol array/id_array declared here is not defined.
W: C:\Users\bzdravkovski\AppData\Local\RanorexStudio8\RxEnv\Android\temp\decomp\res\values\public.xml:21: error: Public symbol array/luis_region_array declared here is not defined.
W: C:\Users\bzdravkovski\AppData\Local\RanorexStudio8\RxEnv\Android\temp\decomp\res\values\public.xml:22: error: Public symbol array/luis_region_array_full_names declared here is not defined.
W: C:\Users\bzdravkovski\AppData\Local\RanorexStudio8\RxEnv\Android\temp\decomp\res\values\public.xml:23: error: Public symbol array/speech_region_array declared here is not defined.
W: C:\Users\bzdravkovski\AppData\Local\RanorexStudio8\RxEnv\Android\temp\decomp\res\values\public.xml:24: error: Public symbol array/speech_region_array_full_names declared here is not defined.
brut.androlib.AndrolibException: brut.common.BrutException: could not exec (exit code = 1): [C:\Users\BZDRAV~1\AppData\Local\Temp\brut_util_Jar_6187579830510168304.tmp, p, --forced-package-id, 127, --min-sdk-version, 23, --target-sdk-version, 27, --version-code, 1, --version-name, 6.4.165.57-Rev.0, --no-version-vectors, -F, C:\Users\BZDRAV~1\AppData\Local\Temp\APKTOOL911523478449189994.tmp, -0, arsc, -0, META-INF/android.arch.core_runtime.version, -0, META-INF/android.arch.lifecycle_extensions.version, -0, META-INF/android.arch.lifecycle_livedata-core.version, -0, META-INF/android.arch.lifecycle_livedata.version, -0, META-INF/android.arch.lifecycle_runtime.version, -0, META-INF/android.arch.lifecycle_viewmodel.version, -0, META-INF/android.arch.persistence.room_runtime.version, -0, META-INF/android.arch.persistence_db-framework.version, -0, META-INF/android.arch.persistence_db.version, -0, META-INF/com.android.support_animated-vector-drawable.version, -0, META-INF/com.android.support_appcompat-v7.version, -0, META-INF/com.android.support_cardview-v7.version, -0, META-INF/com.android.support_design.version, -0, META-INF/com.android.support_exifinterface.version, -0, META-INF/com.android.support_recyclerview-v7.version, -0, META-INF/com.android.support_support-compat.version, -0, META-INF/com.android.support_support-core-ui.version, -0, META-INF/com.android.support_support-core-utils.version, -0, META-INF/com.android.support_support-fragment.version, -0, META-INF/com.android.support_support-media-compat.version, -0, META-INF/com.android.support_support-v4.version, -0, META-INF/com.android.support_support-vector-drawable.version, -0, META-INF/com.android.support_transition.version, -0, png, -0, res/drawable-ldrtl-xxhdpi-v17/abc_spinner_mtrl_am_alpha.9.png, -0, res/drawable-xhdpi-v4/notification_bg_low_normal.9.png, -0, res/drawable-xhdpi-v4/notification_bg_low_pressed.9.png, -0, res/drawable-xhdpi-v4/notification_bg_normal.9.png, -0, res/drawable-xhdpi-v4/notification_bg_normal_pressed.9.png, -0, res/drawable-xxhdpi-v4/abc_ab_share_pack_mtrl_alpha.9.png, -0, res/drawable-xxhdpi-v4/abc_btn_switch_to_on_mtrl_00001.9.png, -0, res/drawable-xxhdpi-v4/abc_btn_switch_to_on_mtrl_00012.9.png, -0, res/drawable-xxhdpi-v4/abc_cab_background_top_mtrl_alpha.9.png, -0, res/drawable-xxhdpi-v4/abc_list_divider_mtrl_alpha.9.png, -0, res/drawable-xxhdpi-v4/abc_list_focused_holo.9.png, -0, res/drawable-xxhdpi-v4/abc_list_longpressed_holo.9.png, -0, res/drawable-xxhdpi-v4/abc_list_pressed_holo_dark.9.png, -0, res/drawable-xxhdpi-v4/abc_list_pressed_holo_light.9.png, -0, res/drawable-xxhdpi-v4/abc_list_selector_disabled_holo_dark.9.png, -0, res/drawable-xxhdpi-v4/abc_list_selector_disabled_holo_light.9.png, -0, res/drawable-xxhdpi-v4/abc_menu_hardkey_panel_mtrl_mult.9.png, -0, res/drawable-xxhdpi-v4/abc_popup_background_mtrl_mult.9.png, -0, res/drawable-xxhdpi-v4/abc_scrubber_primary_mtrl_alpha.9.png, -0, res/drawable-xxhdpi-v4/abc_scrubber_track_mtrl_alpha.9.png, -0, res/drawable-xxhdpi-v4/abc_spinner_mtrl_am_alpha.9.png, -0, res/drawable-xxhdpi-v4/abc_switch_track_mtrl_alpha.9.png, -0, res/drawable-xxhdpi-v4/abc_tab_indicator_mtrl_alpha.9.png, -0, res/drawable-xxhdpi-v4/abc_textfield_activated_mtrl_alpha.9.png, -0, res/drawable-xxhdpi-v4/abc_textfield_default_mtrl_alpha.9.png, -0, res/drawable-xxhdpi-v4/abc_textfield_search_activated_mtrl_alpha.9.png, -0, res/drawable-xxhdpi-v4/abc_textfield_search_default_mtrl_alpha.9.png, -0, res/drawable-xxhdpi-v4/common_google_signin_btn_icon_dark_normal_background.9.png, -0, res/drawable-xxhdpi-v4/common_google_signin_btn_icon_light_normal_background.9.png, -0, res/drawable-xxhdpi-v4/common_google_signin_btn_text_dark_normal_background.9.png, -0, res/drawable-xxhdpi-v4/common_google_signin_btn_text_light_normal_background.9.png, -0, res/raw/ats_keystore_bks, -0, res/raw/inspect2, -0, arsc, -I, C:\Users\BZDRAV~1\AppData\Local\Temp\1.apk, -S, C:\Users\bzdravkovski\AppData\Local\RanorexStudio8\RxEnv\Android\temp\decomp\res, -M, C:\Users\bzdravkovski\AppData\Local\RanorexStudio8\RxEnv\Android\temp\decomp\AndroidManifest.xml]

at Ranorex.Plugin.Mobile.Tools.Android.Instrumentation.ApkFileInstrumenter.CompileApkFile(String sourceApkFilePath, IPathService pathService)
at Ranorex.Plugin.Mobile.Utils.BackgroundWorkerSupported.ExecuteBgwCode[TArg0,TArg1](BgwDelegate`2 methodDelegate, Int32 doneProgress, String progressText, TArg0 arg0, TArg1 arg1, BackgroundWorker bgw)
at Ranorex.Plugin.Mobile.Tools.Android.Instrumentation.ApkFileInstrumenter.Instrument(String apkFilePath, InstrumentApkOptions deployOptions, BackgroundWorker bgw)
at Ranorex.Plugin.Mobile.RxIntegration.Instrumentation.AndroidWorker.Run()
at Ranorex.Controls.AndroidInstrumentProgressPage.bgw_DoWork(Object sender, DoWorkEventArgs e)
I've done some research on the subject and found out that ApkTool (if I'm not mistaken, you're using this tool to decompile the app), has a bug where it is not properly decompiling string-arrays and marks them as styles. I have successfully managed to manually change the decompiled xml's and build them through ApkTool, but can't seem to figure out how to do that with Ranorex.

Can someone please point out a workaround? I'm 100% sure that there are cases where people have successfully recompiled apps that have string-arrays as resources.

Thomas Gruber
Posts: 1
Joined: Wed Feb 21, 2018 11:22 am

Re: Failed to recompile the instrumented code

Post by Thomas Gruber » Thu Feb 14, 2019 9:17 am

We figured out a solution for this issue.
There is a bug in the apktool that wrongly uses <style> tags instead of <string-array> tags.

If you only use the apktool (without Ranorex) to decompile and compile your solution, you have to change the outputted arrays.xml file by changing the wrong recources from "style" to "string-array".

You can also intervene in the automatic apktool process in Ranorex by putting a batch file into <User>\AppData\Local\RanorexStudio9\RxEnv\Android.
Allowed batch file names are:
  • beforeInstrumentation.bat
  • afterInstrumentation.bat
  • afterCompilation.bat
  • beforeSigning.bat
  • afterSigning.bat
  • afterAligning.bat
These batch files are called automatically at the appropriate time in the process.

This specific issue was solved with the batch file "afterInstrumentation.bat", containing this snippet:
powershell.exe -executionpolicy bypass -file C:\Users\<USER>\AppData\Local\RanorexStudio8\RxEnv\Android\change_style_to_string-array.ps1 

And the "change_style_to_string-array.ps1" file containing this:
(Get-Content C:\Users\<USER>\AppData\Local\RanorexStudio8\RxEnv\Android\temp\decomp\res\values\arrays.xml).replace('<style', '<string-array') | Set-Content C:\Users\<USER>\AppData\Local\RanorexStudio8\RxEnv\Android\temp\decomp\res\values\arrays.xml

This general process can also be used to solve other issues with bugs in the apktool. As long as you know how to mitigate the issue using the apktool without Ranorex, you can use this process to also solve it in the instrumentation process in Ranorex.