android - 程序类型已经存在:使用带有 firebase 和 google play 服务的 androidx 库无法构建 android studio 3.1.1
问题描述
我在 apk 构建中面临以下冲突
Program type already present: android.support.v4.media.MediaBrowserCompat$CallbackHandler
Message{kind=ERROR, text=Program type already present: android.support.v4.media.MediaBrowserCompat$CallbackHandler, sources=[Unknown source file], tool name=Optional.of(D8)}
下面是我的应用程序
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'realm-android'
apply plugin: 'kotlin-kapt'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.himotech"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
dataBinding {
enabled true
}
packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE-FIREBASE.txt'
exclude 'META-INF/NOTICE'
}
lintOptions {
abortOnError false
}
realm {
syncEnabled = false;
}
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-jre7:$kotlin_version"
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.appcompat:appcompat:1.0.0'
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'
}
apply plugin: 'com.google.gms.google-services'
下面是项目gradle文件
buildscript {
ext.kotlin_version = '1.2.30'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "io.realm:realm-gradle-plugin:5.4.1"
classpath 'com.google.gms:google-services:4.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
ext{
// The following are only a few examples of the types of properties you can define.
compileSdkVersion = 27
// You can also create properties to specify versions for dependencies.
// Having consistent versions between modules can avoid conflicts with behavior.
supportLibVersion = '27.1.1'
firebaseCoreVersion='16.0.3'
firebaseDatabaseVersion='16.0.2'
firebaseLibVersion='15.1.0'
firebaseMessaging='17.3.1'
}
在 gradle 属性中,我启用了 androidx 使用:
android.useAndroidX = true
android.enableJetifier = false
我确实经历了这个,但仍然无法解决问题。
注意: 如果我从 gradle 中删除数据绑定,那么它工作正常。
解决方案
我在你的 gradle 中看到一堆过时的项目。
您需要将您的 Android Studio 移动到 3.2。(稳定
此外,您的 kotlin 版本应该是 1.2.70+(不要使用 1.2.71)它有错误。严重的是,它会导致Android Studio冻结很多。
您的 Gradle 构建工具也应该在 3.2.0+ 范围内。
确保您打包在 gradle-wrapper.properties 中的 distributionUrl 为 4.10-all 或更高版本
应删除您的 jre 依赖项或 jdk 依赖项
你的 jre 版本是 7,你应该更新到 8。
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
约束布局有 2.0.0-alpha2 输出,如果你可以在那个上稍微领先一点的话。虽然不是必需的。
截至发布日期的 androidx 当前依赖项示例。
//Android Google Libraries
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation "androidx.constraintlayout:constraintlayout:2.0.0-alpha2"
听起来你已经使用过,但确认你有 gradle.properties
android.enableJetifier=true
android.useAndroidX=true
jetifier 将尝试使您的依赖项与 androidX 兼容。最后,如果你有太多问题,更新到 androidX,放弃你的更改,在一个好的地方从版本控制中编译它。然后使用3.2中的迁移工具。它做得很好,只留下一些小错误需要清理。
另外值得注意的是,您可能必须转到每个 xml 文件并确认正确引用了新的命名空间,例如 RecyclerView 应该在您的 xml 中显示新的 androidx 命名空间。当您在代码中引用它们时也是如此。
我最近将我所有的项目都更新到了 androidX,第一个项目经历了一些试验和错误,第二个项目我使用了迁移工具,它让我的生活更加顺畅。
删除数据绑定对您有用的原因是因为它试图将“不正确的名称空间”对象构建到您的数据绑定生成的文件中。您需要检查并修复每个 xml 文件,以便在您的 Android 小部件和框架元素上使用正确的命名空间。
我将提供一个抽屉活动的示例。
<data>
<variable name="activity" type="com.a35.activities.MainActivity"/>
<variable name="iBindingRecyclerView" type="com.a35.interfaces.IBindingRecyclerView"/>
</data>
<androidx.drawerlayout.widget.DrawerLayout
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--TOP TOOLBAR-->
<include
android:id="@+id/toolbarMain"
layout="@layout/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!--TOP BLACK LINE-->
<View
android:id="@+id/vRedLine"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_2"
android:background="@color/black" />
<!-- FrameLayout is used to insert fragments to display -->
<FrameLayout
android:id="@+id/fragPlaceholder"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</LinearLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
android:background="@color/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/nav_drawer_header"
android:id="@+id/navHeader"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/lstMenuItems"
android:layout_below="@+id/navHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:bindRcvInterface="@{iBindingRecyclerView}"
app:bindRcvList="@{activity.getDrawerItemList}"/>
<ImageView
android:id="@+id/imgBottomLogo"
android:layout_width="@dimen/dp_160"
android:layout_height="@dimen/dp_35"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="@dimen/dp_35"
android:src="@drawable/my_logo" />
</RelativeLayout>
</com.google.android.material.navigation.NavigationView>
</androidx.drawerlayout.widget.DrawerLayout>
推荐阅读
- php - NetSuite PHP Toolkit 时间条目搜索
- java - 将 resteasy-jaxrs 从 3.0.26.Final 升级到 3.12.0.Final 时出现运行时错误
- c# - ClosedXML 未返回正确的单元格值
- spring-boot - 多个用户的单个 Spring Boot 应用程序工作
- security - WinAppDriver 是否提供任何身份验证机制和/或 HTTPS 连接?
- vue.js - 如何在 Nuxt 和 Axios 代理中添加默认参数?
- python - 获取由 pyppeteer 呈现的页面发送的请求的 URL
- netlogo - 如何在 NetLogo 中一次杀死所有乌龟?
- python-3.x - 是否可以在 MDDialog 中包含 MDButtons 而无需从主应用程序添加按钮
- reactjs - 我需要一个带有树组件和“内容”窗格的 React 组件