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

本文介绍了在使用Gradle 7.1.0及以上版本时,如何配置AndServer框架。由于版本更新,`buildscript`和`allprojects`闭包移至`settings.gradle`,但依然可以在`Project`的`build.gradle`中进行配置。只需将`classpath 'com.yanzhenjie.andserver:plugin:2.1.10'`写入`buildscript`块,并确保其位于`plugins`块之前。同时,在模块的`build.gradle`中应用插件并添加相关依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

最近想使用一个开源框架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文档配置的,接下来就可以使用这个框架了。

### Android Studio 项目缺少 `build.gradle` 文件的原因分析 在某些情况下,开发者可能会发现项目的根目录或模块下缺失了重要的构建配置文件——即 `build.gradle` 文件。这可能是由于多种因素引起的: - 版本控制系统操作失误导致重要文件未被提交到仓库中[^1]。 - 自动化脚本误删或是手动清理缓存时不慎移除了必要的构建配置文件[^2]。 ### 解决方案概述 为了恢复丢失的 `build.gradle` 文件并使项目能够正常工作,可以采取以下措施来解决问题: #### 方法一:从版本控制历史记录中找回 如果项目使用 Git 或其他版本控制系统管理源码,则可以从历史记录中检索之前的 `build.gradle` 文件副本。具体做法如下所示: ```bash git checkout HEAD -- path/to/build.gradle ``` 此命令会尝试将指定路径下的 `build.gradle` 文件还原至最近一次提交的状态。 #### 方法二:创建新的 `build.gradle` 文件模板 当无法通过上述方式获取旧版文件时,可以根据官方文档中的指导重新编写一份标准格式的 `build.gradle` 文件。对于大多数 Android 应用程序而言,其结构通常类似于下面的例子: ```groovy // Project-level build.gradle plugins { id 'com.android.application' version '7.0.0' apply false } allprojects { repositories { google() mavenCentral() } } ``` 以及应用级别的 `build.gradle`: ```groovy // Module (app)-level build.gradle plugins { id 'com.android.application' } android { compileSdkVersion 30 defaultConfig { applicationId "com.example.myfirstapplication" minSdkVersion 21 targetSdkVersion 30 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.1' implementation 'com.google.android.material:material:1.4.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.0' testImplementation 'junit:junit:4.+' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' } ``` 请注意替换其中的应用 ID SDK 版本号等参数以匹配实际需求。 #### 方法三:同步 Gradle 配置 完成新 `build.gradle` 文件的创建之后,应该立即点击工具栏上的 Sync Now 按钮让 IDE 加载最新的设置信息。这样不仅可以验证语法正确性,还能自动下载所需的库依赖关系。 ### 后续建议 为了避免将来再次发生类似的意外情况,强烈推荐定期备份项目数据,并确保所有更改都已妥善保存于版本控制系统之中。此外,保持良好的编码习惯也有助于减少人为错误的发生几率。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值