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

本地AGP Jar未找到:仅用本地工具构建Android应用遇阻求助

解决方案

核心问题说明

com.android.application插件确实包含在com.android.tools.build:gradle(即AGP)包内,你遇到的问题主要来自三个点:AGP与Gradle版本不兼容、配置方式冲突、旧版AGP不支持高版本SDK,以及本地依赖缺失。


步骤1:匹配AGP与Gradle版本

AGP 2.7.1是2017年的旧版本,必须搭配Gradle 4.1使用。修改gradle/wrapper/gradle-wrapper.properties:

distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip

步骤2:修正Gradle配置(二选一)

方案A:使用Buildscript方式(更适合旧版AGP)

项目级build.gradle
buildscript {
    repositories {
        flatDir {
            dirs("gradle") // 本地Jar所在目录
        }
    }
    dependencies {
        // 引入本地AGP Jar
        classpath(files("gradle/com.android.tools.build.gradle-2.7.1.jar"))
        // 若用Kotlin,需同时引入本地Kotlin Gradle插件Jar(版本需匹配AGP)
        classpath(files("gradle/kotlin-gradle-plugin-1.3.72.jar"))
    }
}

allprojects {
    repositories {
        flatDir {
            dirs("gradle")
        }
    }
}

tasks.register<Delete>("clean") {
    delete(rootProject.buildDir)
}
Settings.gradle
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        flatDir {
            dirs("gradle")
        }
    }
}

rootProject.name = "Debugee"
include ':app'
应用级build.gradle

注意:AGP 2.7.1最高仅支持compileSdk 26,必须降低SDK版本,同时移除alias改用直接引用插件ID:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

android {
    namespace = "com.example.debugee"
    compileSdk = 26
    buildToolsVersion "26.0.3"

    defaultConfig {
        applicationId = "com.example.debugee"
        minSdk = 24
        targetSdk = 26
        versionCode = 1
        versionName = "1.0"

        testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables {
            useSupportLibrary = true
        }
    }
    // 其他原有配置...
}

dependencies {
    // 本地引入所有依赖Jar(如appcompat、kotlin-stdlib等,版本需匹配SDK)
    implementation files('gradle/appcompat-v7-26.1.0.jar')
    implementation files('gradle/kotlin-stdlib-jdk7-1.3.72.jar')
    // 其他依赖...
}

方案B:使用PluginManagement方式

Settings.gradle

修改resolutionStrategy直接指向本地Jar,而非尝试从仓库拉取模块:

pluginManagement {
    repositories {
        flatDir {
            dirs("gradle")
        }
    }
  
    resolutionStrategy {
        eachPlugin {
            if (requested.id.id == "com.android.application") {
                useClasspath(files("gradle/com.android.tools.build.gradle-2.7.1.jar"))
            }
            if (requested.id.id == "org.jetbrains.kotlin.android") {
                useClasspath(files("gradle/kotlin-gradle-plugin-1.3.72.jar"))
            }
        }
    }
}

dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        flatDir {
            dirs("gradle")
        }
    }
}

rootProject.name = "Debugee"
include ':app'
应用级build.gradle
plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

// 后续android、dependencies配置同方案A

步骤3:确保本地依赖完整

本地构建需要手动下载所有依赖的Jar包,包括:

  • AGP核心Jar
  • Kotlin Gradle插件Jar(若用Kotlin)
  • Android Support库/Jetpack库Jar(版本需匹配compileSdk 26)
  • 其他第三方依赖Jar

若Jar损坏或缺失,会导致插件加载失败,建议重新下载对应版本的完整Jar文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 16:23:11