You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在新Android Studio中运行旧Firebase示例项目?

新Android Studio运行旧Gradle项目的解决步骤

针对你打开Udacity旧Firebase项目时的Gradle同步失败问题,按以下步骤逐步修复,比直接让AS自动升级更稳妥:

核心策略

优先手动调整仓库、Gradle插件版本,再逐步升级SDK和依赖,避免一次性升级带来的兼容性冲突。


1. 修改项目根目录的build.gradle

旧项目依赖的jcenter()已停止维护,替换为官方推荐的google()和mavenCentral()仓库,同时升级Gradle插件到兼容新AS的稳定版本:

修改后的根目录build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        google()
        mavenCentral()
        mavenLocal()
    }
    dependencies {
        // 选兼容新AS的稳定Gradle插件版本,3.6.4适配绝大多数新AS版本
        classpath 'com.android.tools.build:gradle:3.6.4'
    }
}

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

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

2. 调整Gradle Wrapper版本

找到项目根目录的gradle/wrapper/gradle-wrapper.properties,修改distributionUrl为对应Gradle版本(Gradle插件3.6.4对应Gradle 5.6.4):

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip

3. 修改app模块的build.gradle

3.1 替换插件与SDK版本

把旧的support库换成AndroidX(官方已废弃support库),升级compileSdk和targetSdk到稳定版本(比如33):

3.2 升级依赖并替换关键字

将compile改为implementation、testCompile改为testImplementation,替换support库为AndroidX对应依赖,升级Glide到兼容版本:

修改后的app模块build.gradle:

plugins {
    id 'com.android.application'
}

repositories {
    mavenLocal()
    flatDir {
        dirs 'libs'
    }
    google()
    mavenCentral()
}

android {
    compileSdk 33
    buildToolsVersion "33.0.2"

    defaultConfig {
        applicationId "com.google.firebase.udacity.friendlychat"
        minSdk 16
        targetSdk 33
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE-FIREBASE.txt'
        exclude 'META-INF/NOTICE'
    }
    // 启用AndroidX命名空间
    namespace 'com.google.firebase.udacity.friendlychat'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    testImplementation 'junit:junit:4.13.2'

    // AndroidX替代旧support库
    implementation 'com.google.android.material:material:1.9.0'
    implementation 'androidx.appcompat:appcompat:1.6.1'

    // 升级Glide到兼容AndroidX的版本
    implementation 'com.github.bumptech.glide:glide:4.15.1'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.15.1'
}

4. 同步与修复剩余问题

  1. 点击AS右上角的「Sync Project with Gradle Files」按钮同步项目
  2. 如果出现R文件报错,执行「Build -> Clean Project」再「Rebuild Project」
  3. 若布局文件中有旧support控件报错(比如android.support.design.widget.TextInputLayout),替换为AndroidX的com.google.android.material.textfield.TextInputLayout

内容的提问来源于stack exchange,提问作者vico

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 12:30:07