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

Support all versions of Android #156

Closed
Closed
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
Binary file modified libs/ARSCLib.jar
Binary file not shown.
739 changes: 739 additions & 0 deletions src/main/java/com/java/util/Base64.java

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/apkeditor/ResourceStrings.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private Properties loadProperties(String config) {
throw new RuntimeException("Missing resource: '" + path + "'");
}
try {
properties.load(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
properties.load(new InputStreamReader(inputStream, "UTF-8"));
try {
inputStream.close();
} catch (Throwable ignored) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/apkeditor/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.function.Predicate;
import org.apache.commons.collections4.Predicate;

public class Util {
public static boolean isHelp(String[] args){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.reandroid.utils.collection.EmptyList;

import java.util.List;
import java.util.function.Predicate;
import org.apache.commons.collections4.Predicate;

public class AndroidManifestHelper {

Expand Down
62 changes: 43 additions & 19 deletions src/main/java/com/reandroid/apkeditor/info/Info.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.reandroid.dex.sections.SectionType;
import com.reandroid.utils.CompareUtil;
import com.reandroid.utils.HexUtil;
import com.reandroid.utils.StringsUtil;
import com.reandroid.utils.collection.CollectionUtil;
import com.reandroid.utils.collection.ComputeIterator;

Expand Down Expand Up @@ -156,7 +157,7 @@ private void printSignatures(ApkModule apkModule) throws IOException {
}
private void printResList(ApkModule apkModule) throws IOException {
InfoOptions options = getOptions();
if(options.resList.size() == 0){
if(options.resList.isEmpty()){
return;
}
if(!apkModule.hasTableBlock()){
Expand Down Expand Up @@ -307,10 +308,14 @@ private void printUsesPermissions(ApkModule apkModule) throws IOException {
return;
}
List<String> usesPermissions = manifest.getUsesPermissions();
if(usesPermissions.size() == 0){
return;
if(usesPermissions.isEmpty()) return;
Object[] a = usesPermissions.toArray();
com.reandroid.utils.collection.ArraySort.sort(a, (Comparator) CompareUtil.getComparableComparator());
ListIterator<String> i = usesPermissions.listIterator();
for (Object e : a) {
i.next();
i.set((String) e);
}
usesPermissions.sort(CompareUtil.getComparableComparator());
//printLine("Uses permission (" + usesPermissions.size() + ")");
String tag = AndroidManifest.TAG_uses_permission;
InfoWriter infoWriter = getInfoWriter();
Expand All @@ -326,7 +331,7 @@ private void printActivities(ApkModule apkModule) throws IOException {
return;
}
List<ResXmlElement> activityList = CollectionUtil.toList(manifest.getActivities(true));
if(activityList.size() == 0){
if(activityList.isEmpty()){
return;
}
ResXmlElement main = manifest.getMainActivity();
Expand Down Expand Up @@ -432,13 +437,16 @@ private void printConfigurations(ApkModule apkModule) throws IOException {
List<String> qualifiers = CollectionUtil.toUniqueList(
ComputeIterator.of(iterator, config -> {
String qualifier = config.getQualifiers();
if (qualifier.length() != 0) {
qualifier = qualifier.substring(1);
}
return qualifier;
return StringsUtil.isEmpty(qualifier) ? qualifier : qualifier.substring(1);
}));

qualifiers.sort(CompareUtil.getComparableComparator());
Object[] a = qualifiers.toArray();
com.reandroid.utils.collection.ArraySort.sort(a, (Comparator) CompareUtil.getComparableComparator());
ListIterator<String> i = qualifiers.listIterator();
for (Object e : a) {
i.next();
i.set((String) e);
}

getInfoWriter().writeArray("configurations", qualifiers.toArray(new String[0]));
}
Expand All @@ -451,7 +459,13 @@ private void printLanguages(ApkModule apkModule) throws IOException {
List<String> languages = CollectionUtil.toUniqueList(
ComputeIterator.of(iterator, ResConfig::getLanguage));

languages.sort(CompareUtil.getComparableComparator());
Object[] a = languages.toArray();
com.reandroid.utils.collection.ArraySort.sort(a, (Comparator) CompareUtil.getComparableComparator());
ListIterator<String> i = languages.listIterator();
for (Object e : a) {
i.next();
i.set((String) e);
}

getInfoWriter().writeArray("languages", languages.toArray(new String[0]));
}
Expand All @@ -466,7 +480,13 @@ private void printLocales(ApkModule apkModule) throws IOException {

locales.remove("");

locales.sort(CompareUtil.getComparableComparator());
Object[] a = locales.toArray();
com.reandroid.utils.collection.ArraySort.sort(a, (Comparator) CompareUtil.getComparableComparator());
ListIterator<String> i = locales.listIterator();
for (Object e : a) {
i.next();
i.set((String) e);
}

getInfoWriter().writeArray("locales", locales.toArray(new String[0]));
}
Expand All @@ -486,7 +506,7 @@ private void printEntries(ApkModule apkModule, String varName, int resourceId) t
return;
}
List<Entry> entryList = tableBlock.resolveReference(resourceId);
if(entryList.size() == 0){
if(entryList.isEmpty()){
logWarn("WARN: Can't find resource: " + HexUtil.toHex8("@0x", resourceId));
//infoWriter.writeNameValue(varName, HexUtil.toHex8("@0x", resourceId));
return;
Expand Down Expand Up @@ -564,13 +584,17 @@ private static List<Entry> sortEntries(Collection<Entry> entryCollection) {
}else {
results = new ArrayList<>(entryCollection);
}
Comparator<Entry> cmp = new Comparator<Entry>() {
@Override
public int compare(Entry entry1, Entry entry2) {
return entry1.getResConfig().compareTo(entry2.getResConfig());
}
Comparator<Object> cmp = (entry1, entry2) -> {
return ((Entry)entry1).getResConfig().compareTo(((Entry)entry2).getResConfig());
};
results.sort(cmp);
//results.sort(cmp);
Object[] elements = results.toArray();
com.reandroid.utils.collection.ArraySort.sort(elements, cmp);
ListIterator<Entry> iterator = results.listIterator();
for (Object element : elements) {
iterator. next();
iterator. set((Entry) element);
}
return results;
}
}
12 changes: 6 additions & 6 deletions src/main/java/com/reandroid/apkeditor/info/InfoWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.reandroid.apkeditor.info;

import com.java.util.Base64;
import com.reandroid.archive.block.ApkSignatureBlock;
import com.reandroid.archive.block.CertificateBlock;
import com.reandroid.arsc.chunk.PackageBlock;
Expand All @@ -32,7 +33,9 @@
import java.io.Closeable;
import java.io.IOException;
import java.io.Writer;
import java.util.*;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

public abstract class InfoWriter implements Closeable {
private final Writer writer;
Expand Down Expand Up @@ -71,10 +74,7 @@ public void writeDexInfo(DexDirectory dexDirectory) throws IOException {
public abstract void writeNameValue(String name, Object value) throws IOException;
public abstract void flush() throws IOException;
boolean contains(SpecTypePair specTypePair, List<String> filterList){
if(filterList.size() == 0){
return true;
}
return filterList.contains(specTypePair.getTypeName());
return filterList.isEmpty() || filterList.contains(specTypePair.getTypeName());
}
public Writer getWriter() {
return writer;
Expand Down Expand Up @@ -108,7 +108,7 @@ static String getValueAsString(Value value){
return HexUtil.toHex8("0x", value.getData());
}
static String toBase64(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
return Base64.encodeToString(bytes, 0);
}

static void writeSpaces(Writer writer, int amount) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/reandroid/apkeditor/merge/Merger.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.function.Predicate;
import org.apache.commons.collections4.Predicate;

public class Merger extends CommandExecutor<MergerOptions> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ private String[] loadDictionary(File file, String resource) {
}
ArrayCollection<String> results = new ArrayCollection<>(
StringsUtil.split(full, '\n', true));
results.removeIf(StringsUtil::isEmpty);
results.removeIf(new org.apache.commons.collections4.Predicate<String>() {
@Override
public boolean evaluate(String text) {
return StringsUtil.isEmpty(text);
}
});
return results.toArray(new String[results.size()]);
}
public boolean isKeepType(String type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private static Map<Integer, HtmlColor> loadColorNames(){
continue;
}
Integer val=htmlColor.getRGBValue();
results.putIfAbsent(val, htmlColor);
if (results.get(val) == null) results.put(val, htmlColor);
}
} catch (IOException e) {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,13 @@ public XMLDocument toXMLDocument(){
}
public List<TypeName> listTypeNames(){
List<TypeName> results=new ArrayList<>(this.map.values());
results.sort(this);
Object[] a = results.toArray();
com.reandroid.utils.collection.ArraySort.sort(a, (Comparator) this);
ListIterator<TypeName> i = results.listIterator();
for (Object e : a) {
i.next();
i.set((com.reandroid.apkeditor.refactor.TypeNameMap.TypeName) e);
}
return results;
}
public int count(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.function.Predicate;
import org.apache.commons.collections4.Predicate;

public class SmaliDecompiler implements DexDecoder {

Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/reandroid/apkeditor/smali/SmaliUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@
package com.reandroid.apkeditor.smali;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;

public class SmaliUtil {

static void sortDexFiles(List<File> fileList){
fileList.sort(new Comparator<File>() {
Object[] a = fileList.toArray();
com.reandroid.utils.collection.ArraySort.sort(a, (Comparator) new Comparator<File>() {
@Override
public int compare(File file1, File file2) {
int i1 = getDexNumber(file1.getName());
Expand All @@ -36,6 +39,11 @@ public int compare(File file1, File file2) {
return 1;
}
});
ListIterator<File> i = fileList.listIterator();
for (Object e : a) {
i.next();
i.set((File) e);
}
}
static String getDexFileName(int i){
if(i==0){
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/com/reandroid/apkeditor/utils/StringHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.ListIterator;

public class StringHelper {
public static String trueOrNull(Boolean value){
Expand All @@ -34,13 +36,14 @@ public static String trueOrNull(boolean value){
return String.valueOf(true);
}
public static List<String> sortAscending(List<String> nameList){
Comparator<String> cmp=new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
return s1.compareTo(s2);
}
};
nameList.sort(cmp);
Comparator<String> cmp= (s1, s2) -> s1.compareTo(s2);
Object[] a = nameList.toArray();
com.reandroid.utils.collection.ArraySort.sort(a, (Comparator) cmp);
ListIterator<String> i = nameList.listIterator();
for (Object e : a) {
i.next();
i.set((String) e);
}
return nameList;
}
public static void printNameAndValues(StringBuilder builder, String tab, int totalWidth, Object[][] objTable){
Expand Down
36 changes: 16 additions & 20 deletions src/main/java/com/reandroid/commons/utils/ElapsedTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,29 +60,25 @@ private long getDiff(){
}
return System.currentTimeMillis()-startTime;
}
private String msToDisplayTime(long l, boolean includeMs, boolean ignoreZero){
final long hr = TimeUnit.MILLISECONDS.toHours(l);
final long min = TimeUnit.MILLISECONDS.toMinutes(l - TimeUnit.HOURS.toMillis(hr));
final long sec = TimeUnit.MILLISECONDS.toSeconds(l - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min));
if(!includeMs){
return String.format(Locale.ENGLISH,"%02d:%02d:%02d", hr, min, sec);
private String msToDisplayTime(long l, boolean includeMs, boolean ignoreZero) {
long hr = l / 3600000; // 1 hour = 3600000 milliseconds
long min = (l % 3600000) / 60000; // 1 minute = 60000 milliseconds
long sec = (l % 60000) / 1000; // 1 second = 1000 milliseconds
long ms = l % 1000; // Remaining milliseconds

if (!includeMs) {
return String.format(Locale.ENGLISH, "%02d:%02d:%02d", hr, min, sec);
}
final long ms = TimeUnit.MILLISECONDS.toMillis(l - TimeUnit.HOURS.toMillis(hr) - TimeUnit.MINUTES.toMillis(min) - TimeUnit.SECONDS.toMillis(sec));
boolean showMs=min<10;
if(ignoreZero){
if(hr==0&&min==0){
return String.format(Locale.ENGLISH,"%02d.%03d", sec, ms);

boolean showMs = min < 10;
if (ignoreZero) {
if (hr == 0 && min == 0) {
return String.format(Locale.ENGLISH, "%02d.%03d", sec, ms);
}
if(hr==0){
if(showMs){
return String.format(Locale.ENGLISH,"%02d:%02d.%03d", min, sec, ms);
}
return String.format(Locale.ENGLISH,"%02d:%02d", min, sec);
if (hr == 0) {
return showMs ? String.format(Locale.ENGLISH, "%02d:%02d.%03d", min, sec, ms) : String.format(Locale.ENGLISH, "%02d:%02d", min, sec);
}
}
if(showMs){
return String.format(Locale.ENGLISH,"%02d:%02d:%02d.%03d", hr, min, sec, ms);
}
return String.format(Locale.ENGLISH,"%02d:%02d:%02d", hr, min, sec);
return showMs ? String.format(Locale.ENGLISH, "%02d:%02d:%02d.%03d", hr, min, sec, ms) : String.format(Locale.ENGLISH, "%02d:%02d:%02d", hr, min, sec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,7 @@ private static Writer createWriter(File file) throws IOException {
dir.mkdirs();
}
FileOutputStream outputStream=new FileOutputStream(file, true);
OutputStreamWriter writer=new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
return writer;
return new OutputStreamWriter(outputStream, "UTF-8");
}
public static FileLogger create(File file){
try {
Expand Down