Skip to content
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

Added parsing plurals into the JSON #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
mvn clean compile assembly:single

cp ./target/appium_apk_tools-0.0.2-SNAPSHOT-jar-with-all.jar ./appium_apk_tools.jar
cp ./target/appium_apk_tools-0.0.3-SNAPSHOT-jar-with-all.jar ./appium_apk_tools.jar
Binary file added lib/apktool-cli-2.2.2-SNAPSHOT.jar
Binary file not shown.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>appium_apk_tools</groupId>
<artifactId>appium_apk_tools</artifactId>
<version>0.0.2-SNAPSHOT</version>
<version>0.0.3-SNAPSHOT</version>
<name>appium_apk_tools</name>
<!-- mvn clean compile assembly:single -->
<packaging>jar</packaging>
Expand All @@ -25,8 +25,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.0</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
Expand Down Expand Up @@ -56,12 +56,12 @@
<dependency>
<groupId>apktool.snapshot</groupId>
<artifactId>apktool</artifactId>
<version>1.5.3</version>
<version>2.2.2</version>
<scope>system</scope>
<!--
systemPath is used because apktool is not in any maven repo.
setting up a local maven repo for one dependency doesn't make sense. -->
<systemPath>${project.basedir}/lib/apktool-cli-1.5.3-SNAPSHOT.jar</systemPath>
<systemPath>${project.basedir}/lib/apktool-cli-2.2.2-SNAPSHOT.jar</systemPath>
</dependency>
</dependencies>
</project>
67 changes: 53 additions & 14 deletions src/io/appium/apktools/StringsXML.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import brut.androlib.res.data.ResResource;
import brut.androlib.res.data.ResTable;
import brut.androlib.res.data.ResValuesFile;
import brut.androlib.res.data.value.ResPluralsValue;
import brut.androlib.res.data.value.ResScalarValue;
import brut.androlib.res.util.ExtFile;
import brut.androlib.res.util.ExtMXSerializer;
Expand All @@ -34,15 +35,16 @@ public static void p(final Object msg) {
}
}

public static void toJSON(final ResValuesFile input,
final File outputDirectory) throws Exception {
String[] paths = input.getPath().split("/"); // always "/" even on Windows
public static void toJSON(final ResValuesFile stringValues,
final ResValuesFile pluralsValues,
final File outputDirectory) throws Exception {
String[] paths = stringValues.getPath().split("/"); // always "/" even on Windows
final String outName = paths[paths.length - 1].replaceFirst("\\.xml$",
".json");
".json");
final File outFile = new File(outputDirectory, outName);
p("Saving to: " + outFile);
JsonGenerator generator = json.createGenerator(
new FileOutputStream(outFile), JsonEncoding.UTF8);
new FileOutputStream(outFile), JsonEncoding.UTF8);

// Ensure output stream is auto closed when generator.close() is called.
generator.configure(Feature.AUTO_CLOSE_TARGET, true);
Expand All @@ -56,20 +58,48 @@ public static void toJSON(final ResValuesFile input,
generator.useDefaultPrettyPrinter();

// ResStringValue extends ResScalarValue which has field mRawValue
final Field valueField = ResScalarValue.class.getDeclaredField("mRawValue");
valueField.setAccessible(true);
final Field stringValueField = ResScalarValue.class.getDeclaredField("mRawValue");
stringValueField.setAccessible(true);
final Field pluralsValueField = ResPluralsValue.class.getDeclaredField("mItems");
pluralsValueField.setAccessible(true);
final Field pluralsQuantityField = ResPluralsValue.class.getDeclaredField("QUANTITY_MAP");
pluralsQuantityField.setAccessible(true);
final String[] pluralsQuantities = (String[]) pluralsQuantityField.get(String[].class);

generator.writeStartObject();
for (ResResource resource : input.listResources()) {
if (input.isSynthesized(resource)) {
for (ResResource resource : stringValues.listResources()) {
if (stringValues.isSynthesized(resource)) {
continue;
}

final String name = resource.getResSpec().getName();
// Get the value field from the ResStringValue object.
final String value = (String) valueField.get(resource.getValue());
final String value = (String) stringValueField.get(resource.getValue());
generator.writeStringField(name, value);
}

if (pluralsValues != null)
{
for (ResResource resource : pluralsValues.listResources()) {
if (pluralsValues.isSynthesized(resource)) {
continue;
}

final String name = resource.getResSpec().getName();
generator.writeObjectFieldStart(name);
// Get the values field from the ResPluralsValue object.
ResScalarValue[] valuesArray = (ResScalarValue[]) pluralsValueField.get(resource.getValue());
for (int i = 0; i < valuesArray.length; i++)
{
ResScalarValue value = valuesArray[i];
if (value != null) {
generator.writeStringField(pluralsQuantities[i], value.encodeAsResXmlValue());
}
}
generator.writeEndObject();
}
}

generator.writeEndObject();
generator.flush();
generator.close();
Expand All @@ -83,19 +113,27 @@ public static void run(final File input, final File outputDirectory,
final ExtFile apkFile = new ExtFile(input);
ResTable table = res.getResTable(apkFile, true);
ResValuesFile stringsXML = null;
ResValuesFile pluralsXML = null;
final String stringsTargetPath = (localization + "/strings.xml").toLowerCase();
final String pluralsTargetPath = (localization + "/plurals.xml").toLowerCase();
for (ResPackage pkg : table.listMainPackages()) {
p(pkg);
for (ResValuesFile values : pkg.listValuesFiles()) {
// strings.xml is not case sensitive. xamarin will call it Strings.xml
final String path = values.getPath().toLowerCase();
final String targetPath = (localization + "/strings.xml").toLowerCase();
p(path);
if (path.endsWith(targetPath)) {
if (path.endsWith(stringsTargetPath)) {
stringsXML = values;
}
if (path.endsWith(pluralsTargetPath)) {
pluralsXML = values;
}
if (stringsXML != null && pluralsXML != null)
{
break;
}
}
if (stringsXML != null) {
if (stringsXML != null && pluralsXML != null) {
break;
}
}
Expand All @@ -104,7 +142,8 @@ public static void run(final File input, final File outputDirectory,
e("Could not find the strings.xml file for localization: " + localization);
}

toJSON(stringsXML, outputDirectory);
toJSON(stringsXML, pluralsXML, outputDirectory);

p("complete");
}

Expand Down