Flutter项目Gradle构建失败,如何将仓库切换为HTTPS访问?
Hey there! I’ve run into this exact issue before when network policies block HTTP access to package repositories. Since you can reach those domains via HTTPS just fine, we just need to update Gradle’s configuration to use HTTPS URLs instead of HTTP ones. Here’s how to do it step by step:
1. Update Project-Level build.gradle
First, open your Flutter project’s root directory and edit the build.gradle file (not the one inside the app folder). Look for the repositories blocks and replace all HTTP URLs with their HTTPS equivalents:
// Before repositories { maven { url 'http://download.flutter.io' } maven { url 'http://dl.google.com/maven2' } jcenter() } // After repositories { maven { url 'https://download.flutter.io' } google() // This uses HTTPS by default, replaces the dl.google.com repo mavenCentral() // Recommended over jcenter, which is no longer maintained }
2. Update Flutter SDK’s Global Gradle Config
If your project relies on the Flutter SDK’s internal Gradle settings, you’ll need to tweak that too:
- Navigate to your Flutter SDK installation directory, then go to
flutter/packages/flutter_tools/gradle/ - Open
flutter.gradleand find therepositoriessection inside thebuildscriptblock - Replace
http://download.flutter.iowithhttps://download.flutter.io
3. Fix Gradle Wrapper Distribution URL
Check if your Gradle wrapper is using an HTTP URL for the Gradle distribution:
- Open
gradle/wrapper/gradle-wrapper.propertiesin your project - Look for the
distributionUrlline, and changehttp://tohttps://:distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
4. Clear Gradle Caches
Old cached HTTP resources might still cause issues, so let’s clear them:
- Run
flutter cleanin your project’s root directory via command line - Delete the Gradle cache folder (Windows path:
C:\Users\[Your Username]\.gradle\caches) - Or run
gradlew cleanBuildCachein your project’s root to clear build-specific caches
After making these changes, restart Android Studio, click "Sync Project with Gradle Files", and try running your Flutter app again. This should resolve the HTTP access errors since all repository requests will now use HTTPS.
内容的提问来源于stack exchange,提问作者Zionnite

