# release屏蔽Log代码

## 愿望

在RELEASE包中,代码中的指写的Log.v() Log.d() Log.i() Log.w() Log.e() 都优化删除掉.不打包进软件

## 原理

通过proguard删除各种日志输出代码。然后编译出apk时，将会删除掉日志代码。

通过配置proguard，将类android.util.Log的方法给置为无效代码。（proguard是一个代码优化的工具，也可以混淆代码）

### assumenosideeffects

assumenosideeffects，assume no side effects；假定无效

assumenosideeffects的官方说明：

> In the optimization step, ProGuard will then remove calls to such methods, if it can determine that the return values aren't used.ProGuard will analyze your program code to find such methods automatically.It will not analyze library code, for which this option can therefore be useful.
>
> In general, making assumptions can be dangerous; you can easily break the processed code. Only use this option if you know what you're doing!

proguard添加以下配置：

-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(...); }

使用此配置要注意-dontoptimize不能有。

## 方法

1. 打开项目build.gradle

   ```
   android.buildTypes.release里
   >minifyEnabled true
   >proguardFiles getDefaultProguardFile('**proguard-android-optimize.txt**'), 'proguard-rules.pro'
   或 > proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
   ```

**proguard-android-optimize.txt 为 sdk>tools>proguard里的默认文件**

1. proguard-android.txt中添加

   ```
   -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(...);
   }
   ```

   且配置里不能有-dontoptimize。
2. 打包运行看日志

## 效果检验

1. 使用AS打开项目的app\build\outputs\apk的文件,解压classes.dex
2. 使用工具dex2jar转成jar文件
3. 再使用jd-gui查看jar的反编译代码,可以看到程序中的Log代码都消失了.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://jiek.gitbook.io/android/code/releaseping-bi-log-dai-ma.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
