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") }
    }
}

作者:帅得不敢出门

相关推荐
梁同学与Android15 分钟前
Android --- 在AIDL进程间通信中,为什么使用RemoteCallbackList 代替 ArrayList?
android
Frank_HarmonyOS3 小时前
【无标题】Android消息机制
android
凯文的内存5 小时前
Android14 OTA升级速度过慢问题解决方案
android·ota·update engine·系统升级·virtual ab
VinRichard5 小时前
Android 常用三方库
android
Aileen_0v06 小时前
【玩转OCR | 腾讯云智能结构化OCR在图像增强与发票识别中的应用实践】
android·java·人工智能·云计算·ocr·腾讯云·玩转腾讯云ocr
栗子~~9 小时前
idea 8年使用整理
java·ide·intellij-idea
江上清风山间明月9 小时前
Flutter DragTarget拖拽控件详解
android·flutter·ios·拖拽·dragtarget
debug_cat11 小时前
AndroidStudio Ladybug中编译完成apk之后定制名字kts复制到指定目录
android·android studio
lmxnsI13 小时前
后端项目打包发布
ide·python·pycharm
爱学习的Allan13 小时前
使用 pyreqs 快速创建 requirements.txt & PyCharm 中 UnicodeDecodeError 问题
ide·python·pycharm·pip