# Gradle 常见问题

## 1. Gradle 支持版本

Android 无埋点 SDK 支持 `com.android.tools.build:gradle` **2.3.3** 及其以上版本，推荐版本是 **3.2.1**。下文将简称 `com.android.tools.build:gradle`为 gradle。

无埋点 SDK 随着 gradle 的升级也在逐步兼容，请参考以下表格更新 SDK：

| 无埋点SDK版本 | 说明                                                                                                                                                                                                                                                                                                                                                               |
| -------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| 2.8.4+   | <p></p><ul><li>SDK 2.8.4 及其以上版本支持 gradle 3.5.0；</li><li>新增在编译期间校验用户使用的 SDK 是否为稳定 SDK 版本，如果使用有问题的版本将主动在编译时提示用户升级 SDK，并抛出异常提示，拒绝编译。</li></ul>                                                                                                                                                                                                                      |
| 2.7.8    | <p>增加接口 <code>setAndroidIdEnable</code> , <code>setImeiEnable</code>, <code>setGoogleAdIdEnable</code> 为海外上架应用涉及采集用户 <code>androidId</code>, <code>imei</code>, <code>googleAdId</code> 隐私数据的开关支持。</p><p><strong>详见</strong><a href="../android-sdk-api/gradle-api"><strong>Gradle编译时配置API</strong></a></p><p><strong>注意，以上接口不支持 gradle 3.0.x 以下版本</strong>。</p> |

## **2. 升级 2.8.4 编译报错**

升级无埋点 SDK 2.8.4如果报错如下：

> Unable to load class **'org.apache.http.impl.client.CloseableHttpClient'**. Possible causes for this unexpected error include: Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.) Re-download dependencies and sync project (requires network)
>
> The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem. Stop Gradle build processes (requires restart)
>
> Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.
>
> In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

此报错原因是在 SDK 2.8.4 时新增在编译期间校验用户使用的 SDK 是否为稳定 SDK 版本，如果使用有问题的版本将主动在编译时提示用户升级 SDK，并抛出异常提示，拒绝编译功能，此部分代码逻辑触发了Proguard 的 Bug ，导致报错。建议解决方案有两种：

1. 升级 gradle 为 3.2.1 及其以上版本；
2. 在项目级别 build.gradle 中添加以下依赖即可：

```
classpath "org.apache.httpcomponents:httpclient:4.5.10"
```

示例代码：

```java
dependencies {
    //使用 gradle 3.2.1 以下并且 SDK 2.8.4 版本会触发此问题
    classpath 'com.android.tools.build:gradle:3.1.4'
    classpath 'com.growingio.android:vds-gradle-plugin:autotrack-2.8.4'
    //添加以下依赖能够解决，或者升级 gradle
    classpath "org.apache.httpcomponents:httpclient:4.5.10"
}
```

## 3. 使用Jack注意

依据Android Studio[官网说明](https://developer.android.com/studio/write/java8-support#migrate)，SDK不支持 Jack 编译器，请确认移除 jackOptions 代码块。

```java
android {
    ...
    defaultConfig {
        ...
        // Remove this block. 删掉这里！！！
        jackOptions {
            enabled true
            ...
        }
    }
    // Keep the following configuration in order to target Java 8.
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}
```

{% hint style="danger" %}
如果您未移除，集成SDK后App将崩溃（Crash）。
{% endhint %}
