yashsingh1/xdaforums huggingface dataset:
[Android Software Development] MicroTransactionsMatter:
Assistance?

So, I'm kinda stuck. I've reconfigured it to pick up the APK I want to work on, but I can't get it to populate app/src/main with new .java or .class files. There's a lot of files in build. Am I missing something? I've matched the app/build.gradle with the source APK, and running decodeApk creates the relevant files in app/build, but nothing changes in app/src. Any assistance would be appreciated
[Android Software Development] denkazzzget:
I downloaded 'dexpatcher-gradle'. Where do I put my .apk/dex? There aren't any documentation on how to use.
[Android Software Development] Lanchon:
you don't need to download that. read the OP, there are samples.
[Android Software Development] kpeved:
Obfuscated imports

Hi,

Is it possible to overrite class which extend obfuscated class?

Let's say I have an app MyApp , which extends MultiDexApplication

In exact the same package I create a class MyApp

package com.example.app;
import android.support.multidex.MultiDexApplication;
import android.util.Log;
import lanchon.dexpatcher.annotation.DexAppend;
import lanchon.dexpatcher.annotation.DexEdit;

@DexEdit
public class MyApplication extends MultiDexApplication {
@DexAppend
@Override
public void onCreate() {
Log.e("ALALA", "onCreate from overriden app");
}
}

But got an error

I think this happens because MultiDexApplication was obfuscated into androidx.multidex.b

Is there a way to fix this?

Thank you.
[Android Software Development] Lanchon:
FIRST...

MultiDexApplication? this app seems to support android <5 (which requires mdex support lib).

DexPatcher (in general) does not support mdex on Android <5 (kitkat and earlier).

are you running the app on android 4.x ? -> then don't use dxp on mdex apps, sorry!

are you running the app on android 5 or later ? -> then continue...

NEXT...

the resulting patched mdex app will NOT support android <5.

so you SHOULD increase minSdkVersion to 21 so that the app will not install in android 4.

> Is it possible to overrite class which extend obfuscated class?

yes.

@DexEdit
public class MyApplication extends MultiDexApplication {

wrong! android.support.multidex.MultiDexApplication does not exist in your app, so the app will fail to verify.

DexEdit on a class will REPLACE the class definition (but not the members). so you are changing the class definition. why?

you can do this:

@DexEdit
public class MyApplication extends androidx.multidex.b {

assuming that this is the EXACT definition of MyApplication existing in the apk.

it will replace the class def with the exact same def; essentially doing a no-operation.

this makes the patch dependent on this particular run of the obfuscator, where androidx.multidex.MultiDexApplication -> androidx.multidex.b, and this is not nice.

you can do better:

@DexEdit(contentOnly = true)
public class MyApplication {

this makes the DxP only patch MEMBERS, but not the class def itself.

but if you need to access members of android.app.Application in your patch, you won't be able to.

(for the compiler of your patch, MyApplication extends Object.)

so you can try do this instead:

@DexEdit(contentOnly = true)
public class MyApplication extends android.app.Application {

now the patch compiler will see MyApplication extending Application, so it will allow access to Application members.

and android will see MyApplication extending androidx.multidex.b, as expected.

BUT...

since dxp does not support mdex on android <5, and on android >=5 mdex support is native (mdex support library is not needed), then there is no reason for the app to even invoke the mdex support library.

so you can even try and remove the invocation of the support lib altogether:

@DexEdit // Full DexEdit changes class def to inherit directly from Application; mdex support lib will not be invoked
public class MyApplication extends android.app.Application {

the reason to do this would be making the app smaller. this change works, then you can DexRemove package androidx.multidex (and its subpackages recursively). (see how to remove packages in the docs.)
[Android Software Development] kpeved:
Patching split-config apks

Hi [MENTION=4141005]Lanchon[/MENTION] ,

Thank you for the quick reply!

I have one more question.

Is it possible to patch split-config apks?

I have a set of APK's which were created with App Bundle

base.apk
split_config.arm64_v8a.apk
split_config.en.apk
split_config.xxhdpi.apk

Obiously installing only base.apk is not enough, because resources and translated files are located in other apk's - xxhdpi, en and others.

I can install them by using adb install-multiple command, which works perfectly with unpached files.

However, if I patch main APK and try to update the app ( leaving package name, version code/name the same) I'm getting an error -

Similar error I get when I try to install app through Android Studio

Which says that other apk's are signed with different certificate and I cannot update existing app, which makes sense, because I don't use original certificate.

The only solution about which I'm thinking right now could be updating all other apk's with DexPatcher in different projects and upload them through install-multiple to the device, but I didn't try it.

Are there any other easier options, which will patch all apk's in one go?

Thank you!

Updated

Managed to launch patched split-config apks.

What should be done is other apks should be signed with the same certificate, usually it will be debug.keystore . I used apksigner to do that.

apksigner sign --ks debug.keystore --ks-key-alias androiddebugkey --ks-pass pass:android split-config.xhdpi.apk
// androiddebugkey and android are default credentials for debug.keystore

after that I can install them using install-multiple command.

The problem right now is that it's not very convenient - I cannot use run command from Android studio because it deletes other apks during installation :/ .
[Android Software Development] Lanchon:
> other apks should be signed with the same certificate

yes, i was going to tell you that.

> I cannot use run command from Android studio because it deletes other apks during installation

do you debug or just run? i'm sure there is a way to include the other apks as they are in the build but i don't know how. never looked into split apps.

an alternative would be to define a post-build script that does the install-multiple. AFAICR, the run command just pushes the app to /tmp and installs it from there, as you can see in IDEA's log window.

yet another alternative: if you don't want to keep the "splitness" of the app, you can use apk tool to decode all parts, manually merge them, and rebuild a non-split apk. sign it, test it, then feed it to dexpatcher.
[Android Software Development] Lanchon:
UPDATE: DexPatcher-tool v1.7.0 and DexPatcher-gradle v2.0.0 are out!

IMPORTANT:

DexPatcher-gradle v2 no longer requires a local clone of the DexPatcher-gradle-tools repo.

More info here:

https://github.com/DexPatcher/dexpatcher-tool/releases

https://github.com/DexPatcher/dexpatcher-gradle/releases
[Android Software Development] ok888:
help

Can anyone please help me install this

I'm a super noob and can't figure out how to actually install and use this. Please help me!

I'm wiling to pay for your time if you want for helping me!
[Android Software Development] Lanchon:
you don't install anything (just prerequisites like Android Studio), you simply clone the samples. the OP (first post) of this thread has a section called "START HERE ! - With the new DexPatcher Gradle v2 plugins". this is really not a project for noobs. app development is "easy": you are doing what you are supposed to do. hacking apps is way harder than developing them, even with specialized tools. if you don't know stuff like how to clone with git or develop android apps, then i think this project is not for you. some experience with apktool comes handy, as does knowing details of the JVM and dalvik/ART virtual machines.
[Android Software Development] ok888:
Thanks for your reply.

Please if you could write a newbie friendly instruction that'd be great!.

I really need DexPatcher this is the only tool I can find that do exactly what I intended to do.

I cant get it to work in Andoird Studio or I dont know how to.

I've read the "START HERE" like 50 times but cant understand it. I know I'm newbie but I got to start somewhere right?
[Android Software Development] Lanchon:
the step by step instructions are there, they are 2 steps. i'm afraid this tool is very much not for newbies. setting up the samples is easy. i suggest you read the section called "PATCHING JAVA CODE IN JAVA" and read the linked code, especially the patch. unless or until you understand 90% of what is going on there, there is really no reason for you to make the samples work. reverse engineering machine code is HARD; orders of magnitude harder than writing it.
[Android Software Development] ok888:
I have read "PATCHING JAVA CODE IN JAVA" as well. It is much simpler to understand but the problem is I'm on window. I guess it is hard for a coder to explain to a newbie in a dumb down way. I'm so determined to get this to work if anyone is willing to help a noob I'm willing to pay.

Please contact me directly if interested.
[Android Software Development] Lanchon:
ok you win. the instructions are posted as you know. now you post step by step what you are doing and the result of it. i'll get you past where you are stuck. don't leave out any details. start from the beginning.

this *should* work on windows. and if there is a problem, it can be fixed.
[Android Software Development] leftytechie:
Okay, so I replaced the jars in the old workflow with version 1.6.2 (this is the most updated version of dexpatcher-annotation that I could find, and I just assumed they should match), and it seems like it's working, but unfortunately it doesn't solve my problem :rolleyes:

My problem is that the APKs I'm trying to modify are very big, and as a result when they get decompiled, there are a lot of classes and packages getting assigned names like "a", "b", "c", etc. which causes name conflicts when we're trying to import them (because there is both a class called "a" and a package "a" in the same directory and it confuses the compiler). Unfortunately, the class I'm trying to modify extends an abstract class that has methods with return types that are masked in this way, and as a result I can't modify it without implementing these methods, which means importing these classes, which then causes compilation to fail since they can't be found. Is there a way for me to avoid having to implement these classes? I tried using "contentOnly=true" and declaring the class abstract, like for the class Concrete2 at DexPatcher/dexpatcher-tool/blob/master/test/source/src/main/java/test/Main.java (I'm a new user so I can't post the URL directly) but that doesn't seem to work either...

The problem almost seems like it is with dex2jar (I think that's the part of the workflow that does the renaming at least). I don't understand why it doesn't just assign unique names to each package and class that it gives a name to, it seems like it would be a simple enough thing to do and would really extend what would be possible in this respect.

But, maybe this is just because I don't understand enough about what's happening under the surface, and there is already a way to solve this problem?

Thanks again.

---------- Post added at 08:39 PM ---------- Previous post was at 08:23 PM ----------

Sorry for posting so much, but now I understand that the issue is not dex2jar, it's that the APK has been obfuscated. I will look into a solution regarding de-obfuscation. Please let me know if you have any tips though, for example, how to insert deobfuscation into the workflow :)

Thanks again for this awesome project!
[Android Software Development] Lanchon:
[MENTION=10289707]leftytechie[/MENTION],

so sorry. it seems i forgot to update parts of the docs from dxp-gradle v1 to v2.

i'll take a look at this really ASAP, and thanks so much for alerting me!
[Android Software Development] leftytechie:
No problem :) and in terms of my situation, I must have tried 6 different deobfuscators, but nothing worked. For about 10 minutes APK-Deguard looked like it had worked really well, it even restored the original names of almost every package and class, but it has bugs and the APK it returned was corrupt and missing files, so it wouldn't run. Since I can't deobfuscate the files properly I can't import the required classes, and therefore I can't modify the APK I wanted to.

The only option left for me seems to be the Xposed framework, if I wanted to keep working on this. But honestly, as much as I dislike it, I think I might throw in the towel and just buy a new camera :(

Maybe I'll come back around in the future just to check in on your work though. Again, this is very cool.
[Android Software Development] DualJoe:
[MENTION=4141005]Lanchon[/MENTION]:

How can i apply the (haystack) dexpatch to a LOS16 source tree? Or is dexpatcher not able to do that? I mean, the java diff code is there but i'm not sure if dexpatcher can handle the file/folder structure of frameworks/base/services.
[Android Software Development] Lanchon:
[MENTION=10289707]leftytechie[/MENTION],

> Not only is there no 'patched' subproject...

thanks. i updated the START HERE section now, sorry about that.

> what I'm resigned to doing now is trying to use the old workflow with updated jar files

i suppose by 'old workflow' you mean the scripts. man those are old! well i guess it should work, i tend to not break backwards compatibility without good reason. note that the gradle plugin is more complex, because it allows patching manifest resources and code.

> there are a lot of classes and packages getting assigned names like "a", "b", "c", etc

yep, obfuscation. it's a *****. some obfuscators encrypt strings, rearrange instrauctions, add instructions, etc.

DXP is perfectly capable of patching obfuscated dex. the problem is that the java compiler has more restrictive naming of stuff than the JVM. and it also clashes package names with class names, as you saw.

proper obfuscation support in DXP is lacking, but i'll try adding stuff. ive got plans. in the meantime, one workaround (i never tried it myself) is using a JVM language that allows quoting of identifiers, thus letting you define the names you want in the patch. i believe one such language is Kotlin, which happens to have first tier support in android and gradle.

> APK-Deguard looked like it had worked really well

good to know. what other deobfuscator did you try?

> but it has bugs and the APK it returned was corrupt

the point of the deobfuscator is to facilitate analysis. typically deobfuscated code will NOT run, but it can be decompiled.

some obfuscators also add self-checking logic, so that if the apk has been tampered with, it will not run. a round trip through apktool will typically trigger the checks, and that is the first test you should do before starting to patch an APK. of course you can disable the checks in the patch, one would suppose these are designed to be hard to find.

> Is there a way for me to avoid having to implement these classes? I tried using "contentOnly=true" and declaring the class abstract [...] but that doesn't seem to work either...

yes it works! as long as you get the class to compile. you can also use cross-class edits (see the dxp-tool sample) to deal with some naming issues. but the general solution requires a language with name quoting.,, for now.
[Android Software Development] Lanchon:
[MENTION=10279273]ok888[/MENTION],

i was alerted to the fact that the docs section you were tying to understand was incorrect. i hadn't updated it to the v2 gradle plugin style, and all the links were broken. it should be fine now if you care to read it again.
[Android Software Development] leftytechie:
[MENTION=4141005]Lanchon[/MENTION]

Thank you for your very detailed response! I've been dragged away from this project for now, but there's a chance I'll come back to it in a couple of weeks.

> one workaround (i never tried it myself) is using a JVM language that allows quoting of identifiers, thus letting you define the names you want in the patch. i believe one such language is Kotlin, which happens to have first tier support in android and gradle.

My understanding of bytecode and JVM's is pretty limited, but this certainly sounds really interesting and it would be exciting if it provided more power to work around obfuscation. Would this be something I would do or would this part of the DXP project?

> good to know. what other deobfuscator did you try?

I tried APK-deguard, procyon and some others that are built from it, dex-oracle, jadx, Deobfuscator (although I didn't spend much time researching the different possible transforms), and one or two more that I can't remember off the top of my head. Basically everything either crashed or returned the same classes and packages that were already there, except APK-deguard which as I mentioned did a great job but lost a bunch of stuff.

> the point of the deobfuscator is to facilitate analysis. typically deobfuscated code will NOT run, but it can be decompiled.

I didn't realize that deobfuscated code isn't intended to be runnable, that's a bit puzzling to me. If it isn't intended to run I don't think I need it that badly, I wasn't having much trouble reading the decompiled code, just need to deal with the import masking.

> a round trip through apktool will typically trigger the checks, and that is the first test you should do before starting to patch an APK.

This APK does seem to run fine after going through apktool at least.

> yes it works! as long as you get the class to compile. you can also use cross-class edits (see the dxp-tool sample) to deal with some naming issues. but the general solution requires a language with name quoting.,, for now.

If I come back to this, I would really like to try the "contentOnly=true" method again. If I remember correctly the compiler complained about the class being declared abstract.
[Android Software Development] Lanchon:
you'd try it yourself. instead of coding the patch in Java you'd do it in Kotlin. the advantage would be that Kotlin can manage and create JVM items (packages, classes, fields, methods) with names that are invalid in Java but valid in the JVM (which is what obfuscators target). you'd need to experiment yourself.
[Android Software Development] Lanchon:
UPDATE: DexPatcher-tool v1.8.0-alpha1 is out!

Now supports patching OBFUSCATED CODE, YAY !!!

More info here:

https://github.com/DexPatcher/dexpatcher-tool/releases
[Android Software Development] mybeshoo:
Thank you for this project.

Well i note that you are using apktool:2.4.0 , whihc is out dated , now there is apktool 2.4.1 which support "--only-main-classes"

see this issue

url : github.com/iBotPeaches/Apktool/issues/2247

How can we use "--only-main-classes" and " apktool 2.4.1 " ?
[Android Software Development] Lanchon:
> i note that you are using apktool:2.4.0 , whihc is out dated

no, the user chooses what version of apktool to use. 2.4.1 has been in the dexpatcher repo since the day it was published.

> How can we use "--only-main-classes"

dexpatcher-gradle only uses apktool to process resources. "--only-main-classes" is not applicable because smali is not used at all. (that said, dexpatcher-tool has since day one only processed what apktool calls "main classes".)

we don't know what you are trying to achieve. but if you want to use smali and apktool, you should use those tools and not dexptacher.

(well in reality, if you are looking to automate your workflow using Gradle, you can use the dexpatcher-gradle base plugin. it defines gradle tasks for apktool and dex2jar. your can define custom builds using those tasks.)
[Android Software Development] mybeshoo:
Thanks for the reply

Lets say i have aar library, and i want to inject it in

An app which i have the apk of the app, as example, Instagram apk.... From top of my head ?

Can we patch any apk? Edit the java code? Add a lib and run it as a service.....
[Android Software Development] Lanchon:
> Lets say i have aar library, and i want to inject it in

no problem. look a the patched-app sample, it adds and uses an AAR file.

> Can we patch any apk? Edit the java code?

only instagram have their java code. in general, there is no way to fully decompile java bytecode or dalvik bytecode. dexpatcher des not try to do the impossible. instead you write patches in java and dexpatcher applies them.

see the doc section PATCHING JAVA CODE IN JAVA to see how patches are written. see the patched-app sample to see how patches are applied in in a real-world setting and how to also modify the app's manifest and resources. there is no other documentation besides those two. you have to read those.
[Android Software Development] mybeshoo:
O.K. Thank you for your reply. and since i am very new to the android studio, i use to use APKTOOL with notpad++.

How to install "dexpatcher-gradle" to the Android Studio?

please kindly provide steps.

I cloned the : https://github.com/DexPatcher/dexpatcher-gradle

O.K. now what ?

I have file.apk and i want to decompile it and then inject aar lib in it!

How to do that via android studio
[Android Software Development] mybeshoo:
[IMG="I am cloning the samples but I have this error please advise"]https://i.vgy.me/O8nbEg.png[/IMG]

12/15/2019

4:03 PM	Gradle sync started with single-variant sync

4:03 PM	Gradle sync failed: Cannot cast object 'map(null)' with class 'org.gradle.api.internal.file.DefaultFilePropertyFactory$ResolvingDirectory' to class 'org.gradle.api.file.Directory' (4 s 23 ms)

4:03 PM	NDK Resolution Outcome: Project settings: Gradle model version=5.1.1, NDK version is UNKNOWN

5:07 PM	Can't bind to local 8600 for debugger

---------------------------------------------

ERROR: Cannot cast object 'map(null)' with class 'org.gradle.api.internal.file.DefaultFilePropertyFactory$ResolvingDirectory' to class 'org.gradle.api.file.Directory'

Open File
[Android Software Development] Lanchon:
- you are not supposed to open the main directory as a project. instructions call for you to open the 'patched-app' project.

- you are not supposed to upgrade any sample component just because Studio offers to do it for you.

follow instructions!