我已经成功配置了android-P SDK环境。当我尝试使用android设计支持库时,我面临项目构建错误。项目配置是:
IDE:3.2 Canary 17目标API:28编译API:28
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.app.navigationpoc"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-alpha3'
implementation 'androidx.constraintlayout:constraintlayout:1.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.android.support:design:28.0.0-alpha3'
implementation 'com.android.support:cardview-v7:28.0.0-alpha3'
}
并且构建失败的错误是:
清单合并失败:来自[androidx.core:core:1.0.0-alpha3] AndroidManifest.xml:22:18-86的属性应用程序@ appComponentFactory值=(androidx.core.app.CoreComponentFactory)也出现在[com.android] .support:support-compat:28.0.0-alpha3] AndroidManifest.xml:22:18-91 value =(android.support.v4.app.CoreComponentFactory)。建议:在AndroidManifest.xml:6:5-40:19中添加'tools:replace =“android:appComponentFactory”'来覆盖。
您既可以使用先前的API包版本的工件,也可以使用新的Androidx,而不是两者。
如果您想使用以前的版本,请用您的依赖项替换
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:design:28.0.0-alpha3'
implementation 'com.android.support:cardview-v7:28.0.0-alpha3'
}
如果你想使用Androidx:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-alpha3'
implementation 'androidx.constraintlayout:constraintlayout:1.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.google.android.material:material:1.0.0-alpha3'
implementation 'androidx.cardview:cardview:1.0.0-alpha3'
}
注意:您不应同时在应用中使用com.android.support和com.google.android.material依赖项。
添加Android的Material Components
dependencies {
// ...
implementation 'com.google.android.material:material:1.0.0-beta01'
// ...
}
如果您的应用当前依赖于原始设计支持库,则可以使用Android Studio提供的Refactor to AndroidX ...选项。这样做会更新您的应用程序的依赖项和代码,以使用新打包的androidx和com.google.android.material库。
如果您还不想切换到新的androidx和com.google.android.material包,可以通过com.android.support:design:28.0.0-alpha3依赖项使用Material Components。
下面的代码与我完美配合:
dependencies {
api 'com.android.support:design:28.0.0-alpha3'
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha2'
}
试试这个:
implementation 'com.android.support:appcompat-v7:28.0.0-alpha1'
Android文档很清楚。转到下面的页面。在下面,有两列名称为“OLD BUILD ARTIFACT”和“AndroidX build artifact”
https://developer.android.com/jetpack/androidx/migrate
现在你在gradle中有很多依赖项。只需与Androidx构建工件匹配,并在gradle中替换它们。
这还不够。
转到您的MainActivity(对所有活动重复此操作)并在语句“public class MainActivity extends AppCompatActivity”中删除单词AppCompact Activity并再次编写相同的单词。但这次androidx库被导入。直到现在appcompact支持文件已导入并且used(同样,删除那个appcompact import语句)。
另外,转到布局文件。假设你有一个约束布局,那么你可以注意到xml文件中的第一行约束布局有一些与appcompact相关的内容。所以只需删除它并再次编写Constraint布局。但是现在添加了与androidx相关的约束布局。
对于尽可能多的活动和许多xml布局文件重复此操作。
但不要担心:Android Studio会在编译时显示所有此类可能的错误。
正确的解决方法是在this回答中讨论的。
您必须通过将这两行添加到jetifier
文件来启用gradle.properties
:
android.useAndroidX=true
android.enableJetifier=true
这将基本上与AndroidX-1交换项目依赖项中的Android支持库依赖项。
如果gradle.properties
不存在,只需在根项目中创建一个名为gradle.properties的文件,并将上述行添加到其中:
root
|--gradle.properties
|--build.gradle
|--settings.gradle
|--app
|----build.gradle
有类似的问题。在build.gradle中添加,它对我有用。
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Android将不会在28.0.0
之后更新支持库。
这将是android.support包装下的最后一个功能版本,鼓励开发人员使用migrate to AndroidX 1.0.0。
所以使用库AndroidX。
androidx
或任何库,因为alpha,beta,rc可能有您不想随应用程序提供的错误。在你的情况下
dependencies {
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.1'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
}
添加这个:
tools:replace="android:appComponentFactory"
android:appComponentFactory="whateverString"
到您的清单应用程序
<application
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
tools:replace="android:appComponentFactory"
android:appComponentFactory="whateverString">
希望能帮助到你
我用过那个选项:
使用Android Studio 3.2及更高版本,您可以通过从菜单栏中选择Refactor> Migrate to AndroidX,快速迁移现有项目以使用AndroidX。
1.将这些代码添加到app / build.gradle:
configurations.all {
resolutionStrategy.force 'com.android.support:support-v4:26.1.0' // the lib is old dependencies version;
}
2.修改了sdk和工具版本到28:
compileSdkVersion 28
buildToolsVersion '28.0.3'
targetSdkVersion 28
2.在AndroidManifest.xml文件中,您应该添加两行:
<application
android:name=".YourApplication"
android:appComponentFactory="anystrings be placeholder"
tools:replace="android:appComponentFactory"
android:icon="@drawable/icon"
android:label="@string/app_name"
android:largeHeap="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
谢谢你的答案@Carlos Santiago:Android design support library for API 28 (P) not working
打开文件gradle.properties并将这两行添加到它:
android.useAndroidX = true
android.enableJetifier = true
清洁和建设
谷歌推出了新的AndroidX依赖项。你需要迁移到AndroidX,这很简单。
我将所有依赖项替换为AndroidX依赖项
旧的设计依赖
implementation 'com.android.support:design:28.0.0'
新的AndroidX设计依赖
implementation 'com.google.android.material:material:1.0.0-rc01'
你可以在这里找到AndroidX依赖项https://developer.android.com/jetpack/androidx/migrate
自动AndroidX迁移选项(android studio 3.3+支持)
通过从菜单栏中选择Refactor> Migrate to AndroidX,将现有项目迁移到使用AndroidX。
我通过将所有androidx.*
替换为appropiate package name
来克服这种情况。
改变你的路线
implementation 'androidx.appcompat:appcompat:1.0.0-alpha3'
implementation 'androidx.constraintlayout:constraintlayout:1.1.1'
androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
至
implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
implementation 'com.android.support.constraint:constraint-layout:1.1.1'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
风陵渡
tools:replace="android:appComponentFactory"
androidX的设计支持库是implementation 'com.google.android.material:material:1.0.0'