feat: set minimum sdk to Android 9 if original value is lower (#167)

This commit is contained in:
Js0n 2023-01-28 11:53:55 +08:00 committed by GitHub
parent 03487577d8
commit e62da55614
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 7 deletions

@ -1 +1 @@
Subproject commit aff51233504a52dcb09b5d7e7119bc8b428585f9 Subproject commit 8133add330141096033cc3e6bad48100c1148c11

View File

@ -218,12 +218,15 @@ public class LSPatch {
// parse the app appComponentFactory full name from the manifest file // parse the app appComponentFactory full name from the manifest file
final String appComponentFactory; final String appComponentFactory;
int minSdkVersion;
try (var is = manifestEntry.open()) { try (var is = manifestEntry.open()) {
var pair = ManifestParser.parseManifestFile(is); var pair = ManifestParser.parseManifestFile(is);
if (pair == null) if (pair == null)
throw new PatchError("Failed to parse AndroidManifest.xml"); throw new PatchError("Failed to parse AndroidManifest.xml");
appComponentFactory = pair.appComponentFactory; appComponentFactory = pair.appComponentFactory;
minSdkVersion = pair.minSdkVersion;
logger.d("original appComponentFactory class: " + appComponentFactory); logger.d("original appComponentFactory class: " + appComponentFactory);
logger.d("original minSdkVersion: " + minSdkVersion);
} }
logger.i("Patching apk..."); logger.i("Patching apk...");
@ -231,7 +234,7 @@ public class LSPatch {
final var config = new PatchConfig(useManager, debuggableFlag, overrideVersionCode, sigbypassLevel, originalSignature, appComponentFactory); final var config = new PatchConfig(useManager, debuggableFlag, overrideVersionCode, sigbypassLevel, originalSignature, appComponentFactory);
final var configBytes = new Gson().toJson(config).getBytes(StandardCharsets.UTF_8); final var configBytes = new Gson().toJson(config).getBytes(StandardCharsets.UTF_8);
final var metadata = Base64.getEncoder().encodeToString(configBytes); final var metadata = Base64.getEncoder().encodeToString(configBytes);
try (var is = new ByteArrayInputStream(modifyManifestFile(manifestEntry.open(), metadata))) { try (var is = new ByteArrayInputStream(modifyManifestFile(manifestEntry.open(), metadata, minSdkVersion))) {
dstZFile.add(ANDROID_MANIFEST_XML, is); dstZFile.add(ANDROID_MANIFEST_XML, is);
} catch (Throwable e) { } catch (Throwable e) {
throw new PatchError("Error when modifying manifest", e); throw new PatchError("Error when modifying manifest", e);
@ -314,11 +317,13 @@ public class LSPatch {
} }
} }
private byte[] modifyManifestFile(InputStream is, String metadata) throws IOException { private byte[] modifyManifestFile(InputStream is, String metadata, int minSdkVersion) throws IOException {
ModificationProperty property = new ModificationProperty(); ModificationProperty property = new ModificationProperty();
if (overrideVersionCode) if (overrideVersionCode)
property.addManifestAttribute(new AttributeItem(NodeValue.Manifest.VERSION_CODE, 1)); property.addManifestAttribute(new AttributeItem(NodeValue.Manifest.VERSION_CODE, 1));
if (minSdkVersion < 28)
property.addUsesSdkAttribute(new AttributeItem(NodeValue.UsesSDK.MIN_SDK_VERSION, "28"));
property.addApplicationAttribute(new AttributeItem(NodeValue.Application.DEBUGGABLE, debuggableFlag)); property.addApplicationAttribute(new AttributeItem(NodeValue.Application.DEBUGGABLE, debuggableFlag));
property.addApplicationAttribute(new AttributeItem("appComponentFactory", PROXY_APP_COMPONENT_FACTORY)); property.addApplicationAttribute(new AttributeItem("appComponentFactory", PROXY_APP_COMPONENT_FACTORY));
property.addMetaData(new ModificationProperty.MetaData("lspatch", metadata)); property.addMetaData(new ModificationProperty.MetaData("lspatch", metadata));

View File

@ -18,6 +18,7 @@ public class ManifestParser {
AXmlResourceParser parser = new AXmlResourceParser(); AXmlResourceParser parser = new AXmlResourceParser();
String packageName = null; String packageName = null;
String appComponentFactory = null; String appComponentFactory = null;
int minSdkVersion = 0;
try { try {
parser.open(is); parser.open(is);
@ -40,13 +41,21 @@ public class ManifestParser {
} }
} }
if ("uses-sdk".equals(name)) {
if ("android:minSdkVersion".equals(attrName)) {
minSdkVersion = Integer.parseInt(parser.getAttributeValue(i));
}
}
if ("appComponentFactory".equals(attrName) || attrNameRes == 0x0101057a) { if ("appComponentFactory".equals(attrName) || attrNameRes == 0x0101057a) {
appComponentFactory = parser.getAttributeValue(i); appComponentFactory = parser.getAttributeValue(i);
} }
if (packageName != null && packageName.length() > 0 && if (packageName != null && packageName.length() > 0 &&
appComponentFactory != null && appComponentFactory.length() > 0) { appComponentFactory != null && appComponentFactory.length() > 0 &&
return new Pair(packageName, appComponentFactory); minSdkVersion > 0
) {
return new Pair(packageName, appComponentFactory, minSdkVersion);
} }
} }
} else if (type == XmlPullParser.END_TAG) { } else if (type == XmlPullParser.END_TAG) {
@ -56,7 +65,7 @@ public class ManifestParser {
} catch (XmlPullParserException | IOException e) { } catch (XmlPullParserException | IOException e) {
return null; return null;
} }
return new Pair(packageName, appComponentFactory); return new Pair(packageName, appComponentFactory, minSdkVersion);
} }
/** /**
@ -73,9 +82,12 @@ public class ManifestParser {
public String packageName; public String packageName;
public String appComponentFactory; public String appComponentFactory;
public Pair(String packageName, String appComponentFactory) { public int minSdkVersion;
public Pair(String packageName, String appComponentFactory, int minSdkVersion) {
this.packageName = packageName; this.packageName = packageName;
this.appComponentFactory = appComponentFactory; this.appComponentFactory = appComponentFactory;
this.minSdkVersion = minSdkVersion;
} }
} }