From b8eaff66faa0e10796f91d27a7398a2afb0e7610 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sat, 28 Jul 2018 23:40:41 +0800 Subject: [PATCH] Shrink snet APK, and prevent crashing --- .../com/topjohnwu/magisk/utils/Const.java | 4 +-- snet/build.gradle | 7 +++-- snet/proguard-rules.pro | 3 +- .../com/topjohnwu/snet/ModdedGPSUtil.java | 29 ++++++++++--------- .../com/topjohnwu/snet/SafetyNetHelper.java | 8 ++--- 5 files changed, 28 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Const.java b/app/src/main/java/com/topjohnwu/magisk/utils/Const.java index 122a1d3d4..b7ffbb5aa 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Const.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Const.java @@ -39,7 +39,7 @@ public class Const { // Versions public static final int UPDATE_SERVICE_VER = 1; - public static final int SNET_VER = 9; + public static final int SNET_VER = 10; public static int MIN_MODULE_VER() { return MagiskManager.get().magiskVersionCode >= MAGISK_VER.REMOVE_LEGACY_LINK ? 1500 : 1400; @@ -81,7 +81,7 @@ public class Const { public static class Url { public static final String STABLE_URL = "https://raw.githubusercontent.com/topjohnwu/magisk_files/master/stable.json"; public static final String BETA_URL = "https://raw.githubusercontent.com/topjohnwu/magisk_files/master/beta.json"; - public static final String SNET_URL = "https://github.com/topjohnwu/magisk_files/raw/fc819e3974e96d0e4430a2957df4410971ebd6f3/snet.apk"; + public static final String SNET_URL = "https://github.com/topjohnwu/magisk_files/raw/a300521162587da23e45010797bfd8c9a03594f6/snet.apk"; public static final String REPO_URL = "https://api.github.com/users/Magisk-Modules-Repo/repos?per_page=100&page=%d"; public static final String FILE_URL = "https://raw.githubusercontent.com/Magisk-Modules-Repo/%s/master/%s"; public static final String ZIP_URL = "https://github.com/Magisk-Modules-Repo/%s/archive/master.zip"; diff --git a/snet/build.gradle b/snet/build.gradle index 23a484182..31a144e43 100644 --- a/snet/build.gradle +++ b/snet/build.gradle @@ -16,12 +16,15 @@ android { release { minifyEnabled true shrinkResources true - proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' + proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'com.google.android.gms:play-services-safetynet:7.0.0' /* The oldest version */ + /* The oldest version */ + implementation('com.google.android.gms:play-services-safetynet:7.0.0') { + exclude module: 'support-v4' + } } diff --git a/snet/proguard-rules.pro b/snet/proguard-rules.pro index 8cc07d286..6f3f1285f 100644 --- a/snet/proguard-rules.pro +++ b/snet/proguard-rules.pro @@ -20,6 +20,7 @@ # hide the original source file name. #-renamesourcefileattribute SourceFile --keep class com.topjohnwu.snet.* { *; } +-ignorewarnings +-keep class com.topjohnwu.snet.Snet { *; } -dontwarn java.lang.invoke** -dontwarn com.google.android.gms.common.GooglePlayServicesUtil** diff --git a/snet/src/main/java/com/topjohnwu/snet/ModdedGPSUtil.java b/snet/src/main/java/com/topjohnwu/snet/ModdedGPSUtil.java index 918c68661..3ab005e04 100644 --- a/snet/src/main/java/com/topjohnwu/snet/ModdedGPSUtil.java +++ b/snet/src/main/java/com/topjohnwu/snet/ModdedGPSUtil.java @@ -5,13 +5,14 @@ import android.app.AlertDialog; import android.app.Dialog; import android.content.Context; import android.content.ContextWrapper; +import android.content.DialogInterface; import android.content.Intent; +import android.content.pm.PackageManager; import android.content.res.AssetManager; import android.content.res.Resources; import android.util.Log; import com.google.android.gms.common.GooglePlayServicesUtil; -import com.google.android.gms.common.internal.zzg; import com.google.android.gms.internal.zzlu; /* Decompiled and modified from GooglePlayServiceUtil.class */ @@ -20,7 +21,7 @@ public class ModdedGPSUtil { private static final String TAG = "GooglePlayServicesUtil"; static String dexPath; - static Dialog getErrorDialog(int errCode, Activity activity, int requestCode) { + static Dialog getErrorDialog(int errCode, final Activity activity, final int requestCode) { SwapResContext ctx = new SwapResContext(activity, dexPath); Resources res = ctx.getResources(); if (zzlu.zzQ(ctx) && errCode == 2) { @@ -33,8 +34,16 @@ public class ModdedGPSUtil { String btnMsg = GooglePlayServicesUtil.zzf(ctx, errCode); if (btnMsg != null) { - Intent intent = GooglePlayServicesUtil.zzan(errCode); - builder.setPositiveButton(btnMsg, new zzg(activity, intent, requestCode)); + final Intent intent = GooglePlayServicesUtil.zzan(errCode); + builder.setPositiveButton(btnMsg, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int i) { + PackageManager pm = activity.getPackageManager(); + if (intent != null && intent.resolveActivity(pm) != null) + activity.startActivityForResult(intent, requestCode); + dialog.dismiss(); + } + }); } switch(errCode) { @@ -116,20 +125,14 @@ public class ModdedGPSUtil { public SwapResContext(Context base, String apk) { super(base); - asset = getAssets(apk); - Resources res = base.getResources(); - resources = new Resources(asset, res.getDisplayMetrics(), res.getConfiguration()); - } - - private AssetManager getAssets(String apk) { try { - AssetManager asset = AssetManager.class.newInstance(); + asset = AssetManager.class.newInstance(); AssetManager.class.getMethod("addAssetPath", String.class).invoke(asset, apk); - return asset; } catch (Exception e) { e.printStackTrace(); - return null; } + Resources res = base.getResources(); + resources = new Resources(asset, res.getDisplayMetrics(), res.getConfiguration()); } @Override diff --git a/snet/src/main/java/com/topjohnwu/snet/SafetyNetHelper.java b/snet/src/main/java/com/topjohnwu/snet/SafetyNetHelper.java index 777d0a496..419e28112 100644 --- a/snet/src/main/java/com/topjohnwu/snet/SafetyNetHelper.java +++ b/snet/src/main/java/com/topjohnwu/snet/SafetyNetHelper.java @@ -2,8 +2,6 @@ package com.topjohnwu.snet; import android.app.Activity; import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; import android.util.Base64; import com.google.android.gms.common.ConnectionResult; @@ -31,7 +29,7 @@ public class SafetyNetHelper implements InvocationHandler, GoogleApiClient.Conne public static final int BASIC_PASS = 0x10; public static final int CTS_PASS = 0x20; - public static final int SNET_EXT_VER = 9; + public static final int SNET_EXT_VER = 10; private GoogleApiClient mGoogleApiClient; private Activity mActivity; @@ -81,14 +79,14 @@ public class SafetyNetHelper implements InvocationHandler, GoogleApiClient.Conne } @Override - public void onConnectionFailed(@NonNull ConnectionResult result) { + public void onConnectionFailed(ConnectionResult result) { if (GooglePlayServicesUtil.isUserRecoverableError(result.getErrorCode())) ModdedGPSUtil.getErrorDialog(result.getErrorCode(), mActivity, 0).show(); invokeCallback(CONNECTION_FAIL); } @Override - public void onConnected(@Nullable Bundle bundle) { + public void onConnected(Bundle bundle) { // Create nonce byte[] nonce = new byte[24]; new SecureRandom().nextBytes(nonce);