AndroidStudio中Project下的build.gradle没有buildscript和allprojects了

前言

最近想使用一个开源框架AndServer,根据文档说明,首先需要在Project下的build.gradle做如下配置:

buildscript {
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.yanzhenjie.andserver:plugin:2.1.10'
        ...
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}
...

但是我的项目下的build.gradle长得是这样子的:

plugins {
    id 'com.android.application' version '7.1.2' apply false
    id 'com.android.library' version '7.1.2' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

纳尼?怎么buildscript和allprojects闭包都不见了?那我还怎么配置?
查阅了一下官方文档,原来是2022年1月的Gradle7.1.0版本做的更新导致的,如下图:
在这里插入图片描述
如果你用的是小蜜蜂版本的Android Studio,创建的的项目,默认就是没有buildscript和allprojects的,之前的仓库配置被挪到Project下的setting.gradle里面了。

解决办法

说了那么多,好像也没提到我所要关心的内容:要怎么配置“classpath ‘com.yanzhenjie.andserver:plugin:2.1.10’”?
其实依旧可以放在Project下的build.gradle里面,完整的配置如下:

buildscript {
    dependencies {
        classpath 'com.yanzhenjie.andserver:plugin:2.1.10'
    }
}

plugins {
    id 'com.android.application' version '7.1.2' apply false
    id 'com.android.library' version '7.1.2' apply false
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

注意buildscript闭包需要放在plugins闭包前面,不然会报错。好了,这下已经达成了我的目的。

补充

我的module下的build.gradle如下:

plugins {
    id 'com.android.application'
}
apply plugin: 'com.yanzhenjie.andserver'
android {
    compileSdk 31

    defaultConfig {
        applicationId "com.example.lyyserverdemo"
        minSdk 21
        targetSdk 31
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.3.0'
    implementation 'com.google.android.material:material:1.4.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'com.yanzhenjie.andserver:api:2.1.10'
    annotationProcessor 'com.yanzhenjie.andserver:processor:2.1.10'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

就是在顶部加了apply plugin: ‘com.yanzhenjie.andserver’,以及在底部的依赖里加了
implementation ‘com.yanzhenjie.andserver:api:2.1.10’
annotationProcessor ‘com.yanzhenjie.andserver:processor:2.1.10’
这是根据AndServer文档配置的,接下来就可以使用这个框架了。

  • 31
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
根据您提供的信息,您似乎在引用两个不同的引用和。 如果您使用的是小蜜蜂版本的Android Studio,并且创建的项目没有buildscript和allprojects,那么默认情况下,这些配置将被移动到Project下的settings.gradle文件。因此,您可以在settings.gradle文件找到与您的引用所描述的内容相对应的配置。 另一方面,如果您正在查看的是最新版本的Android Studio(不使用小蜜蜂),那么build.gradle文件应该是类似的。请确保您的build.gradle文件与引用的代码片段完全匹配。如果您遇到任何问题,请检查build.gradle文件的任何拼写错误或配置错误。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [最新版AS配置Project下的build.gradle的classpath](https://blog.csdn.net/qq_36388797/article/details/128454785)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [AndroidStudioProject下的build.gradle没有buildscript和allprojects了](https://blog.csdn.net/Deep_rooted/article/details/124764731)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值