Android studio升级到2021、gradle使用7+ 版本、切换至Androidx出现的问题

1、错误:

DSL element ‘dexOptions’ is obsolete and should be removed.
It will be removed in version 8.0 of the Android Gradle plugin.
Using it has no effect, and the AndroidGradle plugin optimizes dexing automatically.
Affected Modules: app

报上述错误需要将如下配置移除

//设置虚拟机堆内存空间大小,避免在编译期间OOM
dexOptions {
// INFO: DSL element ‘DexOptions.incremental’ is obsolete and will be removed at the end of 2018.
// Affected Modules: app
// incremental true
javaMaxHeapSize “4g”
}

2、错误

Failed to resolve: com.github.chrisbanes:PhotoView:latest.release.here
Show in Project Structure dialog
Show Details
Affected Modules: common

gradle7 以上版本配置maven 库。报上述错误需要如下解决

在项目根路径下的build.gradle中配置:

buildscript {
    
    
    repositories {
    
    
        mavenCentral()
        google()
        //添加阿里云镜像,不添加无法拉取包
        maven {
    
     url "https://maven.aliyun.com/repository/public" }
        maven {
    
     url "https://maven.aliyun.com/mvn/view" }
        maven {
    
     url "https://jitpack.io" }

    }
    dependencies {
    
    
        classpath 'com.android.tools.build:gradle:7.2.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

在setting.gradle配置文件中配置:

dependencyResolutionManagement {
    
    
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
    
    
        google()
        mavenCentral()
        maven {
    
     url "https://maven.aliyun.com/repository/public" }
        maven {
    
     url "https://maven.aliyun.com/mvn/view" }
        maven {
    
     url "https://jitpack.io" }

    }
}

3、无法找到主模块的R文件

我的环境的中的gradle的版本是7+ ,在主模块依赖子模块后,发现主模块使用R.id.tv_back时无法找到子模块的r资源,也就是说我并没有发现R资源文件合并到主模块中,也无法在build的生成目录下找到r文件。

这个问题没有找到真正的原因,只是听说,高版本的gradle 在编译时之间将资源文件id值替换成了常量,但是R文件肯定是生成了的,只是没找到。这个具体还要进一步验证一下。

以下是截取其他项目的(gradle 4+)
在这里插入图片描述
但是在本项目中却没找到。不知道为什么,但是还能用。

4、将butternife进行升级后,如何使用

我将butternife 升级到10.0.0 了,是兼容adroidx 的。具体使用方式网上例子很多。

5、R资源没有合并

主模块依赖子模块,子模块R资源并没有合并到主模块的R中,导致主R无法找到子模块下的R资源

原因分析:

发现多个模块的资源文件id无法合并到主模块中。而是每个模块自己保持自己的。
后来研究发现是:
android.nonTransitiveRClass=true 搞得鬼。
该字段描述位于 工程/ gradle.properties下,新建工程时默认是开的,需要自己主动关闭。

该字段描述:
非传递性 R 类
启用非传递性 R 类 (non-transitive R-class) 后,您应用中的 R 类将只会包含在子项目中声明的资源,依赖项中的资源会被排除在外。这样一来,子项目中的 R 类大小将会显著减少。

解决:

6、报错

Caused by: com.android.tools.build.jetifier.processor.transform.bytecode.AmbiguousStringJetifierException: The given artifact contains a string literal with a package reference ‘android.support.v4.content’ that cannot be safely rewritten. Libraries using reflection such as annotation processors need to be updated manually to add support for androidx.

问题出现的原因:

产生这个错误的原因是由于项目引用了AndroidX的依赖包,但是下面butterknife这两个依赖的内部是引用之前的 support 包实现的,因此产生矛盾

解决:

将butternife进行升级到10.0.0 即可

7、报错

Manifest merger failed : android:exported needs to be explicitly specified for element <activity#com.cop.ronghw.app.ui.main.activity.SplashActivity>. 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. See https://developer.android.com/guide/topics/manifest/activity-element#exported for details.
问题翻译:
针对Android 12及更高版本的应用,当相应的组件定义了意图过滤器时,需要为Android:exported指定显式值。详情请参见https://developer.android.google.cn/guide/topics/manifest/activity-element#exported
解决:
在当前的activity 中添加 android:export = true
在这里插入图片描述

8、报错:

Android resource linking failed
ERROR:/Users/senda/Desktop/hzs2/hzsup/android相关/andriodxcop/app/build/intermediates/incremental/debug/mergeDebugResources/merged.dir/values-night-v8/values-night-v8.xml:5: AAPT: error: resource color/purple_200 (aka com.cop.ronghw.app:color/purple_200) not found.

ERROR:/Users/senda/Desktop/hzs2/hzsup/android相关/andriodxcop/app/build/intermediates/incremental/debug/mergeDebugResources/merged.dir/values-night-v8/values-night-v8.xml:6: AAPT: error: resource color/purple_700 (aka com.cop.ronghw.app:color/purple_700) not found.

ERROR:/Users/senda/Desktop/hzs2/hzsup/android相关/andriodxcop/app/build/intermediates/incremental/debug/mergeDebugResources/merged.dir/values-night-v8/values-night-v8.xml:9: AAPT: error: resource color/teal_200 (aka com.cop.ronghw.app:color/teal_200) not found.

ERROR:/Users/senda/Desktop/hzs2/hzsup/android相关/andriodxcop/app/build/intermediates/incremental/debug/mergeDebugResources/merged.dir/values-night-v8/values-night-v8.xml:10: AAPT: error: resource color/teal_200 (aka com.cop.ronghw.app:color/teal_200) not found.

解决:这里是我项目中的colors.xml 中没有配置color 相关的资源数据,这是纯xml报错了,没改正过来。

9、报错

The error I am getting is : "The color “colorRed” in values has no declaration in the base values folder; this can lead to crashes when the resource is queried in a configuration that does not match this qualifier less… "

解决:在Android studio 中操作:

Invalidate Cache and Restart

如下:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/superzhang6666/article/details/127490954