Android Studio加载旧的安卓工程项目报错处理

文章目录

新版本的Android Studio加载旧的安卓工程项目,会报各种问题

  1. Invalid Gradle JDK configuration found
  2. NDK not configured. Download it with SDK manager. Preferred NDK version is
  3. CMake '3.10.2' was not found in PATH or by cmake.dir property
  4. com.intellij.openapi.externalSystem.model.ExternalSystemException
  5. gradle下载不了或time out

Invalid Gradle JDK configuration found

点击Select the Gradle JDK location

或者点击Android Studio-->Settings找到下面选项

Build, Execution, Deployment | Build Tools | Gradle

在Gradle JDK这行,选择系统中已经有的适合的。

NDK not configured

NDK not configured. Download it with SDK manager. Preferred NDK version is '21.1.6352462'

这里会提示未配置NDK, 且提示中会建议要配置哪个版本号,比如上图中提示的ndkVersion '21.1.6352462'

那么把这个建议的版本号替换到app/build.gradle中旧的版本号就行了。

修正:

app/build.gradle

patch 复制代码
diff --git a/app/build.gradle b/app/build.gradle
index 7daf6a1..a29085d 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -43,7 +43,7 @@ android {
         sourceCompatibility JavaVersion.VERSION_1_8
         targetCompatibility JavaVersion.VERSION_1_8
     }
-    ndkVersion '21.3.6528147'
+    ndkVersion '21.1.6352462'
 }

CMake '3.10.2' was not found

错误

CMake '3.10.2' was not found in PATH or by cmake.dir property.

  1. 可能是系统中没有安装CMake
  2. 也可能是系统中的Cmake版本号不对应,不是配置要求的3.10.2

安装cmake

Android Studio--> Tools-- >SDK Manager

点击选中CMake,就可以自动安装到 Android SDK的路径下

适配cmake版本号

安装成功后查看下版本号:

cmake --version指令查看,

如果安装后提示not found: cmake,则要配置下环境变量PATH

Mac中修改~/.bash_profile

把刚刚安装的cmake的bin路径加到PATH中

bash 复制代码
export PATH=$PATH:/Users/temp/Library/Android/sdk/cmake/3.30.5/bin

再次查看,就可以在命令行看到cmake指令及版本了

app/build.gradle中cmake的版本号与系统中安装的cmake版本如果不一样,也会报错,需要修改

patch 复制代码
diff --git a/app/build.gradle b/app/build.gradle
index 7daf6a1..a714679 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -36,14 +36,14 @@ android {
     externalNativeBuild {
         cmake {
             path "src/main/cpp/CMakeLists.txt"
-            version "3.10.2"
+            version "3.30.5"
         }
     }

com.intellij.openapi.externalSystem.model.ExternalSystemException

com.intellij.openapi.externalSystem.model.ExternalSystemException: 
	at com.intellij.gradle.toolingExtension.impl.modelAction.GradleModelFetchAction.addModels(GradleModelFetchAction.java:183)
	at com.intellij.gradle.toolingExtension.impl.modelAction.GradleModelFetchAction.lambda$execute$1(GradleModelFetchAction.java:73)
	at com.intellij.gradle.toolingExtension.impl.telemetry.GradleOpenTelemetry.lambda$runWithSpan$1(GradleOpenTelemetry.java:87)
	at com.intellij.gradle.toolingExtension.impl.telemetry.GradleOpenTelemetry.callWithSpan(GradleOpenTelemetry.java:73)
	at com.intellij.gradle.toolingExtension.impl.telemetry.GradleOpenTelemetry.callWithSpan(GradleOpenTelemetry.java:61)
	at com.intellij.gradle.toolingExtension.impl.telemetry.GradleOpenTelemetry.runWithSpan(GradleOpenTelemetry.java:86)
	at com.intellij.gradle.toolingExtension.impl.modelAction.GradleModelFetchAction.execute(GradleModelFetchAction.java:73)
	at org.jetbrains.plugins.gradle.model.ProjectImportAction.doExecute(ProjectImportAction.java:170)

build.gradle中配置的gradle版本号过低,在高版本Android Studio中会报错

修复:

修改build.gradle中版本号为新版本

patch 复制代码
diff --git a/build.gradle b/build.gradle
index 15087da..a5a4b7d 100644
--- a/build.gradle
+++ b/build.gradle
     dependencies {
-        classpath "com.android.tools.build:gradle:4.1.0"
+        classpath "com.android.tools.build:gradle:7.1.1"

gradle版本过低或下载不了

gradle版本过低或下载超时提示url不存在的,可以通过修改gradle-wrapper.properties中的url来解决,把版本号改成较新的,另外把url中域名改成国内的域名,比如阿里的。

patch 复制代码
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index bc2dd44..292d061 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
-#Wed Nov 04 15:11:01 CST 2020
+#Wed Nov 06 11:06:30 CST 2024
 distributionBase=GRADLE_USER_HOME
 distributionPath=wrapper/dists
+distributionUrl=https\://mirrors.aliyun.com/macports/distfiles/gradle//gradle-7.2-bin.zip
 zipStoreBase=GRADLE_USER_HOME
 zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip

下载gradle与依赖库超时

Failed to resolve: com.blankj:utilcode:1.29.0

替换gradle国内源

修改grale/gradle-wrapper.properties

把distributionUrl=https://services.gradle.org/distributions/gradle-7.2-bin.zip

修改为

distributionUrl=https://mirrors.aliyun.com/macports/distfiles/gradle//gradle-7.2-bin.zip

替换Maven 仓库的镜像为国内源

修改build.gradle

 buildscript {
     repositories {
        google()
        jcenter()
     }
 }

 allprojects {
     repositories {
        google()
        jcenter()
     }
 }

改为以下内容:

buildscript {
    repositories {
        maven { url = uri("https://maven.aliyun.com/repository/releases") }
        maven { url = uri("https://maven.aliyun.com/repository/google") }
        maven { url = uri("https://maven.aliyun.com/repository/central") }
        maven { url = uri("https://maven.aliyun.com/repository/gradle-plugin") }
        maven { url = uri("https://maven.aliyun.com/repository/public") }
    }
}

allprojects {
    repositories {
        maven { url = uri("https://maven.aliyun.com/repository/releases") }
        maven { url = uri("https://maven.aliyun.com/repository/google") }
        maven { url = uri("https://maven.aliyun.com/repository/central") }
        maven { url = uri("https://maven.aliyun.com/repository/gradle-plugin") }
        maven { url = uri("https://maven.aliyun.com/repository/public") }
    }
}

作者:帅得不敢出门

相关推荐
Eastsea.Chen2 小时前
MTK Android12 user版本MtkLogger
android·framework
小江村儿的文杰8 小时前
XCode Build时遇到 .entitlements could not be opened 的问题
ide·macos·ue4·xcode
为什么每天的风都这么大9 小时前
Vscode/Code-server无网环境安装通义灵码
ide·vscode·阿里云·编辑器·ai编程·code-server
长亭外的少年9 小时前
Kotlin 编译失败问题及解决方案:从守护进程到 Gradle 配置
android·开发语言·kotlin
建群新人小猿12 小时前
会员等级经验问题
android·开发语言·前端·javascript·php
1024小神13 小时前
tauri2.0版本开发苹果ios和安卓android应用,环境搭建和最后编译为apk
android·ios·tauri
兰琛13 小时前
20241121 android中树结构列表(使用recyclerView实现)
android·gitee
Y多了个想法14 小时前
RK3568 android11 适配敦泰触摸屏 FocalTech-ft5526
android·rk3568·触摸屏·tp·敦泰·focaltech·ft5526
芝麻团坚果15 小时前
对subprocess启动的子进程使用VSCode python debugger
linux·ide·python·subprocess·vscode debugger
NotesChapter15 小时前
Android吸顶效果,并有着ViewPager左右切换
android