Android Studio升级3.2以后 Androidx 异常总结

栏目: 编程工具 · Android · 发布时间: 5年前

内容简介:错误日志1错误日志2错误日志3

一.Android resource linking failed

错误日志1

Android resource linking failed
Output:  error: resource style/Theme.AppCompat.Light.DarkActionBar (aka com.hp.sortfilebysize:style/Theme.AppCompat.Light.DarkActionBar) not found.
E:\ASWorkSpace\SortFileBySize\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:9: error: style attribute 'attr/colorPrimary (aka com.hp.sortfilebysize:attr/colorPrimary)' not found.
E:\ASWorkSpace\SortFileBySize\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:10: error: style attribute 'attr/colorPrimaryDark (aka com.hp.sortfilebysize:attr/colorPrimaryDark)' not found.
E:\ASWorkSpace\SortFileBySize\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:11: error: style attribute 'attr/colorAccent (aka com.hp.sortfilebysize:attr/colorAccent)' not found.
error: failed linking references.

Command: C:\Users\tangdekun\.gradle\caches\transforms-1\files-1.1\aapt2-3.2.0-beta03-4818971-windows.jar\868db5e9c484c628dee78cdbe6877651\aapt2-3.2.0-beta03-4818971-windows\aapt2.exe link -I\
        N:\ProgramFiles\android\sdk\platforms\android-28\android.jar\
        --manifest\
        E:\ASWorkSpace\SortFileBySize\app\build\intermediates\merged_manifests\debug\processDebugManifest\merged\AndroidManifest.xml\
        -o\
        E:\ASWorkSpace\SortFileBySize\app\build\intermediates\processed_res\debug\processDebugResources\out\resources-debug.ap_\
        -R\
        @E:\ASWorkSpace\SortFileBySize\app\build\intermediates\incremental\processDebugResources\resources-list-for-resources-debug.ap_.txt\
        --auto-add-overlay\
        --java\
        E:\ASWorkSpace\SortFileBySize\app\build\generated\not_namespaced_r_class_sources\debug\processDebugResources\r\
        --custom-package\
        com.hp.sortfilebysize\
        -0\
        apk\
        --output-text-symbols\
        E:\ASWorkSpace\SortFileBySize\app\build\intermediates\symbols\debug\R.txt\
        --no-version-vectors
Daemon:  AAPT2 aapt2-3.2.0-beta03-4818971-windows Daemon #0

错误日志2

Android resource linking failed
Output:  E:\ASWorkSpace\SortFileBySize\app\src\main\res\layout\activity_main.xml:13: error: attribute layout_constraintBottom_toBottomOf (aka com.hp.sortfilebysize:layout_constraintBottom_toBottomOf) not found.
E:\ASWorkSpace\SortFileBySize\app\src\main\res\layout\activity_main.xml:13: error: attribute layout_constraintLeft_toLeftOf (aka com.hp.sortfilebysize:layout_constraintLeft_toLeftOf) not found.
E:\ASWorkSpace\SortFileBySize\app\src\main\res\layout\activity_main.xml:13: error: attribute layout_constraintRight_toRightOf (aka com.hp.sortfilebysize:layout_constraintRight_toRightOf) not found.
E:\ASWorkSpace\SortFileBySize\app\src\main\res\layout\activity_main.xml:13: error: attribute layout_constraintTop_toTopOf (aka com.hp.sortfilebysize:layout_constraintTop_toTopOf) not found.
error: failed linking file resources.

Command: C:\Users\tangdekun\.gradle\caches\transforms-1\files-1.1\aapt2-3.2.0-beta03-4818971-windows.jar\868db5e9c484c628dee78cdbe6877651\aapt2-3.2.0-beta03-4818971-windows\aapt2.exe link -I\
        N:\ProgramFiles\android\sdk\platforms\android-28\android.jar\
        --manifest\
        E:\ASWorkSpace\SortFileBySize\app\build\intermediates\merged_manifests\debug\processDebugManifest\merged\AndroidManifest.xml\
        -o\
        E:\ASWorkSpace\SortFileBySize\app\build\intermediates\processed_res\debug\processDebugResources\out\resources-debug.ap_\
        -R\
        @E:\ASWorkSpace\SortFileBySize\app\build\intermediates\incremental\processDebugResources\resources-list-for-resources-debug.ap_.txt\
        --auto-add-overlay\
        --java\
        E:\ASWorkSpace\SortFileBySize\app\build\generated\not_namespaced_r_class_sources\debug\processDebugResources\r\
        --custom-package\
        com.hp.sortfilebysize\
        -0\
        apk\
        --output-text-symbols\
        E:\ASWorkSpace\SortFileBySize\app\build\intermediates\symbols\debug\R.txt\
        --no-version-vectors
Daemon:  AAPT2 aapt2-3.2.0-beta03-4818971-windows Daemon #0

错误日志3

Android resource linking failed
Output:  E:\ASWorkSpace\Temp\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values-v28\values-v28.xml:7: error: resource android:attr/dialogCornerRadius not found.
E:\ASWorkSpace\Temp\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values-v28\values-v28.xml:11: error: resource android:attr/dialogCornerRadius not found.
E:\ASWorkSpace\Temp\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:946: error: resource android:attr/fontVariationSettings not found.
E:\ASWorkSpace\Temp\app\build\intermediates\incremental\mergeDebugResources\merged.dir\values\values.xml:946: error: resource android:attr/ttcIndex not found.
error: failed linking references.

Command: C:\Users\tangdekun\.gradle\caches\transforms-1\files-1.1\aapt2-3.2.0-beta03-4818971-windows.jar\868db5e9c484c628dee78cdbe6877651\aapt2-3.2.0-beta03-4818971-windows\aapt2.exe link -I\
        N:\ProgramFiles\android\sdk\platforms\android-27\android.jar\
        --manifest\
        E:\ASWorkSpace\Temp\app\build\intermediates\merged_manifests\debug\processDebugManifest\merged\AndroidManifest.xml\
        -o\
        E:\ASWorkSpace\Temp\app\build\intermediates\processed_res\debug\processDebugResources\out\resources-debug.ap_\
        -R\
        @E:\ASWorkSpace\Temp\app\build\intermediates\incremental\processDebugResources\resources-list-for-resources-debug.ap_.txt\
        --auto-add-overlay\
        --java\
        E:\ASWorkSpace\Temp\app\build\generated\not_namespaced_r_class_sources\debug\processDebugResources\r\
        --custom-package\
        com.hp.temp\
        -0\
        apk\
        --output-text-symbols\
        E:\ASWorkSpace\Temp\app\build\intermediates\symbols\debug\R.txt\
        --no-version-vectors
Daemon:  AAPT2 aapt2-3.2.0-beta03-4818971-windows Daemon #0

问题出现的原因:

Android studio升级到3.0以上,工程自动会引入Androidx的相关包,当因为某些原因,我们把默认依赖删除以后,就会出现 Android resource linking failed ,错误日志1是缺少 androidx.appcompat:appcompat:1.0.0-alpha1 依赖,错误日志2是缺少 androidx.constraintlayout:constraintlayout:1.1.2 依赖,

错误日志3是因为 compileSdkVersion 版本不对,应该改为 compileSdkVersion 28

解决方案

添加相关依赖即可,如下:

implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'

二. java.lang.ClassNotFoundException

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider4Util" on path: DexPathList[[zip file "/data/app/com.hp.temp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.hp.temp-1/lib/arm64, /system/lib64, /vendor/lib64, /system/vendor/lib64, /oem/lib, /oem/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
        at android.app.ActivityThread.installProvider(ActivityThread.java:5962)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:5545) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5484) 
        at android.app.ActivityThread.-wrap2(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1595) 
        at android.os.Handler.dispatchMessage(Handler.java:110) 
        at android.os.Looper.loop(Looper.java:203) 
        at android.app.ActivityThread.main(ActivityThread.java:6251) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924) 
        Suppressed: java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/content/FileProvider;
        at java.lang.VMClassLoader.findLoadedClass(Native Method)
        at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:742)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:362)
                ... 12 more
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.content.FileProvider" on path: DexPathList[[zip file "/data/app/com.hp.temp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.hp.temp-1/lib/arm64, /system/lib64, /vendor/lib64, /system/vendor/lib64, /oem/lib, /oem/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
                ... 15 more

原因分析:

build.gradle文件如下

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.hp.temp"
        minSdkVersion 15
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
//    implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    implementation 'com.android.support:appcompat-v7:27.1.1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
    implementation 'com.blankj:utilcode:1.15.0'
}

原因我依赖了 implementation 'com.blankj:utilcode:1.15.0' ,其中有个 FileProvider 类找不到,如下:

package android.support.v4.content;

import android.support.annotation.Keep;

/**
 * <pre>
 *     author: Blankj
 *     blog  : http://blankj.com
 *     time  : 2018/04/23
 *     desc  :
 * </pre>
 */
@Keep
public final class FileProvider4Util extends FileProvider {
}

由于androidx的使用,架包的分包方式发生了变化, com.android.support:support-v4androidx.core 代替,但是 utilcode 架包中并没有使用androidx分包结构,所以就存在问题.

解决方案:

1.不使用androidx分包,将

<code>implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'</code>
<code>  implementation 'com.android.support:appcompat-v7:27.1.1'</code>

虽然后报红,但是不会报错

2.重构到新的androidx命名空间

工具栏 —>Refactor—>Migrate to AndroidX


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

编写高质量代码

编写高质量代码

秦小波 / 机械工业出版社华章公司 / 2011-12-28 / 59.00元

在通往“Java技术殿堂”的路上,本书将为你指点迷津!内容全部由Java编码的最佳实践组成,从语法、程序设计和架构、工具和框架、编码风格和编程思想等五大方面对Java程序员遇到的各种棘手的疑难问题给出了经验性的解决方案,为Java程序员如何编写高质量的Java代码提出了151条极为宝贵的建议。对于每一个问题,不仅以建议的方式从正反两面给出了被实践证明为十分优秀的解决方案和非常糟糕的解决方案,而且还......一起来看看 《编写高质量代码》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具