在Android Studio中添加阿里云仓库加速下载

使用 GPT-4o 回答,作者 GPT4oMini.app

问题:

在Android Studio Koala中,为了下载插件和依赖包更加流畅,我要增加阿里云仓库地址作为插件和依赖包下载地址,应该在哪个配置文件进行添加(修改build.gradle 还是 settings.gradle.kts),如何修改?

答案:

在Android Studio中,为了使用阿里云的仓库地址来加速插件和依赖包的下载,你需要修改项目的settings.gradle.kts文件和各个模块的build.gradle文件。

修改settings.gradle.kts

settings.gradle.kts文件中,添加阿里云的仓库地址到插件管理的repositories中:

pluginManagement {
    repositories {
        gradlePluginPortal()
        maven {
            url = uri("https://maven.aliyun.com/repository/gradle-plugin")
        }
        mavenCentral()
        google()
    }
}

修改build.gradle

在项目根目录下的build.gradle文件中,添加阿里云的仓库地址到repositories中:

allprojects {
    repositories {
        google()
        mavenCentral()
        maven {
            url "https://maven.aliyun.com/repository/public"
        }
        maven {
            url "https://maven.aliyun.com/repository/google"
        }
        maven {
            url "https://maven.aliyun.com/repository/jcenter"
        }
    }
}

修改模块级别的build.gradle

在每个模块(如app模块)的build.gradle文件中,确保repositories中包含阿里云的仓库地址:

repositories {
    google()
    mavenCentral()
    maven {
        url "https://maven.aliyun.com/repository/public"
    }
    maven {
        url "https://maven.aliyun.com/repository/google"
    }
    maven {
        url "https://maven.aliyun.com/repository/jcenter"
    }
}

完整示例

settings.gradle.kts

pluginManagement {
    repositories {
        gradlePluginPortal()
        maven {
            url = uri("https://maven.aliyun.com/repository/gradle-plugin")
        }
        mavenCentral()
        google()
    }
}

根目录下的build.gradle

buildscript {
    repositories {
        google()
        mavenCentral()
        maven {
            url "https://maven.aliyun.com/repository/public"
        }
        maven {
            url "https://maven.aliyun.com/repository/google"
        }
        maven {
            url "https://maven.aliyun.com/repository/jcenter"
        }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:7.0.2"
        // 其他依赖
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        maven {
            url "https://maven.aliyun.com/repository/public"
        }
        maven {
            url "https://maven.aliyun.com/repository/google"
        }
        maven {
            url "https://maven.aliyun.com/repository/jcenter"
        }
    }
}

模块级别的build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 30
    defaultConfig {
        applicationId "com.example.myapp"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.3.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}

repositories {
    google()
    mavenCentral()
    maven {
        url "https://maven.aliyun.com/repository/public"
    }
    maven {
        url "https://maven.aliyun.com/repository/google"
    }
    maven {
        url "https://maven.aliyun.com/repository/jcenter"
    }
}

通过以上配置,你就可以使用阿里云的仓库地址来加速插件和依赖包的下载了。