AS导入项目
- 打开一个正常的项目
- 打开待导入的项目,一般都会报错
- 将正常项目的build.gradle(project)复制到新项目,目的是同步gradle plugin
- 将正常项目的gradle-wrapper.properties中的distributionUrl复制到新项目,目的是同步gradle
Gradle plugin和Gradle版本对应关系
随时可能更新,更具体的可看官网

Gradle plugin和Gradle版本可在File——Project Structure中查看,需要保证上面的最低版本对应

Gradle下载代理配置
修改gradle-wrapper.properties中distributionUrl的地址
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
distributionUrl=https://mirrors.cloud.tencent.com/gradle/gradle-8.9-bin.zip
修改build.gradle(.kts)或settings.gradle(.kts)(不同版本AS位置可能不同)
- 在pluginManagement、dependencyResolutionManagement或buildscript、allprojects代码块里添加如下
- 简单来说,就是google()/mavenCentral()它们在哪,就加到它们前面
- 仓库地址可能会变,可以查看https://maven.aliyun.com给出的仓库
repositories {
maven { url 'https://maven.aliyun.com/repository/central' }
maven { url 'https://maven.aliyun.com/repository/public' }
maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
maven { url 'https://maven.aliyun.com/repository/apache-snapshots' }
}

AS导入依赖
在http://www.mvnrepository.com找到要导入的模块,选择Gradle

AS导入Jar
将Jar复制到libs,然后右键选择Add As Library

报错Could not create parent directory for lock file
检查gradle user home是否为默认目录

报错Could not find com.android.tools.build:gradle:8.0.0.

按照提示点击Add google Maven repository and sync project
报错Could not resolve all files for configuration ‘:classpath’.
A problem occurred configuring root project 'OTA'.
> Could not resolve all files for configuration ':classpath'.
> Could not resolve com.android.tools.build:gradle:8.0.0.
Required by:
project :
> No matching variant of com.android.tools.build:gradle:8.0.0 was found. The consumer was configured to find a library for use during runtime, compatible with Java 8, packaged as a jar, and its dependencies declared externally, as well as attribute 'org.gradle.plugin.api-version' with value '8.0' but:
- Variant 'apiElements' capability com.android.tools.build:gradle:8.0.0 declares a library, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
- Variant 'javadocElements' capability com.android.tools.build:gradle:8.0.0 declares a component for use during runtime, and its dependencies declared externally:
- Incompatible because this component declares documentation and the consumer needed a library
- Other compatible attributes:
- Doesn't say anything about its target Java version (required compatibility with Java 8)
- Doesn't say anything about its elements (required them packaged as a jar)
- Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
- Variant 'runtimeElements' capability com.android.tools.build:gradle:8.0.0 declares a library for use during runtime, packaged as a jar, and its dependencies declared externally:
- Incompatible because this component declares a component, compatible with Java 11 and the consumer needed a component, compatible with Java 8
- Other compatible attribute:
- Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
- Variant 'sourcesElements' capability com.android.tools.build:gradle:8.0.0 declares a component for use during runtime, and its dependencies declared externally:
- Incompatible because this component declares documentation and the consumer needed a library
- Other compatible attributes:
- Doesn't say anything about its target Java version (required compatibility with Java 8)
- Doesn't say anything about its elements (required them packaged as a jar)
- Doesn't say anything about org.gradle.plugin.api-version (required '8.0')
上面报错关键为组件在JDK11编译,当前环境为Java8
Incompatible because this component declares a component for use during compile-time, compatible with Java 11 and the consumer needed a component for use during runtime, compatible with Java 8
点击File——Project Structure

点击Gradle Settings,选择大于11的SDK(我选择的是17)

报错Could not find method compile() for arguments
修改build.gralde(app),将dependencies中的compile改为implementation
报错Out of memory: Java heap space
help-Change Memory Settings修改堆的大小

在gradle-wrapper.properties设置堆和元空间大小
org.gradle.jvmargs=-Xmx8192m -XX:MaxPermSize=1024m
怎么改都溢出,最后发现是导入的AOSP的Framework.jar中的java文件参与了运行,将implementation改为compileOnly
compileOnly files('libs\\classes.jar')
报错Google Play requires that apps target API level XX or higher.
谷歌商店上架应用API不小于XX,可在build.gralde(app)的andoird配置里添加lintOptions取消检查
android {
......
lintOptions{
checkReleaseBuilds false
abortOnError false
}
......
}
报错-source 1.7 中不支持 lambda 表达式
确保File-Project Structure中的JDK大于1.8

build.gradle添加如下设置
android {
......
compileOptions{
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
import或open选择目录时显示Loading或点击刷新加载不出新文件夹
点击File-Repair IDE按照步骤修复IDE
你的主机中的软件中止了一个已建立的连接
下载Gradle失败,需要切换到可以下载的网络
Namespace not specified. Specify a namespace in the module’s build file
setting.gradle中include指定的每一个模块的build.gradle都要添加namespace
android {
namespace "包名"
......
}
No cached version available for offline mode
Settings-Builds, Execution, Deployment-Gradle下面将Offline work取消

如果上面没有,则可能是Compiler参数添加了offline

解决AndroidStudio中Gradle导入项目及兼容性问题指南
本文详细介绍了在AndroidStudio中处理Gradle项目导入时遇到的各种错误,包括版本不匹配、库兼容性、内存设置、API级别要求以及源代码兼容性问题,并提供了相应的解决方案。
2148

被折叠的 条评论
为什么被折叠?



