load android framework as per apk's build version

This commit is contained in:
REAndroid 2023-03-13 06:46:10 -04:00
parent 4e204018fb
commit ec889cfd86
3 changed files with 87 additions and 11 deletions

View File

@ -27,6 +27,7 @@ import com.reandroid.arsc.container.SpecTypePair;
import com.reandroid.arsc.group.StringGroup; import com.reandroid.arsc.group.StringGroup;
import com.reandroid.arsc.item.TableString; import com.reandroid.arsc.item.TableString;
import com.reandroid.arsc.pool.TableStringPool; import com.reandroid.arsc.pool.TableStringPool;
import com.reandroid.arsc.util.FrameworkTable;
import com.reandroid.arsc.value.Entry; import com.reandroid.arsc.value.Entry;
import com.reandroid.arsc.value.ResConfig; import com.reandroid.arsc.value.ResConfig;
import com.reandroid.common.Frameworks; import com.reandroid.common.Frameworks;
@ -56,6 +57,57 @@ public class ApkModule implements ApkFile {
this.mUncompressedFiles=new UncompressedFiles(); this.mUncompressedFiles=new UncompressedFiles();
this.mUncompressedFiles.addPath(apkArchive); this.mUncompressedFiles.addPath(apkArchive);
} }
public void initializeAndroidFramework() throws IOException {
if(!hasTableBlock()){
return;
}
initializeAndroidFramework(getTableBlock());
}
private void initializeAndroidFramework(TableBlock tableBlock) throws IOException {
if(tableBlock == null || isAndroid(tableBlock)){
return;
}
List<TableBlock> frameWorkList = tableBlock.getFrameWorks();
for(TableBlock frameWork:frameWorkList){
if(isAndroid(frameWork)){
return;
}
}
logMessage("Initializing android framework ...");
Integer version = getAndroidFrameworkVersion();
FrameworkApk frameworkApk;
if(version==null){
logMessage("Can not read framework version from manifest, loading latest");
frameworkApk = AndroidFrameworks.getLatest();
}else {
logMessage("Loading android framework for version: " + version);
frameworkApk = AndroidFrameworks.getBestMatch(version);
}
FrameworkTable frameworkTable = frameworkApk.getTableBlock();
tableBlock.addFramework(frameworkTable);
logMessage("Initialized framework: "+frameworkApk.getName());
}
private boolean isAndroid(TableBlock tableBlock){
if(tableBlock instanceof FrameworkTable){
FrameworkTable frameworkTable = (FrameworkTable) tableBlock;
return frameworkTable.isAndroid();
}
return false;
}
public Integer getAndroidFrameworkVersion(){
if(!hasAndroidManifestBlock()){
return null;
}
AndroidManifestBlock manifestBlock = getAndroidManifestBlock();
Integer version = manifestBlock.getCompileSdkVersion();
if(version == null){
version = manifestBlock.getPlatformBuildVersionCode();
}
if(version == null){
version = manifestBlock.getTargetSdkVersion();
}
return version;
}
public void removeResFilesWithEntry(int resourceId) throws IOException { public void removeResFilesWithEntry(int resourceId) throws IOException {
removeResFilesWithEntry(resourceId, null, true); removeResFilesWithEntry(resourceId, null, true);
} }
@ -392,6 +444,9 @@ public class ApkModule implements ApkFile {
} }
try { try {
mTableBlock = loadTableBlock(); mTableBlock = loadTableBlock();
if(loadDefaultFramework){
initializeAndroidFramework(mTableBlock);
}
} catch (IOException exception) { } catch (IOException exception) {
throw new IllegalArgumentException(exception); throw new IllegalArgumentException(exception);
} }
@ -442,15 +497,11 @@ public class ApkModule implements ApkFile {
}else if(inputSource instanceof SingleJsonTableInputSource){ }else if(inputSource instanceof SingleJsonTableInputSource){
tableBlock=((SingleJsonTableInputSource)inputSource).getTableBlock(); tableBlock=((SingleJsonTableInputSource)inputSource).getTableBlock();
}else if(inputSource instanceof BlockInputSource){ }else if(inputSource instanceof BlockInputSource){
Chunk block = ((BlockInputSource<?>) inputSource).getBlock(); Chunk<?> block = ((BlockInputSource<?>) inputSource).getBlock();
tableBlock = (TableBlock) block; tableBlock = (TableBlock) block;
}else { }else {
InputStream inputStream = inputSource.openStream(); InputStream inputStream = inputSource.openStream();
if(loadDefaultFramework){
tableBlock=TableBlock.loadWithAndroidFramework(inputStream);
}else {
tableBlock = TableBlock.load(inputStream); tableBlock = TableBlock.load(inputStream);
}
inputStream.close(); inputStream.close();
} }
BlockInputSource<TableBlock> blockInputSource=new BlockInputSource<>(inputSource.getName(), tableBlock); BlockInputSource<TableBlock> blockInputSource=new BlockInputSource<>(inputSource.getName(), tableBlock);

View File

@ -279,6 +279,7 @@ import java.util.*;
return builder.toString(); return builder.toString();
} }
@Deprecated
public static TableBlock loadWithAndroidFramework(InputStream inputStream) throws IOException{ public static TableBlock loadWithAndroidFramework(InputStream inputStream) throws IOException{
TableBlock tableBlock=load(inputStream); TableBlock tableBlock=load(inputStream);
tableBlock.addFramework(Frameworks.getAndroid()); tableBlock.addFramework(Frameworks.getAndroid());

View File

@ -37,6 +37,7 @@ public class FrameworkTable extends TableBlock {
private String frameworkName; private String frameworkName;
private int versionCode; private int versionCode;
private int mainPackageId;
private ResNameMap<EntryGroup> mNameGroupMap; private ResNameMap<EntryGroup> mNameGroupMap;
private boolean mOptimized; private boolean mOptimized;
private boolean mOptimizeChecked; private boolean mOptimizeChecked;
@ -44,14 +45,28 @@ public class FrameworkTable extends TableBlock {
super(); super();
} }
public boolean isAndroid(){
return "android".equals(getFrameworkName())
&& getMainPackageId() == 0x01;
}
public int getMainPackageId() {
if(mainPackageId!=0){
return mainPackageId;
}
PackageBlock packageBlock = pickOne();
if(packageBlock!=null){
mainPackageId = packageBlock.getId();
}
return mainPackageId;
}
@Override @Override
public void destroy(){ public void destroy(){
ResNameMap<EntryGroup> nameGroupMap = this.mNameGroupMap; clearResourceNameMap();
if(nameGroupMap!=null){
nameGroupMap.clear();
}
this.frameworkName = null; this.frameworkName = null;
this.versionCode = 0; this.versionCode = 0;
this.mainPackageId = 0;
super.destroy(); super.destroy();
} }
public int resolveResourceId(String typeName, String entryName){ public int resolveResourceId(String typeName, String entryName){
@ -141,6 +156,15 @@ public class FrameworkTable extends TableBlock {
if(frameworkName == null){ if(frameworkName == null){
frameworkName = loadProperty(PROP_NAME); frameworkName = loadProperty(PROP_NAME);
} }
if(frameworkName == null){
PackageBlock packageBlock = pickOne();
if(packageBlock!=null){
String name = packageBlock.getName();
if(name!=null && !name.trim().isEmpty()){
frameworkName = name;
}
}
}
return frameworkName; return frameworkName;
} }
public void setFrameworkName(String value){ public void setFrameworkName(String value){