someone5678

Merge decompiled prebuilt apps with AOSP apps

Trial and error

Not all apps can directly includable on AOSP.
And some apps requires reverse engineering…
And some extreme cases, you might even want to merge prebuilt apps with AOSP apps…

For example, if you want to merge prebuilt Settings with AOSP one,

1. Decompile with apktool

apktool d -r SettingsOEM.apk

(Don’t decompile resources by adding -r option to prevent rebuild breakages)

2. Fix Resource linking

Find and replace ALL hex ref of resouces to actual vars
inside of resource classes or xml (e.g. R$string, public.xml).

For example, from this

const v0, 0x7f040ad0

to this

sget v0, Lcom/android/settings/R$string;->ok_button:I

If you’re using VS Code,
open all R$*.smali files in the new window
and do ctrl+shift+f for opened file wide search.
Or, create auto replace script.

3. Recompile with apktool

apktool b SettingsOEM.apk

3. Retrieving classes from dex files with d2j-dex2jar

d2j-dex2jar.sh SettingsOEM.apk

4. Clean-up and import

unzip SettingsOEM-dex2jar.jar
Clean-up and import required classes and resources that you want.
Zip classes to SettingsOEM-lib.jar
Place resources to (e.g.) res dir.
You should check whether classes names are not duplicated.
Target apps’ resources will be overrided by your imported ones.

Additional. Decompile with jadx to get decompiled java files for better code edit

jadx SettingsOEM-lib.jar

Remove class inside SettingsOEM-lib.jar and place java files
to corresponding dirs such as src/com/android/....

5. Define Android.bp

java_import  {
    name: "SettingsOEM-lib",
    sdk_version: "current",
    jars: ["libs/SettingsOEM-lib.jar"],
}

android_library {
    name: "SettingsOEM-core",
    platform_apis: true,

    resource_dirs: [
        "res",
    ],
    static_libs: [
        "SettingsOEM-lib",
    ],
    manifest: "AndroidManifest.xml",
}

android_app {
    name: "SettingsOEM",
    static_libs: [
        "SettingsOEM-core",
    ],
    srcs: [
        "src/**/*.java",
    ],
    defaults: ["platform_app_defaults"],
    platform_apis: true,
    certificate: "platform",
    system_ext_specific: true,
    privileged: true,

    overrides: [
        "Settings",
    ],
}

Include that app as module when you build AOSP.
Still it will requires bunch of fixes…
ADB logcat will be your friend for debugging.