学习Head First Android第2版:如何在AS3.1用旧Gradle建新项目?
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.propertiesfile and check thedistributionUrlline (e.g.,gradle-4.4-all.zip). - Android Gradle Plugin (AGP) Version: Look in the root
build.gradlefile underdependenciesfor thecom.android.tools.build:gradleentry (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 Versionto match the one used in the companion code (common choices for the 2nd edition are API 27 or 28). - Set the
Minimum SDK Versionto the same value as the sample code to avoid compatibility gaps.
- Set the
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

