A quick way to confuse beginners with Android

Keywords: Android Google Java SDK

A fast way to confuse small whites for Android

Why confuse

Simply put, Android ProGuard can compress, confuse, pre-check, optimize, and although not safer, it is still an important part.

Start confusing step one

First turn on the confused switch in build.gradle, where minifyEnabled is set to true

Then we'll go to proguard-rules.pro and write our confused rules, such as the name of the file, to prevent important classes from being removed from confusion.

Several commands related to rules that keep relevant elements out of confusion are highlighted first:

command Effect
-keep Prevent classes and members from being removed or renamed
-keepnames Prevent class and member renames
-keepclassmembers Prevent members from being removed or renamed
-keepclasseswithmembers Prevent classes and members that have this member from being removed or renamed
-keepclasseswithmembernames Prevent classes and members that have this member from being renamed

Fast confusion?

The first step is to set up a template, which is to find templates shared by others on the online blog, such as the four components, mainstream open source libraries, JNI calls local methods, R resources, etc. There are generally confusion rules. One thing to note is that the set of templates, but also a set of templates with a relatively close time, no rollover templates, can save more time, after all, so many lines may not necessarily be able toQuickly see, it's embarrassing to pack it out and find something wrong.

Generally speaking, after using confusing templates, there are still many project-specific parts that need to be confused, such as our custom View, Json parsed entity classes, the classes used for reflection, and the non-mainstream open source frameworks we rely on, SDK s, and so on.
For custom Views, many templates use keep directives to prevent confusion with classes that inherit from Views.One lazy way to do this is to put your custom View in the same package and write all its classes into the confusion rule.
For Json parsed entity classes, the same is true for reflection, finding those that cannot be removed and writing them into your own confusion rules.

So if when you do a project, you think ahead of time about confusing it, and you write the confusion rules when you import dependencies, you will be happy afterwards.

What if the relying non-mainstream open source library authors don't mention anything about confusion and don't find it in demo?

First, determine if it has a built-in confusion rule.If you don't state that its obfuscation rule is working, it's already confusing.

However, life is not always smooth and there are usually all kinds of mistakes, so it needs to be confused.

The easiest way is to find the dependent package and write confusion rules for all its classes.For example:

A non-mainstream open source library was imported into the project, and then I need to write confusion rules on it to make sure it works properly.

implementation 'site.gemus:openingstartanimation:1.0.0'

You can't think of a place to use it directly at this point, just keep class about its import.

Since it may use other classes in the package internally, it is best to carefully check that the simplest (tou) list (lan) is to use keep instructions for all classes in the package to prevent them from being confused.As follows:

Generally speaking, you can prevent the classes in this open source library from being confused.

What if the SDK is imported, the official website does not mention the confusion, and demo does not find it?

for example

First use the -libraryjars command to declare that the imported jar packages and so files are preserved.

-libraryjars libs/Msc.jar
#-libraryjars libs/armeabi-v7a/libmsc.so
#-libraryjars libs/armeabi-v7a/libmsc.so

If there are still errors, try going to the External library folder or using this SDK to find the package for this SDK and keep all the classes in this package with keep instructions.For example:

Write confusion rules based on package name

-keep class com.iflytek.**{*;}
-keepattributes Signature

In general, this avoids confusing classes for this SDK

Template Time

Okay, finally attach a confusion rule (pseudo template) for your own project

# Specify a code compression level of 0 - 7 (specifying the number of iterations the code will optimize, which defaults to 5 in Android, and this directive only works when it can be optimized.)
-optimizationpasses 5
# Confusion does not result in a variety of class names (mixed case class names are not used for confusion)
-dontusemixedcaseclassnames
# Specifies that non-public library classes are not ignored (non-public classes in libraries are not skipped)
-dontskipnonpubliclibraryclasses
# Specifies that members of library classes that do not ignore package visibility
-dontskipnonpubliclibraryclassmembers
#No optimization, this option is recommended.
-dontoptimize
 # Without pre-validation, Android does not need it to speed up confusion.
-dontpreverify
# Blocking Warning
-ignorewarnings
# Specifies that the ambiguity is the algorithm used, followed by a filter
# This filter is Google's recommended algorithm and generally remains unchanged
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
# Protect Annotation from confusion in code
-keepattributes *Annotation*
# Avoid confusing generics, which is important when mapping JSON entities
-keepattributes Signature
# Keep line numbers when throwing exceptions
-keepattributes SourceFile,LineNumberTable
 #Allowing access to and modifying modifiers on classes and class members during optimization can improve the results of the optimization steps.
# For example, when inlining a public getter method, this may also require public access from outside the country.
# Although this is not required by the java binary specification, otherwise some virtual machines will have problems handling this code.This applies when there are optimizations and use-repackageclasses.
#Indicator: Code in the library cannot be processed with this directive because some classes and class members are not designed to be public and may become public in the api
-allowaccessmodification
#This applies when there are optimizations and use-repackageclasses.
-repackageclasses ''
 # Log confusion (print confusion details)
 # This sentence can confuse our project and produce a mapping file
 # Mapping relationships containing class names - > confused class names
-verbose
# -------------------------------------- Default Reservation-----------------------------
# Keep which classes unobscured
#Inherit activity,application,service,broadcastReceiver,contentprovider.... No confusion
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.support.multidex.MultiDexApplication
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class * extends android.view.View
-keep class android.support.** {*;}## Keep all classes under support and their internal classes

-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
#Represents a class that is not confused with the one declared above, and we can hardly use the last two classes, which are used when accessing some services native to Google.
#----------------------------------------------------

# Preserve inherited
-keep public class * extends android.support.v4.**
-keep public class * extends android.support.v7.**
-keep public class * extends android.support.annotation.**

#Indicates that the class name and the native method name of any class containing the native method are not confused, which is consistent with the results we just verified
-keepclasseswithmembernames class * {
    native <methods>;
}

#This is mainly the onclick method android:onclick="onClick" written in the layout, without confusion
#Indicates that a parameter in an Activity is not confused with a View method because there is a use to configure the android:onClick="buttonClick" attribute in XML.
#When the user clicks the button, the buttonClick(View view) method in the Activity is called, and if this method is confused, it cannot be found.
-keepclassmembers class * extends android.app.Activity{
    public void *(android.view.View);
}

#Indicates that the values() and valueOf() methods in an enumeration are not confused. I use very few enumerations, so I won't comment on them.
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

#Indicates that the setXxx() and getXxx() methods in any View are not confused.
#Because property animation requires corresponding setter and getter implementations, confusion can't work.
-keep public class * extends android.view.View{
    *** get*();
    void set*(***);
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclasseswithmembers class * {
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
}

#Indicates that the CREATOR field in the Parcelable implementation class is not confused.
#There is no doubt that the CREATOR field can never be changed, including case, or the entire Parcelable working mechanism will fail.
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
# This specifies that the following members of a class that inherits Serizalizable should not be removed from confusion
-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}
# Retain resources below R
-keep class **.R$* {
 *;
}
#Do not confuse static under resource class
-keepclassmembers class **.R$* {
    public static <fields>;
}

# For onXXEvent, **On*Listener with callback function, it should not be confused
-keepclassmembers class * {
    void *(**On*Event);
    void *(**On*Listener);
}

# Keep our custom controls (inherited from View) unobscured
-keep public class * extends android.view.View{
    *** get*();
    void set*(***);
    public <init>(android.content.Context);
    public <init>(android.content.Context, android.util.AttributeSet);
    public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keep class com.example.odm.garbagesorthelper.widget.** { *; }


#
#--------------------------------- WebView (Nothing to Ignore in Project) -------------------------------------------------------------------------------------------------
#
#webView needs special handling

#Special handling of JavaScript interaction with HTML5 in app
#We need to make sure that the native methods that these js are calling cannot be confused, so we need to do the following:


#
#------------------------------------------ Entity Class---------------------------
#-------- (Entity Model s cannot be confused, otherwise corresponding attributes cannot be found and values cannot be obtained)-----
#
-keep class com.example.odm.garbagesorthelper.model.entity.** { *; }
-dontwarn  com.example.odm.garbagesorthelper.model.entity.**

#Processing with reflective classes
-keep class com.example.odm.garbagesorthelper.utils.** { *; }

#
# ---------------------------------------------- Other-----------------------
#
# Remove Log-related code from code
-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int i(...);
    public static int w(...);
    public static int d(...);
    public static int e(...);
}

# Keep test-related code
-dontnote junit.framework.**
-dontnote junit.runner.**
-dontwarn android.test.**
-dontwarn android.support.test.**
-dontwarn org.junit.**
#
# -------------------------------------- Third party libraries, frameworks, SDK s------------------------------------------------------------------------------------------------------------
#logger
-dontwarn com.orhanobut.logger.**
-keep class com.orhanobut.logger.**{*;}
-keep interface com.orhanobut.logger.**{*;}

# Gson
-keep class com.google.gson.stream.** { *; }
-keepattributes EnclosingMethod
-dontwarn com.google.gson.**
-keep class com.google.gson.**{*;}
-keep interface com.google.gson.**{*;}
#gson
#If you use Gson parsing packages, you can successfully confuse them by adding the following lines or you will get an error.

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Prevent proguard from stripping interface information from TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
# Application classes that will be serialized/deserialized over Gso


# OkHttp3
-dontwarn okhttp3.logging.**
-keep class okhttp3.internal.**{*;}
-dontwarn okio.**

# Retrofit
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
#-keepattributes Signature-keepattributes Exceptions

# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions


# RxJava RxAndroid
-dontwarn sun.misc.**
-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
    long producerIndex;
    long consumerIndex;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
    rx.internal.util.atomic.LinkedQueueNode producerNode;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
    rx.internal.util.atomic.LinkedQueueNode consumerNode;
}

#LiveEventBus
-dontwarn com.jeremyliao.liveeventbus.**
-keep class com.jeremyliao.liveeventbus.** { *; }
-keep class androidx.lifecycle.** { *; }
-keep class androidx.arch.core.** { *; }

#glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}

#utilcodex
-keep class  com.blankj.utilcode.util.** { *; }

#
-keep class site.gemus.** { *; }

#XUI
-keep class com.xuexiang.xui.** { *; }

#Xinfei SDK
-libraryjars libs/Msc.jar
-libraryjars src/main/jniLibs/Msc.jar
-keep class com.iflytek.**{*;}
-keepattributes Signature

#RxPermission
-keep class com.tbruyelle.rxpermissions2.** { *; }

#XBanner
-keep class com.stx.xhb.androidx.XBanner.** { *; }

#Bottom navigation bar
-keep class com.aurelhubert.ahbottomnavigation.** { *; }


#Bugly SDK
-dontwarn com.tencent.bugly.**
-keep public class com.tencent.bugly.**{*;}
-keep class android.support.**{*;}
Published 5 original articles, won approval 3, visited 860
Private letter follow

Posted by KevMull on Fri, 17 Jan 2020 19:01:00 -0800