[Sprint LG Optimus G] AdhvanIt:
Porting guide

Porting an AOSP based ROM (MIUI, COS, CM, etc):

For this project, you'll need to get familiar with adb, apktool, and meld. Since these already have tutorials, I'll link those instead of writing my own for each.

COMPREHENSIVE GUIDE TO ADB | LEARN TO LOGCAT LIKE A PRO | APKTOOL | MELD

The most successful ports to the LGOG will come from Nexus 4 based ROMs, as they are the most similar in chipset to our device. For this guide, we will reference base ROM as the build from our device, and reference port as the ROM you'll be bringing to our device.

Getting started:

Extract your base ROM into a working folder of your choice. Inside, you'll find:

System - The majority of the work we'll do will be in here.
Data - May contain applications, boot scripts, config files etc.
META-INF - Contains certificates and signature for signcheck and updater-script, update-binary (updater-script command processor)
boot.img - Kernel/ramdisk packed together. This should only be touched in the event you wish to use a kernel that is separate from the one in your base ROM, or to edit the ramdisk.

From inside your working folder, you'll want to delete the following directories and all files contained within them:

/system/app
/system/fonts
/system/framework
/system/media

Open your port archive and extract those same folders into /system directory of your working folder.

Some ports such as MIUI, cos, etc also require that certain permissions xml's (inside /system/etc/permissions) be copied over so that the theme engines will work properly.

Modifying build.prop:

In your additional build properties section, you'll find lines similar to this.
ro.config.ringtone=
ro.config.notification_sound=
ro.config.alarm_alert=
ro.config.sms_received_sound=
ro.config.sms_delivered_sound=
They contain ringtones that are in the base ROM, and will need to be changed to those in the port.

This is the base work for porting a ROM. From here, you'll want to zip and sign the ROM, then attempt to boot (Note: it will not boot yet).

Framework mods required for the device to boot:

Navigate to /system/framework and decompile the following:
framework-res.apk
framework.jar
telephony-common.jar

Note: some ports may require services.jar and android.policy.jar decompiled as well, but no other framework will change in a port as they are common among all AOSP ROMs.

Extract and decompile the same frameworks from your base ROM zip.

The following is a list of the exact methods/classes and framework xml's that need replaced or modified to port COS from the N4 to the LGOG. Some of these will be universal between different ports, and some will be specific to COS. When you run into issues, you'll want to take a look at the steps for debugging below.

framework-res.apk:

/res/values/arrays.xml:

<array name="config_cdma_dun_supported_types" />
Change this line to:
<string-array name="config_cdma_dun_supported_types">
<item>default</item>
<item>dun</item>
<item>mms</item>
<item>supl</item>
</string-array>

res/values/bools.xml:

<bool name="config_telephony_use_own_number_for_voicemail">false</bool>
<bool name="config_mms_content_disposition_support">true</bool>
<bool name="config_showNavigationBar">true</bool>

Change to:

<bool name="config_telephony_use_own_number_for_voicemail">true</bool>
<bool name="config_mms_content_disposition_support">false</bool>
<bool name="config_showNavigationBar">false</bool>

res/xml/eri.xml:

Replace this xml with the one from your base ROM.

telephony-common.jar:

com/android/internal/telephony:

For this, you'll want to replace entire smali trees with those from your base ROM. First, the RIL smali tree, or RIL*.smali:

RIL.smali
RIL$1.smali
RIL$RILReceiver.smali
RIL$RILSender.smali
RILRequest.smali

Also:

LGEQualcommCDMARIL*.smali
PhoneFactory.smali

framework.jar

android/content/res/AssetManager.smali:

Change:

.field private static final sSync:Ljava/lang/Object;

.field static sSystem:Landroid/content/res/AssetManager;

# instance fields
.field private mNObject:I

.field private mNumRefs:I

To:

.field private static final sSync:Ljava/lang/Object;

.field static sSystem:Landroid/content/res/AssetManager;

# instance fields
.field private mAppName:Ljava/lang/String;

.field private mAssetDir:Ljava/lang/String;

.field private mNObject:I

.field private mNumRefs:I

Change:

.field private mOpen:Z

.field private mRefStacks:Ljava/util/HashMap;
.annotation system Ldalvik/annotation/Signature;

To:

.field private mOpen:Z

.field private mRedirections:Landroid/util/SparseArray;
.annotation system Ldalvik/annotation/Signature;
value = {
"Landroid/util/SparseArray",
"<",
"Landroid/content/res/PackageRedirectionMap;",
">;"
}
.end annotation
.end field

.field private mRefStacks:Ljava/util/HashMap;
.annotation system Ldalvik/annotation/Signature;

Change:

.field private mStringBlocks:[Landroid/content/res/StringBlock;

.field private final mValue:Landroid/util/TypedValue;

# direct methods

To:

.field private mStringBlocks:[Landroid/content/res/StringBlock;

.field private mThemeCookie:I

.field private mThemePackageName:Ljava/lang/String;

.field private mThemeSupport:Z

.field private final mValue:Landroid/util/TypedValue;

# direct methods

Change:

.end method

.method static final native applyStyle(IIII[I[I[I)Z
.end method

.method static final native applyThemeStyle(IIZ)V
.end method

.method static final native copyTheme(II)V
.end method

To:

.end method

.method private final native addRedirectionsNative(I)V
.end method

.method static final native applyStyle(IIII[I[I[I)Z
.end method

.method static final native applyThemeStyle(IIZ)V
.end method

.method private final native clearRedirectionsNative()V
.end method

.method static final native copyTheme(II)V
.end method

Change:

.method private final native seekAsset(IJI)J
.end method

# virtual methods
.method public final native addAssetPath(Ljava/lang/String;)I

To:

.end method

.method private final native splitThemePackage(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)I
.end method

# virtual methods
.method public final native addAssetPath(Ljava/lang/String;)I

Change:

invoke-virtual {p0, v2}, Landroid/content/res/AssetManager;->addAssetPath(Ljava/lang/String;)I

move-result v2

aput v2, v0, v1

.line 620
add-int/lit8 v1, v1, 0x1

goto :goto_0
.end method

.method public close()V
.locals 1

To:

invoke-virtual {p0, v2}, Landroid/content/res/AssetManager;->addAssetPath(Ljava/lang/String;)I

move-result v2

aput v2, v0, v1

.line 620
add-int/lit8 v1, v1, 0x1

goto :goto_0
.end method

.method public addRedirections(Landroid/content/res/PackageRedirectionMap;)V
.locals 2
.parameter "map"

.prologue
.line 747
iget-object v0, p0, Landroid/content/res/AssetManager;->mRedirections:Landroid/util/SparseArray;

if-nez v0, :cond_0

.line 748
new-instance v0, Landroid/util/SparseArray;

const/4 v1, 0x2

invoke-direct {v0, v1}, Landroid/util/SparseArray;-><init>(I)V

iput-object v0, p0, Landroid/content/res/AssetManager;->mRedirections:Landroid/util/SparseArray;

.line 750
:cond_0
iget-object v0, p0, Landroid/content/res/AssetManager;->mRedirections:Landroid/util/SparseArray;

invoke-virtual {p1}, Landroid/content/res/PackageRedirectionMap;->getPackageId()I

move-result v1

invoke-virtual {v0, v1, p1}, Landroid/util/SparseArray;->append(ILjava/lang/Object;)V

.line 751
invoke-virtual {p1}, Landroid/content/res/PackageRedirectionMap;->getNativePointer()I

move-result v0

invoke-direct {p0, v0}, Landroid/content/res/AssetManager;->addRedirectionsNative(I)V

.line 752
return-void
.end method

.method public final native attachThemePath(Ljava/lang/String;)I
.end method

.method public clearRedirections()V
.locals 1

.prologue
.line 759
iget-object v0, p0, Landroid/content/res/AssetManager;->mRedirections:Landroid/util/SparseArray;

if-eqz v0, :cond_0

.line 760
iget-object v0, p0, Landroid/content/res/AssetManager;->mRedirections:Landroid/util/SparseArray;

invoke-virtual {v0}, Landroid/util/SparseArray;->clear()V

.line 762
:cond_0
invoke-direct {p0}, Landroid/content/res/AssetManager;->clearRedirectionsNative()V

.line 763
return-void
.end method

.method public close()V
.locals 1

Change:

.end method

.method final ensureStringBlocks()V

To:

.end method

.method public final native detachThemePath(Ljava/lang/String;I)Z
.end method

.method final ensureStringBlocks()V

Change:

.end method

.method final native getArrayIntResource(I)[I
.end method

.method final native getArraySize(I)I
.end method

.method public final native getCookieName(I)Ljava/lang/String;

To:

.end method

.method public final native generateStyleRedirections(III)Z
.end method

.method final native getArrayIntResource(I)[I
.end method

.method final native getArraySize(I)I
.end method

.method public final native getBasePackageCount()I
.end method

.method public final native getBasePackageId(I)I
.end method

.method public final native getBasePackageName(I)Ljava/lang/String;
.end method

.method public final native getCookieName(I)Ljava/lang/String;

Change:

.end method

.method final getThemeValue(IILandroid/util/TypedValue;Z)Z

To:

.end method

.method public getThemeCookie()I
.locals 1

.prologue
.line 730
iget v0, p0, Landroid/content/res/AssetManager;->mThemeCookie:I

return v0
.end method

.method public getThemePackageName()Ljava/lang/String;
.locals 1

.prologue
.line 714
iget-object v0, p0, Landroid/content/res/AssetManager;->mThemePackageName:Ljava/lang/String;

return-object v0
.end method

.method final getThemeValue(IILandroid/util/TypedValue;Z)Z

Change:

.end method

.method public final native isUpToDate()Z

To:

.end method

.method public final hasThemeSupport()Z
.locals 1

.prologue
.line 696
iget-boolean v0, p0, Landroid/content/res/AssetManager;->mThemeSupport:Z

return v0
.end method

.method public final native isUpToDate()Z

Change:

.end method

.method final releaseTheme(I)V

To:

.end method

.method final recreateStringBlocks()V
.locals 1

.prologue
.line 272
monitor-enter p0

.line 273
const/4 v0, 0x1

:try_start_0
invoke-virtual {p0, v0}, Landroid/content/res/AssetManager;->makeStringBlocks(Z)V

.line 274
monitor-exit p0

.line 275
return-void

.line 274
:catchall_0
move-exception v0

monitor-exit p0
:try_end_0
.catchall {:try_start_0 .. :try_end_0} :catchall_0

throw v0
.end method

.method final releaseTheme(I)V

Change:

.method public final native setLocale(Ljava/lang/String;)V
.end method

.method xmlBlockGone(I)V

To:

.method public final native setLocale(Ljava/lang/String;)V
.end method

.method public setThemeCookie(I)V
.locals 0
.parameter "cookie"

.prologue
.line 738
iput p1, p0, Landroid/content/res/AssetManager;->mThemeCookie:I

.line 739
return-void
.end method

.method public setThemePackageName(Ljava/lang/String;)V
.locals 0
.parameter "packageName"

.prologue
.line 722
iput-object p1, p0, Landroid/content/res/AssetManager;->mThemePackageName:Ljava/lang/String;

.line 723
return-void
.end method

.method public final setThemeSupport(Z)V
.locals 0
.parameter "themeSupport"

.prologue
.line 686
iput-boolean p1, p0, Landroid/content/res/AssetManager;->mThemeSupport:Z

.line 687
return-void
.end method

.method public final splitDrmProtectedThemePackage(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)I
.locals 1
.parameter "packageFileName"
.parameter "lockedFileName"
.parameter "drmProtectedresources"

.prologue
.line 492
invoke-direct {p0, p1, p2, p3}, Landroid/content/res/AssetManager;->splitThemePackage(Ljava/lang/String;Ljava/lang/String;[Ljava/lang/String;)I

move-result v0

return v0
.end method

.method xmlBlockGone(I)V

Replace the following smali files with the ones from your base:

android/content/res/PackageRedirectionMap*.smali
android/hardware/Camera*.smali
android/telephony/TelephonyManager.smali
android/webkit/HTML5*.smali

android/webkit/WebSettingsClassic.smali

Change:

.field private mWebGLEnabled:Z

.field private mWebView:Landroid/webkit/WebViewClassic;

To:

.field private mWebGLEnabled:Z

.field private mWebSocketsEnabled:Z

.field private mWebView:Landroid/webkit/WebViewClassic;

Change:

.line 93
iput-boolean v3, p0, Landroid/webkit/WebSettingsClassic;->mDomStorageEnabled:Z

.line 94

To:

.line 93
iput-boolean v3, p0, Landroid/webkit/WebSettingsClassic;->mWebSocketsEnabled:Z

iput-boolean v3, p0, Landroid/webkit/WebSettingsClassic;->mDomStorageEnabled:Z

.line 94

Change:

monitor-exit p0

throw v2
.end method

.method isNarrowColumnLayout()Z

To:

monitor-exit p0

throw v2
.end method

.method public declared-synchronized getWebSocketsEnabled()Z
.locals 1

.prologue
.line 1342
monitor-enter p0

:try_start_0
iget-boolean v0, p0, Landroid/webkit/WebSettingsClassic;->mWebSocketsEnabled:Z
:try_end_0
.catchall {:try_start_0 .. :try_end_0} :catchall_0

monitor-exit p0

return v0

:catchall_0
move-exception v0

monitor-exit p0

throw v0
.end method

.method isNarrowColumnLayout()Z

The methods above can be found in your base AssetManager.smali. I've provided exactly what you need to replace so that you can do it with as little trial and error as necessary. If you make a mistake in AssetManager, even your logcat won't give you any usable information...