Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the entry-level knowledge points of Android

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the basic knowledge points of Android". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Say in front.

Here we directly use Android Studio to explain how to confuse, Android Studio itself integrates ProGuard of Java language as a compression, optimization and obfuscation tool, it is easy to use with Gradle build tool, you only need to set minifyEnabled to true in the gradle file of the project application directory. Then we can add our obfuscation rules to the proguard-rules.pro file.

Android {... BuildTypes {release {minifyEnabled true proguardFiles getDefaultProguardFile ('proguard-android.txt'),' proguard-rules.pro'}

The above sample code indicates that the release version is obfuscated. Let's start with a brief introduction to the three major functions of ProGuard and briefly describe their commonly used commands.

ProGuard action

Shrinking: enabled by default to reduce application volume, remove unused classes and members, and perform again after the optimization action is performed (because some unused classes and members may be exposed again after optimization).

-dontshrink turns off compression

Optimization: enabled by default, optimizations are performed at the bytecode level to make applications run faster.

-dontoptimize turn off optimization-optimizationpasses n indicates the number of iterations that proguard optimizes the code. Android is generally 5.

Obfuscation: enabled by default, increases the difficulty of decompilation, and classes and class members are randomly named unless protected by keep.

-dontobfuscate turns off obfuscation

After confusion, a mapping.txt file will be generated by default under the project directory app/build/outputs/mapping/release, which is the confusion rule. We can push the confused code back to the source code according to this file, so this file is very important, so pay attention to protect it. In principle, the more chaotic and irregular the code is confused, the better, but there are some places where we should avoid confusion, otherwise the program will make mistakes, so here's what we need to teach you. How to avoid confusion in some of your code so as to prevent errors.

Basic rules

First look at the following two more commonly used commands, many children's shoes may be more confused about the difference between the two.

-keep class cn.hadcn.test.**-keep class cn.hadcn.test.*

A star means only to keep the class name under the package, but the class name under the subpackage will still be confused; two stars means to keep both the class name in this package and the class name under the subpackage; after using the above method to keep the class name, you will find that although the class name is not confused, the specific methods and variable names in it have changed. At this time, if you want to keep the class name and keep the contents of the package not confused, we need the following methods

-keep class cn.hadcn.test.* {*;}

On this basis, we can also use the basic rules of Java to protect specific classes from confusion, for example, we can use Java rules such as extend,implement. The following example avoids confusion of all classes that inherit Activity

-keep public class * extends android.app.Activity

If we want to keep the inner class in a class from being confused, we need to use the $symbol, as shown in the following example to keep all the public content in the ScriptFragment inner class JavaScriptInterface unconfused.

-keepclassmembers class cc.ninty.chat.ui.fragment.ScriptFragment$JavaScriptInterface {public *;}

Furthermore, if you do not want to keep all the content in a class from being confused, but just want to protect the specific content under the class, you can use the

; / / match all constructors; / match all fields; / / match all methods

You can also add private, public, native, etc., before or before to further specify content that will not be confused, such as

-keep class cn.hadcn.test.One {public;}

Indicates that all public methods under the One class will not be confused. Of course, you can also add parameters, such as the following that indicates that constructors with JSONObject as input parameters will not be confused

-keep class cn.hadcn.test.One {public (org.json.JSONObject);}

Sometimes you still think, I don't need to keep the class name, I just need to keep the specific methods under this class from being confused, then you can't use the keep method, the keep method will keep the class name, and you need to use keepclassmembers, so the class name will not be preserved, in order to understand these rules, the official website gives the following table

Keep prevent from being removed or renamed prevent classes and class members from being renamed-keep-keepnames class members only-if keepclassmembers-keepclassmembernames has a member, retain classes and class members-keepclasseswithmembers-keepclasseswithmembernames

Removal refers to whether it will be deleted when it is Shrinking. The above contents need to be mastered in the confusion rules, after understanding, you should be able to understand all the basic confusion rules documents. Combined with the following points for attention

Matters needing attention

1 the JNI method should not be confused, because this method needs to be consistent with the native method

-keepclasseswithmembernames class * {# keep the native method from being confused with native;}

2. The classes used in reflection are not confused (otherwise reflection may have problems)

The classes in Application Mainfest are not confused, so the four components and the subclasses of Android and all the classes under the Framework layer are not confused by default. Custom View will not be confused by default; so many rules that exclude custom View, or four major components that are confused, as posted online, do not need to be added in Android Studio.

4. When interacting with the server, when using GSON, fastjson and other frameworks to parse the server data, the written JSON object classes are not confused, otherwise the JSON cannot be parsed into the corresponding object.

5. When using a third-party open source library or referencing other third-party SDK packages, if there are special requirements, you also need to add the corresponding obfuscation rules in the obfuscation file

6. JS calls that are useful to WebView also need to ensure that the written interface methods are not confused for the same reason as in the * * section.

7The subclass of Creator Parcelable is not confused with the static member variables of Android.os.BadParcelableException, otherwise an Android.os.BadParcelableException exception will be generated.

-keep class * implements Android.os.Parcelable {# keep Parcelable unconfused public static final Android.os.Parcelable$Creator *;}

8. When using the enum type, you need to be careful to avoid the confusion of the following two methods. Because of the particularity of the enum class, the following two methods will be called by reflection, see the second rule.

-keepclassmembers enum * {public static * * [] values (); public static * * valueOf (java.lang.String);} "what are the basic knowledge points of Android"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report