使用Android Studio 4.2.1 版本新建Project时会出现部分小问题,其他版本若出现类似问题可以参考下以下解决方案。
1、部分.jar文件无法下载;
(1)新建项目Project下的build.gradle内容如下
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
mavenCentral()
jcenter() // Warning: this repository is going to shut down soon
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
(2)这里可以修改repositories下的内容,改从阿里云仓库下载
阿里云仓库引导:https://developer.aliyun.com/mvn/guide
注意要将 jcenter()注释掉
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
maven {url 'https://maven.aliyun.com/repository/public/'}
maven {url 'https://maven.aliyun.com/repository/google/'}
maven {url 'https://maven.aliyun.com/repository/central/'}
maven {url 'https://maven.aliyun.com/repository/gradle-plugin/'}
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.2.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
maven {url 'https://maven.aliyun.com/repository/public/'}
maven {url 'https://maven.aliyun.com/repository/google/'}
maven {url 'https://maven.aliyun.com/repository/central/'}
maven {url 'https://maven.aliyun.com/repository/gradle-plugin/'}
google()
mavenCentral()
// jcenter() // Warning: this repository is going to shut down soon
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2、经过以上步骤后,运行程序到Android设备上又会出现以下错误
* What went wrong:
Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
> Failed to find Build Tools revision 32.0.0
方案一:
修改app目录下的build.gradle内容,降低build-tools版本
-
修改前:
-
修改后:
方案二:
不修改build-tools版本号
(1)在Build栏拉到最上可以看到以下信息;
(2)这里对比了30.0.3和32.0.0,发现确实是少了一些文件;
(3)将32.0.0文件夹下缺少的文件从30.0.3下拷贝过来即可,主要有两个文件;
- dx.bat
- dx.jar
(4)再次运行程序后,又出现如下错误;
Apps targeting Android 12 and higher are required to specify an explicit value for `android:exported` when the corresponding component has an intent filter defined.
(5)以上问题官方说明为;
更安全的组件导出
如果您的应用以 Android 12 或更高版本为目标平台,且包含使用 intent 过滤器的 activity、服务或广播接收器,您必须为这些应用组件显式声明 android:exported 属性。
警告:如果 activity、服务或广播接收器使用 intent 过滤器,并且未显式声明 android:exported 的值,您的应用将无法在搭载 Android 12 或更高版本的设备上进行安装。
如果应用组件包含 LAUNCHER 类别,请将 android:exported 设置为 true。在大多数其他情况下,请将 android:exported 设置为 false。
(6)在AndroidManifest.xml中设置android:exported的值,因为MainActivity是LAUNCHER类别,所以这里将android:exported 设置为 true。