前言
google 在I/O 2018 更新了material design设计原则,给开发者推出了新的MDC-android支持库,但是在我想写个demo测试一下时,却遇到了很多问题,在下面记录一下。
1. 首先遇到的问题是编译错误 AAPT2 error: check logs for details
在app下的build.gradle 中添加implementation'com.google.android.material:material:1.0.0-alpha1' error
错误的具体原因是error: resource android:attr/dialogCornerRadius not found.也就是不能找的value-28v中的资源。
解决方法:将 app 下的 build.gradle 中 将compileVsersion 改成 'android-P'

然后sync now,会提示你下载android P preview版本,下载完成再编译就不会再报错。
2. 在build时提示 Compilation failed to complete
具体报错是Program type already present: android.support.v4.app.INotificationSideChannel$Stub,其实是因为依赖重复而导致build不通过。
解决方法1:
将implementation 'com.android.support:appcompat-v7:27.1.1' 更改为 implementation 'androidx.appcompat:appcompat:1.0.0-alpha1';将implementation 'com.android.support:support-v4:27.1.1'更改为implementation 'androidx.legacy:legacy-support-v4:1.0.0-alpha1'
将constraintlayout 依赖改为 implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
如果存在design包,直接删除即可。
在build.properties文件中添加
android.enableJetifier=true
android.useAndroidX=true
最后比较麻烦的是将 java文件中原来添加的v7 和 v4包中的import删除,重新 import class.
gradle 如下图


解决方法2.
下载 android studio 3.2 Canary
使用Refactor to AndroidX功能一键转换依赖
android studio >>refactor >> refactor to AndroidX
注意:使用此功能同样需要将compileVersion设置成 android-P

关于Android X
Refactored versions of the Android APIs that are not bundled with the operating system. 未与操作系统捆绑的Android API的重构版本。
网友评论