Skip to content

new: featureFlags support for SDK 35 apps #3706

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,15 @@ private void invokeAapt2(File apkFile, File manifest, File resDir, File rawDir,
cmd.add("-x");
}

if (!mApkInfo.featureFlags.isEmpty()) {
List<String> featureFlags = new ArrayList<>();
for (Map.Entry<String, Boolean> entry : mApkInfo.featureFlags.entrySet()) {
featureFlags.add(entry.getKey() + "=" + entry.getValue());
}
cmd.add("--feature-flags");
cmd.add(String.join(",", featureFlags));
}

if (include != null) {
for (File file : include) {
cmd.add("-I");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import brut.androlib.exceptions.OutDirExistsException;
import brut.androlib.apk.ApkInfo;
import brut.androlib.res.ResourcesDecoder;
import brut.androlib.res.xml.ResXmlPatcher;
import brut.androlib.src.SmaliDecoder;
import brut.directory.Directory;
import brut.directory.ExtFile;
Expand Down Expand Up @@ -321,6 +322,15 @@ private void writeApkInfo(File outDir) throws AndrolibException {
mApkInfo.setMinSdkVersion(Integer.toString(mMinSdkVersion));
}

// record feature flags
File manifest = new File(outDir, "AndroidManifest.xml");
List<String> featureFlags = ResXmlPatcher.pullManifestFeatureFlags(manifest);
if (featureFlags != null) {
for (String flag : featureFlags) {
mApkInfo.addFeatureFlag(flag, true);
}
}

// record uncompressed files
try {
Map<String, String> resFileMapping = mResDecoder.getResFileMapping();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class ApkInfo implements YamlSerializable {
public Map<String, String> sdkInfo = new LinkedHashMap<>();
public PackageInfo packageInfo = new PackageInfo();
public VersionInfo versionInfo = new VersionInfo();
public Map<String, Boolean> featureFlags = new LinkedHashMap<>();
public boolean sharedLibrary;
public boolean sparseResources;
public List<String> doNotCompress = new ArrayList<>();
Expand Down Expand Up @@ -185,6 +186,10 @@ private int mapSdkShorthandToVersion(String sdkVersion) {
}
}

public void addFeatureFlag(String flag, boolean value) {
featureFlags.put(flag, value);
}

public void save(File file) throws AndrolibException {
try (YamlWriter writer = new YamlWriter(new FileOutputStream(file))) {
write(writer);
Expand Down Expand Up @@ -235,7 +240,7 @@ public void readItem(YamlReader reader) throws AndrolibException {
}
case "sdkInfo": {
sdkInfo.clear();
reader.readMap(sdkInfo);
reader.readStringMap(sdkInfo);
break;
}
case "packageInfo": {
Expand All @@ -248,6 +253,11 @@ public void readItem(YamlReader reader) throws AndrolibException {
reader.readObject(versionInfo);
break;
}
case "featureFlags": {
featureFlags.clear();
reader.readBoolMap(featureFlags);
break;
}
case "sharedLibrary": {
sharedLibrary = line.getValueBool();
break;
Expand All @@ -270,9 +280,12 @@ public void write(YamlWriter writer) {
writer.writeString("apkFileName", apkFileName);
writer.writeBool("isFrameworkApk", isFrameworkApk);
writer.writeObject("usesFramework", usesFramework);
writer.writeStringMap("sdkInfo", sdkInfo);
writer.writeMap("sdkInfo", sdkInfo);
writer.writeObject("packageInfo", packageInfo);
writer.writeObject("versionInfo", versionInfo);
if (!featureFlags.isEmpty()) {
writer.writeMap("featureFlags", featureFlags);
}
writer.writeBool("sharedLibrary", sharedLibrary);
writer.writeBool("sparseResources", sparseResources);
if (!doNotCompress.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,21 @@ public void readIntList(List<Integer> list) throws AndrolibException {
readList(list, (items, reader) -> items.add(reader.getLine().getValueInt()));
}

public void readMap(Map<String, String> map) throws AndrolibException {
public void readStringMap(Map<String, String> map) throws AndrolibException {
readObject(map,
line -> line.hasColon,
(items, reader) -> {
YamlLine line = reader.getLine();
items.put(line.getKey(), line.getValue());
});
}

public void readBoolMap(Map<String, Boolean> map) throws AndrolibException {
readObject(map,
line -> line.hasColon,
(items, reader) -> {
YamlLine line = reader.getLine();
items.put(line.getKey(), line.getValueBool());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -95,15 +95,15 @@ public <T> void writeList(String key, List<T> list) {
}
}

public void writeStringMap(String key, Map<String, String> map) {
public <T> void writeMap(String key, Map<String, T> map) {
if (Objects.isNull(map)) {
return;
}
writeIndent();
mWriter.println(escape(key) + ":");
nextIndent();
for (String mapKey : map.keySet()) {
writeString(mapKey, map.get(mapKey));
writeString(mapKey, String.valueOf(map.get(mapKey)));
}
prevIndent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.*;
import java.io.*;
import java.util.*;
import java.util.logging.Logger;

public final class ResXmlPatcher {
Expand Down Expand Up @@ -357,6 +358,42 @@ public static void renameManifestPackage(File file, String packageOriginal) {
}
}

/**
* Finds all feature flags set on permissions in AndroidManifest.xml.
*
* @param file File for AndroidManifest.xml
*/
public static List<String> pullManifestFeatureFlags(File file) {
if (!file.exists()) {
return null;
}
try {
Document doc = loadDocument(file);
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression expression = xPath.compile("/manifest/permission");

Object result = expression.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;

List<String> featureFlags = new ArrayList<>();

for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
NamedNodeMap attrs = node.getAttributes();
Node featureFlagAttr = attrs.getNamedItem("android:featureFlag");

if (featureFlagAttr != null) {
featureFlags.add(featureFlagAttr.getNodeValue());
}
}

return featureFlags;

} catch (SAXException | ParserConfigurationException | IOException | XPathExpressionException ignored) {
return null;
}
}

/**
*
* @param file File to load into Document
Expand Down