You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

学习Head First Android第2版:如何在AS3.1用旧Gradle建新项目?

How to Create a New Android Studio 3.1 Project with an Older Gradle Version

Got it, let's walk through exactly how to set up a new project in Android Studio 3.1 that uses the older Gradle version matching your Head First Android (2nd Edition) companion code. Here's the step-by-step breakdown:

1. First, Identify the Target Gradle & AGP Versions

Before you start, you need to confirm two tightly coupled versions from the companion code:

  • Gradle Distribution Version: Open the code's gradle/wrapper/gradle-wrapper.properties file and check the distributionUrl line (e.g., gradle-4.4-all.zip).
  • Android Gradle Plugin (AGP) Version: Look in the root build.gradle file under dependencies for the com.android.tools.build:gradle entry (e.g., 3.1.4—which is fully compatible with Android Studio 3.1).

2. Create a New Project with Matching SDK Settings

  • Launch Android Studio 3.1 and select "Start a new Android Studio project".
  • Pick a basic template (like Empty Activity) and click through the initial steps until you reach the Configure your project screen.
  • On this screen:
    • Set the Compile SDK Version to match the one used in the companion code (common choices for the 2nd edition are API 27 or 28).
    • Set the Minimum SDK Version to the same value as the sample code to avoid compatibility gaps.

3. Update Gradle Configuration Files

Once the project is created, immediately modify these two critical files to switch to the older Gradle version:

a. Root build.gradle File

Open the root-level build.gradle file and update the AGP dependency to your target version. For example:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4' // Replace with your target AGP version
    }
}

b. gradle-wrapper.properties File

Navigate to gradle/wrapper/gradle-wrapper.properties and update the distributionUrl to point to your target Gradle distribution. For example:

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip // Replace with your target Gradle version

4. Sync the Project

Click the Sync Project with Gradle Files button (the elephant icon in the top toolbar) and wait for Android Studio to download the older Gradle version and sync all dependencies. If you run into download issues, you can manually download the Gradle zip file and place it in your local Gradle cache directory (typically ~/.gradle/wrapper/dists/ on macOS/Linux or C:\Users\<YourName>\.gradle\wrapper\dists\ on Windows).

5. Verify the Configuration

After sync completes, confirm everything is set up correctly:

  • Go to File > Project Structure > Project—check that the Gradle version and Android Plugin Version match your target values.
  • Try building and running the project (or import a module from the companion code) to ensure there are no build errors.

A quick note: Android Studio 3.1 fully supports older Gradle/AGP versions from that era, but you might encounter minor warnings about deprecated APIs in the sample code. These are usually safe to ignore, or you can make small adjustments if needed to resolve them.

内容的提问来源于stack exchange,提问作者timepass

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 07:04:47