Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Merge pull request #133 from google/duplicate-dependencies-inspector
Browse files Browse the repository at this point in the history
Java dependencies inspector
  • Loading branch information
borisf authored Nov 8, 2016
2 parents 3f111b4 + 3b704ab commit e5f9235
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ClassySharkWS/src/com/google/classyshark/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
public class Version {

public static final int MAJOR = 7;
public static final int MINOR = 1;
public static final int MINOR = 2;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,17 @@ public void apply() {
apkDashboard = new ApkDashboard(apkFile);
apkDashboard.inspect();

ELEMENT element = new ELEMENT("\n ~ APK DASHBOARD ~\n" , TAG.IDENTIFIER);
elements.add(element);


Iterator<ClassesDexDataEntry> dexesIter = apkDashboard.iterator();

while (dexesIter.hasNext()) {

ClassesDexDataEntry dexEntry = dexesIter.next();

ELEMENT element = new ELEMENT("\n" + dexEntry.getName(), TAG.MODIFIER);
element = new ELEMENT("\n" + dexEntry.getName(), TAG.MODIFIER);
elements.add(element);

element = new ELEMENT(
Expand All @@ -74,7 +77,13 @@ public void apply() {
elements.add(element);
}

ELEMENT element = new ELEMENT("\n\n\nDynamic Symbol Errors", TAG.MODIFIER);
element = new ELEMENT("\n\nPossible Java Dependencies Errors", TAG.MODIFIER);
elements.add(element);

element = new ELEMENT("\n" + apkDashboard.getJavaDependenciesErrorsAsString(), TAG.DOCUMENT);
elements.add(element);

element = new ELEMENT("\n\n\nDynamic Symbol Errors", TAG.MODIFIER);
elements.add(element);

for (String error : apkDashboard.getNativeErrors()) {
Expand Down Expand Up @@ -131,11 +140,9 @@ public List<String> getDependencies() {

public String toString() {
StringBuilder sb = new StringBuilder();

for (ELEMENT element : elements) {
sb.append(element.text);
}

return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ public class ApkDashboard implements Iterable<ClassesDexDataEntry> {
public ArrayList<ClassesDexDataEntry> customClassesDexEntries = new ArrayList<>();
public List<String> nativeLibs = new ArrayList<>();
public ArrayList<String> nativeDependencies = new ArrayList<>();
public List<String> nativeErrors = new LinkedList<>();
public List<String> nativeErrors = new ArrayList<>();
public List<String> allClasses = new ArrayList<>();
public File apkFile;

public ApkDashboard(File apkFile) {
Expand Down Expand Up @@ -84,7 +85,6 @@ public String getPrivateLibErrorTag(String nativeLib) {
} else {
return "";
}

}

public static Set<String> getClassesWithNativeMethodsPerDexIndex(int dexIndex,
Expand All @@ -95,6 +95,25 @@ public static Set<String> getClassesWithNativeMethodsPerDexIndex(int dexIndex,
return dexInspectionsData.classesWithNativeMethods;
}

public String getJavaDependenciesErrorsAsString() {
List<String> javaDependenciesErrors = getJavaDependenciesErrors();

StringBuilder builder = new StringBuilder();

for (String javaDepError : javaDependenciesErrors) {
builder.append(javaDepError);
}

return builder.toString();
}

public List<String> getJavaDependenciesErrors() {
JavaDependenciesInspector ddi = new JavaDependenciesInspector(allClasses);
List<String> result = ddi.getInspections();

return result;
}

public static ClassesDexDataEntry fillAnalysisPerClassesDexIndex(int dexIndex, File classesDex) {
ClassesDexDataEntry dexData = new ClassesDexDataEntry(dexIndex);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2016 Google, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.classyshark.silverghost.translator.apk.dashboard;

import java.util.ArrayList;
import java.util.List;

public class JavaDependenciesInspector {

private final List<String> allClasses;

private int imageLoading = 0;
private boolean hasGlide;
private boolean hasPicasso;
private boolean hasFresco;

private int asyncHttp = 0;
private boolean hasOkHttp;
private boolean hasVolley;
private boolean hasLoopj;

private int jsonParsing = 0;
private boolean hasJackson;
private boolean hasGson;

// other dependencies
private boolean hasGuava;
private boolean hasDeprecatedHttp;
private boolean hasActionBarSherlock;
private boolean hasPullToRefresh;

public JavaDependenciesInspector(List<String> allClasses) {
this.allClasses = allClasses;
}

public List<String> getInspections() {
List result = new ArrayList<>();

for (String cName : allClasses) {
updateLogic(cName);
}

if (imageLoading > 1) {
result.add("\n* Duplicate image loading libraries - ");
if (hasPicasso) result.add("picasso ");
if (hasGlide) result.add("glide ");
if (hasFresco) result.add("fresco ");
}

if (asyncHttp > 1) {
result.add("\n* Duplicate async http libraries - ");
if (hasOkHttp) result.add("okhttp ");
if (hasVolley) result.add("volley ");
if (hasLoopj) result.add("loopj ");
}

if (jsonParsing > 1) {
result.add("\n* Duplicate json parsing - ");
if (hasJackson) result.add("jackson ");
if (hasGson) result.add("gson ");
}

if (hasGuava) {
result.add("\n* Guava (server side library)usage");
}

if (hasDeprecatedHttp) {
result.add("\n* Apache Http is deprecated");
}

if (hasActionBarSherlock) {
result.add("\n* ActionBar Sherlock is deprecated");
}

if (hasPullToRefresh) {
result.add("\n* PullToRefresh is deprecated");
}

return result;
}

private void updateLogic(String cName) {

if (cName.contains("glide") && !hasGlide) {
hasGlide = true;
imageLoading++;
} else if (cName.contains("picasso") && !hasPicasso) {
hasPicasso = true;
imageLoading++;
} else if (cName.contains("fresco") && !hasFresco) {
hasFresco = true;
imageLoading++;
} else if (cName.contains("okhttp") && !hasOkHttp) {
hasOkHttp = true;
asyncHttp++;
} else if (cName.contains("volley") && !hasVolley) {
hasVolley = true;
asyncHttp++;
} else if (cName.contains("loopj") && !hasLoopj) {
hasLoopj = true;
asyncHttp++;
} else if (cName.contains("fasterxml.jackson") && !hasJackson) {
hasJackson = true;
jsonParsing++;
} else if (cName.contains("google.code.gson") && !hasGson) {
hasGson = true;
jsonParsing++;
} else if (cName.contains("google.common") && !hasGuava) {
hasGuava = true;
} else if (cName.contains("apache.http") && !hasDeprecatedHttp) {
hasDeprecatedHttp = true;
} else if (cName.contains("'com.actionbarsherlock") && !hasActionBarSherlock) {
hasActionBarSherlock = true;
} else if (cName.contains("chrisbanes.pulltorefresh") && !hasPullToRefresh) {
hasPullToRefresh = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.classyshark.silverghost.translator.elf.ElfTranslator;
import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
Expand Down Expand Up @@ -125,6 +126,8 @@ public static void fillApkDashboard(File binaryArchiveFile, ApkDashboard to) {
} catch (Exception e) {
e.printStackTrace();
}

readClassNamesFromMultidex(binaryArchiveFile, to.allClasses, new ArrayList<ContentReader.Component>());
}

public static void readClassNamesFromMultidex(File binaryArchiveFile,
Expand Down

0 comments on commit e5f9235

Please sign in to comment.