yashsingh1/xdaforums huggingface dataset:
[Android Software/Hacking General [Developers Only]] osm0sis:
Script/Patch Reference

Properties / Variables

(envvar.sh)

banner="Facebook Contacts Sync Enabler";
apklist="ContactsProvider.apk";
apkbak=/data/media/APK-Backup;
backup=1;
cleanup=1;

banner is the name of your patch zip, usually suggestive of what it does, to be displayed at the beginning of the zip flash. You should include your name/handle here like "by osm0sis @ xda-developers" for credit purposes.

apklist is a string containing the list of APKs to be patched included in the patch zip, separated by spaces between the quotes. Each APK is automatically found recursively in /system, then copied to the working directory to be decompiled and acted on, then copied back to /system.

apkbak is the location to place backups of the untouched APKs in apklist if backup=1 is set.

backup=1 will store backups of the untouched APKs in the location specified in apkbak.

cleanup=0 will keep the zip from removing it's working directory in /tmp/apkpatcher or any of the files resulting from the smali process - this can be useful if trying to debug in adb shell whether the patches worked correctly. cleanup=1 is necessary on multi-APK patching zips to clean the baksmali classout directory for the next APK, so it's recommended each APK to be patched be tested on their own with cleanup=0 before combining into a single zip.
[Android Software/Hacking General [Developers Only]] Surge1223:
Did you happen to figure out how to cross compile jdk for arm? (different from the arm sources available from oracle etc)

..and nice work!
[Android Software/Hacking General [Developers Only]] osm0sis:
Nah, just forced bak/smali and apktool to run by building/adding classes.dex for each of them and figuring out how to call a functional dalvikvm from recovery. :cowboy::cool:

The first part was mentioned in passing (and quite awesomely) by JesusFreke back in 2011.

That got it running in booted Android. Then running from recovery was a whole other issue, but I stumbled upon Jim Huang's work on executing dalvikvm on Linux, and that lead me to figuring out a solution.

As it stands, apktool should technically be able to do both the classes and resources parts of things (since it incorporates baksmali and smali), but for some reason it bails in recovery on decompiling the classes.dex where baksmali standalone doesn't. So perhaps in future revisions apktool could be the only jar required.

Similarly, apktool also bails when rebuilding resources.asrc because it can't call its internal(?) aapt, so I've included aapt compiled as PIE since that's the only working one I could find; in the future this could go too if apktool resolves it, or at the very least it would be more universal if we had a static compile of aapt to go with the other static binaries included.

Other possible items on the to-do list would include adding zip re-signing (currently it uses the apktool /system APK trick of keeping META-INF the same), reflashing to revert to the backup APKs, and adding busybox to always have an expected execution environment like I recently did on AnyKernel2 to work around Cyanogen Recovery and toybox missing basic stuff like awk at the moment. These aren't something I'm going to be actively working on since it works and APK modding isn't really a focus for me, but since I figured out how to automate the process I wanted to provide the WIP template for the modding community. :good:
[Android Software/Hacking General [Developers Only]] Surge1223:
Nice! I tried to compile aapt from my device a few mins ago just for s&g's got pretty far actually before it couldn't compile some external android libs because I didn't have them downloaded.

Have you ever seen the Android apktool apk by pqy330? It's not on git anymore for some reason, but the guy figured out how to get java, javac, aapt, aidl, and everything working on arm, only required a LD_LIBRARY_PATH export. I wish I could get a hold of the guy to ask him about getting some of those tougher arm static binaries compiled.

And yeah I wouldn't actively spend hours working on this if it's already in a working state like you said, it's just interesting compared to the other mundane things I read of xda lol. Especially new developments. Great job man.
[Android Software/Hacking General [Developers Only]] osm0sis:
APK-Patcher: minor fixes, binary updates:

- allow for recoveries without busybox

- update tool compiles to my newer zipalign and static(!) aapt builds

- strip zip binary to compensate for larger aapt and zipalign binaries

- simplify aapt usage now that we have a static arm binary

- configure zipalign for Marshmallow system APK handling

https://github.com/osm0sis/APK-Patcher/commit/af24805f06e2e2b92f2bfeeb1490d6022073c4b5

Thanks [MENTION=4939386]Surge1223[/MENTION] for the help with figuring out compiling zipalign and aapt statically for arm. :) :highfive:
[Android Software/Hacking General [Developers Only]] AL_IRAQI:
Wow, i wish that i know this patcher before

so im trying to add two lines after line #24 in "res/xml/tuner_prefs.xml"

i tried

sed -i '24 i\> <Preference android:title="@string/nav_bar" android:key="nav_bar" android:fragment="com.android.systemui.tuner.NavBarTuner" />' res/xml/tuner_prefs.xml;
sed -i '25 i\> <Preference android:title="@string/color_and_appearance" android:key="color_transform" android:fragment="com.android.systemui.tuner.ColorAndAppearanceFragment" />' res/xml/tuner_prefs.xml;

and this also

sed -i '24 i\> <Preference android:title="@string/nav_bar" android:key="nav_bar" android:fragment="com.android.systemui.tuner.NavBarTuner" />' res/xml/tuner_prefs.xml;
sed -i '25 i\> <Preference android:title="@string/color_and_appearance" android:key="color_transform" android:fragment="com.android.systemui.tuner.ColorAndAppearanceFragment" />' res/xml/tuner_prefs.xml;

and it doesn't work, im really sorry i never used sed commands before :p

do i have to add original file in "patch/SystemUI/res/xml" and then the script should change it ? :confused:
[Android Software/Hacking General [Developers Only]] osm0sis:
Nah, per the OP, the "patch" directory is only for replacing files whole, which you shouldn't do to keep it universal.

You just need a working sed command in the script directory.

You can borrow this script function from my AnyKernel2 to make the insertion easier for you as a sed beginner:

# insert_line <file> <if search string> <before|after> <line match string> <inserted line>
insert_line() {
if [ -z "$(grep "$2" $1)" ]; then
case $3 in
before) offset=0;;
after) offset=1;;
esac;
line=$((`grep -n "$4" $1 | head -n1 | cut -d: -f1` + offset));
sed -i "${line}s;^;${5}\n;" $1;
fi;
}

Then follow that with an insert_line command using the syntax provided. "if search string" tests whether the mod has been completed already, then "line match string" is the line you want to put the inserted lines before/after.

You can insert both lines at once with a \n between as well. :)
[Android Software/Hacking General [Developers Only]] AL_IRAQI:
im such a noob, even this basic code is hard for me to understand :crying:

do i have to keep grep commands ?, because i dont need to replace or find something in the file, only inserting after line #24

$1 is the file here right ?, and your trying to find $2 and add someting, and in case of 3$ there is another case, and edits will probably be before/after $4 ?
[Android Software/Hacking General [Developers Only]] osm0sis:
What is on line 24? Wouldn't it be more universal to match against some part of the contents of the current line 24 to ensure its always in the right place, even if the number changes?

The $1 $2 $3 $4 $5 correspond to each argument supplied as per the <syntax> in the #commented line.
[Android Software/Hacking General [Developers Only]] AL_IRAQI:
i modded systemui for N5X,N6/6P,N9 and android one device, all of them has the same layout

or i can search for "<Preference android:title="@string/other"" and add it before it

oh, now its easier to understand ;P
[Android Software/Hacking General [Developers Only]] osm0sis:
Okay, so.. Probably:

insert_line res/xml/tuner_prefs.xml "com.android.systemui.tuner.NavBarTuner" before "com.android.systemui.tuner.OtherPrefs" "<Preference android:title=\"@string/nav_bar\" android:key=\"nav_bar\" android:fragment=\"com.android.systemui.tuner.NavBarTuner\" />\n<Preference android:title=\"@string/color_and_appearance\" android:key=\"color_transform\" android:fragment=\"com.android.systemui.tuner.ColorAndAppearanceFragment\" />";
[Android Software/Hacking General [Developers Only]] AL_IRAQI:
i removed /patch and added the script to SystemUI-res.sh, but it doesn't seems to work

screenshot:
[Android Software/Hacking General [Developers Only]] osm0sis:
You shouldn't see that message for what you're doing unless you've got things set up weird. Can you just attach your WIP zip and I'll take a look?
[Android Software/Hacking General [Developers Only]] AL_IRAQI:
here you are, link

Thanks :D
[Android Software/Hacking General [Developers Only]] osm0sis:
Okay, try this. I've got you about 90% of the way there; fixed all the weirdness with the unnecessary patch folder, extra files and extracmd. The sed insertion is still untested, so you'll have to check that out, and from what you and others have told me you'll need to update the included tools for Nougat support.

Download the newer bak/smali and apktool, then that's done with

dx --dex --output=classes.dex <file>.jar

from within the latest SDK. Add the classes.dex to the jar (it's a zip). Name them like the current ones (version numbers, etc.) and put them in tools and remove the old ones. :good:

[ Attachment removed. ]
[Android Software/Hacking General [Developers Only]] AL_IRAQI:
Awesome, ill try it today :D :fingers-crossed:

Edit:

i already downloaded newer tools, and now i have to run this code ? :D

dx --dex --output=classes.dex apktool_2.0.3.jar
dx --dex --output=classes.dex baksmali-2.1.1.jar
dx --dex --output=classes.dex smali-2.1.1.jar
[Android Software/Hacking General [Developers Only]] osm0sis:
[MENTION=4648515]AL_IRAQI[/MENTION], sorry, just saw your edit. The latest bak/smali is 2.1.3, and latest apktool is 2.2.0, but otherwise yes, you run that command on each jar separately from the build-tools directory of the latest Android SDK, and put the resulting classes.dex inside the jar (which is a zip file). Then it's able to be run on Android as in my zip. :)
[Android Software/Hacking General [Developers Only]] osm0sis:
New commit!

tools: binary updates:

https://github.com/osm0sis/APK-Patcher/commit/e4f6e3e90e13c862652183460a86e49cb0514fd9

Since I felt bad [MENTION=4648515]AL_IRAQI[/MENTION] didn't appear to be able to figure it out for his mods. :(:angel:
[Android Software/Hacking General [Developers Only]] osm0sis:
From an incorrect thread:

Seems like you're in the wrong thread? I'm not sure. You can try reverting to the previous builds of tools, but beyond that I'm not sure at the moment. It's on my to-do to test everything out again in the near future and resolve any issues I can come up with. Please reply in the APK-Patcher thread.
[Android Software/Hacking General [Developers Only]] aj20010319:
Yea was in the wrong thread. I will try reverting to previous tools.
[Android Software/Hacking General [Developers Only]] aj20010319:
Okay so I tried using older tools but it doesn't work. One thing is sure that it is not because of tools, I tried using the same tools to decompile same framework-res.apk in my PC and it is working. Also the zips are okay, they work fine, I tried decompiling the apk in shell but I get the same error.

Are some libraries missing?

Didn't find class "javax.imageio.ImageIO" on path: DexPathList[[zip file "/tmp/apkpatcher/tools/apktool.jar"],nativeLibraryDirectories=[/system/lib]]
[Android Software/Hacking General [Developers Only]] aj20010319:
So, I was searching for this thing and it seems Android doesn't include this class "ImageIO" or the complete Java AWT Classes anymore, except fonts. It was there in froyo though,

https://android.googlesource.com/pl.../froyo-release/awt/javax/imageio/ImageIO.java

So, we need to port it to Android?
[Android Software/Hacking General [Developers Only]] osm0sis:
Interesting find!

Basically whatever jar is in your device's framework dir that has ImageIO needs to be included in my minimal Dalvik path to avoid that error.

It's a tricky balance because if you load everything, it dexopts them all to a temp cache, so that takes forever to flash. That's why I got it down to bare bones of what would function in my testing.

Once you figure it out let me know and I'll add it to the list by default.
[Android Software/Hacking General [Developers Only]] aj20010319:
Okay I will try to search it.
[Android Software/Hacking General [Developers Only]] aj20010319:
[MENTION=4544860]osm0sis[/MENTION] so I tried to search the file which has that class but I couldn't find. I hope that I did it the correct way, I got the classes.dex from the .jar then used dexdump and searched for "imageio" but no such class is there, I did it for all the files, the only .jar which had the keyword is com.google.android.maps but that's not a class, it is something under jpegutil.java, I don't understand anything else from there. Here is the text of that part (class #172 - JpegUtil.java),

http://pastebin.com/vCxtHqtW

Complete dump is here,

https://drive.google.com/open?id=0B5Duhbztps7la203WURzZjVlYWM
[Android Software/Hacking General [Developers Only]] osm0sis:
Not sure why there would be no match. Can you upload the file you're trying to baksmali so I can take a look?
[Android Software/Hacking General [Developers Only]] aj20010319:
I didn't baksmali anything, i just used dexdump from build tools on every classes.dex of every jar file.
[Android Software/Hacking General [Developers Only]] osm0sis:
I mean upload your framework-res.apk so I can try and figure it out. :)
[Android Software/Hacking General [Developers Only]] aj20010319:
Oh...here it is.
[Android Software/Hacking General [Developers Only]] osm0sis:
[MENTION=4280397]djb77[/MENTION] This project is due for a bit of debugging again now that I've got the updated utilities for Nougat in there.

I don't have much time, but it's on my list. I'll be happy to help how I can if you post any trouble you run into here. :)
[Android Software/Hacking General [Developers Only]] djb77:
Thanks for the response :)

I'll wait until you get a chance to update the code and then try and implement it again, this may be the reason why I can't get it to work 100%.

I have noticed that it had fun with 64bit compatibility, as in the recovery log (when it tries to locate lib files) that the libs aren't the correct "bit". I tried setting it to both lib and lib64 for tests, but when it was set to lib it would want 64bit files, and if it was set to lib64 it would want 32bit files.

The device I'm using for testing is the Galaxy S7 btw.
[Android Software/Hacking General [Developers Only]] Ibuprophen:
Have you taken a look at the following 2 links as regards to redirecting a 32-bit dll to 64-bit?

Microsoft File System Redirector (Windows)

SamLogic Visual Installer - Installation Software

I had performed this a handful of times before and, when you had mentioned what you had attempted to do, i had located some of my saved links and thought I'd share these two to, possibly, help you out.

Good Luck!
[Android Software/Hacking General [Developers Only]] djb77:
I'm not talking about windows, i'm talking about the .sh script inside the installer, which is all android (linux)
[Android Software/Hacking General [Developers Only]] Ibuprophen:
Ooooooohhhhhh... LOL!

Sorry for the mix up.

As you were :cyclops:
[Android Software/Hacking General [Developers Only]] putridfoetus:
By separated by spaces between the quotes do you mean like this:

"SystemUI.apk SystemUIGoogle.apk"

Or like this:

"SystemUI.apk" "SystemUIGoogle.apk"

Also, if an entry in apklist isn't found, will the tool simply ignore the missing apk, or achieve sentience and murder my first-born? :silly:
[Android Software/Hacking General [Developers Only]] osm0sis:
Good questions! Should be the first way, if I recall correctly, and the commands will fail if it's on a non-existent APK. That could be handled better, I'll throw it on my list for my debugging update.
[Android Software/Hacking General [Developers Only]] aj20010319:
Hey [MENTION=4544860]osm0sis[/MENTION], were you able to figure out that ImageIO class thing?
[Android Software/Hacking General [Developers Only]] djb77:
Looking forward to seeing an update on this so I can continue work on my project, and hopefully finish it :)
[Android Software/Hacking General [Developers Only]] MBariya:
[MENTION=4544860]osm0sis[/MENTION]

Is it possible to convert your script into module for magisk?

That way it selects apk file from system and keeps it back with systemless.
[Android Software/Hacking General [Developers Only]] osm0sis:
Sorry guys, still crazy short on time unfortunately.. this has always been a back-burner project for me - I wasn't really planning any updates after the original proof-of-concept - but I'll probably be able to spare an afternoon to pick away at it after New Year's. Thanks for your patience. :)

Cool idea! Yes you could pretty easily mod the part of the script that places the modded APK in system to instead put it in su.img or magisk.img and have it set up the bind mount for it accordingly. :good:
[Android Software/Hacking General [Developers Only]] djb77:
That's the project I'm working on, I've had it on hold for a while because I was having some issues with it.

[MENTION=4544860]osm0sis[/MENTION] has said he's going to do a debug update on this script soon, so when he does that I'll try and continue my work on the magisk integration.
[Android Software/Hacking General [Developers Only]] putridfoetus:
I haven't gotten this to work yet on my Nexus 6 running 7.0. It's been a while since I tried it, but when it tries to decode the apk, it fails every time. So I was looking through the code, and I believe I may have found something. Hopefully it's related to my problem:

baksmali() {
ANDROID_DATA=$ap ANDROID_ROOT=/system LD_LIBRARY_PATH=/system/lib dalvikvm -Xbootclasspath:/system/framework/core.jar:/system/framework/conscrypt.jar:/system/framework/apache-xml.jar -classpath $baksmali org.jf.baksmali.main -o classout $1;
test $? != 0 && abort "Decompiling APK classes failed. Aborting...";
}
smali() {
ANDROID_DATA=$ap ANDROID_ROOT=/system LD_LIBRARY_PATH=/system/lib dalvikvm -Xbootclasspath:/system/framework/core.jar:/system/framework/conscrypt.jar:/system/framework/apache-xml.jar -classpath $smali org.jf.smali.main -o classes.dex classout;
test $? != 0 && abort "Rebuilding APK classes failed. Aborting...";
}
apktool_d() {
ANDROID_DATA=$ap ANDROID_ROOT=/system LD_LIBRARY_PATH=/system/lib dalvikvm -Xbootclasspath:/system/framework/core.jar:/system/framework/conscrypt.jar:/system/framework/apache-xml.jar -classpath $apktool brut.apktool.Main d --frame-path $ap/framework --no-src -o resout $1;
test $? != 0 && abort "Decoding APK resources failed. Aborting...";
}
apktool_b() {
ANDROID_DATA=$ap ANDROID_ROOT=/system LD_LIBRARY_PATH=/system/lib dalvikvm -Xbootclasspath:/system/framework/core.jar:/system/framework/conscrypt.jar:/system/framework/apache-xml.jar -classpath $apktool brut.apktool.Main b --frame-path $ap/framework --aapt $bin/aapt --copy-original -o $1 resout;
test $? != 0 && abort "Rebuilding APK resources failed. Aborting...";
}

Specifically, Xbootclasspath:/system/framework/core.jar <-- this file does not exist in /system/framework (at least on the Nexus 6 build NBD91U).

There are core-junit.jar, core-libart.jar, and core-oj.jar, but no regular core.jar.

Could this be causing my issues? (I know, hard to say without logs) And could it be a broader issue with Nougat compatibility?

Sidenote: I got curious about the Magisk idea, so I built one too: https://github.com/putridfoetus/NavBarEnabler-Magisk

It uses apk-patcher to edit SystemUI.apk, then places the modded apk in a magisk module. (Unable to test properly, due to above issue)
[Android Software/Hacking General [Developers Only]] djb77:
This is a little off-topic, but I thought I'd share this with you anyway.

I managed to have success with integrating an upgraded version of the old-styled VRTheme into Magisk, and this seems to work perfectly.

https://github.com/djb77/magisk-module-template-eviltheme

Unfortuantely though this only replaces compiled xml files, classes.dex etc, meaning it doesn't decompile the files as such, only unzip and re-zip.
[Android Software/Hacking General [Developers Only]] osm0sis:
Sorry again about the wait everyone. I just finally got time to start looking into this again today and your post seems to line up with this, which looks like a major clue for why it stopped working and what I'll need to add to that classpath argument to hopefully get things working again universally in recovery:

https://github.com/JesusFreke/smali...ib2/analysis/ClassPathResolver.java#L341-L464

The latest commit for those (for Nougat support) is from after the most recent bak/smali 2.2b4 build though, so we may need to wait for 2.2 stable to really get things working currently again.

I'll have to retest with the latest apktool to see if the included aapt works yet. Would be nice to remove that external dependency. :)
[Android Software/Hacking General [Developers Only]] djb77:
Looking forward to an update so I can continue work on integrating it into Magisk :)
[Android Software/Hacking General [Developers Only]] djb77:
Thanks for looking into it a bit more. it's a shame that we've run into a brick wall with this. When I was testing I was using API 23 (Galaxy S7 MM), I can't remember if I told you this previously or not.

It seems like APKTool is having more success than baksmali / smali, which is one step forward at least.

I vaguely remember something in the recovery.log which was saying there was conflict between 32 and 64 bit. Since S7 is 64 bit I thought that might have been a possible issue too. For example, it would look in /system/lib and say it's not 64bit, but if I re-directed it to /system/lib64 it would complain there's no 32 bit. Arrrrghh! So I'm obviously not sure enough to say for certain, but maybe an issue may lie here too.

Just out of curiosity, what was the last API version it was tested with? I'm only assuming it's pre-API 23.

If you ever need any help I'm here and I'll do what I can to help. I'm on Telegram if you ever want to brainstorm / discuss things with me.
[Android Software/Hacking General [Developers Only]] osm0sis:
Sure, I've gone ahead and pushed a commit to restore the last jars that run so people at least have something to tinker with that works.

I know Chainfire has some lib64 stuff in the SuperSU zip script that might be of help with your other issue, and I've added it to the to-do above as well.

See how far you can get now that the jars actually work, that's the best I can do for now. I know depending on the APK to be decompiled it still seems to work fine - even the API 23 ContactsProvider.apk I was experimenting with - and though the output might not be as nice as it would with a newer bak/smali, it may still be possible to do certain things on newer files.

Pull request me if you manage to tick anything off of the list. :)
[Android Software/Hacking General [Developers Only]] djb77:
Just merged the latest commit into my repo :)

Now that I'm up-to-date with my Kernel / ROM I'll have some time over the weekend to look into this some more. Thanks for the tip with SuperSU too, I'll definitely look at what CF has done with it.

I'll see what I can achieve and keep logs along the way, just in case you see something that I don't :)
[Android Software/Hacking General [Developers Only]] ungeeked:
I guess the smali/baksmali isn't working on Android N. However I have a small mod which only requires modding a couple of xml files. I placed the new xml in patch folder. Script folder was empty. Getting the error "Updating APK classes failed. Aborting..."

I wonder if it is possible to delete a few lines to skip smali functionality and just use apktool for xml editing. Thank you