如何在Android Studio中添加传递依赖项

时间:2018-10-16 12:04:38

标签: android android-studio android-gradle

背景

我的旧计算机上有一个运行Android Studio 2.2.2和Gradle 2.1.4的项目。 我现在有一台装有最新版本或更高版本android studio(3.2)和gradle 4.3的新机器。

我最初的项目是使用compilesdkVersion 23并集成了GCM。现在,我有一台新计算机,并希望将我的IDE和依赖项更新到最新版本,以便我可以集成FireBase并使用运行时权限。我决定在新机器上完成所有这些操作,因为我的软件正在8000个设备上生产,并且不想破坏我的生产版本。

问题

我的新项目正在使用compileSdkVersion 28和buildToolsVersion '28 .0.0',但出现以下构建错误:

Could not find com.android.support:mediarouter:28.0.0.
Searched in the following locations:
    file:/C:/Users/MatthewW/AppData/Local/Android/Sdk/extras/m2repository/com/android/support/mediarouter/28.0.0/mediarouter-28.0.0.pom
    file:/C:/Users/MatthewW/AppData/Local/Android/Sdk/extras/m2repository/com/android/support/mediarouter/28.0.0/mediarouter-28.0.0.jar
    file:/C:/Users/MatthewW/AppData/Local/Android/Sdk/extras/google/m2repository/com/android/support/mediarouter/28.0.0/mediarouter-28.0.0.pom
    file:/C:/Users/MatthewW/AppData/Local/Android/Sdk/extras/google/m2repository/com/android/support/mediarouter/28.0.0/mediarouter-28.0.0.jar
    file:/C:/Users/MatthewW/AppData/Local/Android/Sdk/extras/android/m2repository/com/android/support/mediarouter/28.0.0/mediarouter-28.0.0.pom
    file:/C:/Users/MatthewW/AppData/Local/Android/Sdk/extras/android/m2repository/com/android/support/mediarouter/28.0.0/mediarouter-28.0.0.jar
    https://repo1.maven.org/maven2/com/android/support/mediarouter/28.0.0/mediarouter-28.0.0.pom
    https://repo1.maven.org/maven2/com/android/support/mediarouter/28.0.0/mediarouter-28.0.0.jar
    https://jcenter.bintray.com/com/android/support/mediarouter/28.0.0/mediarouter-28.0.0.pom
    https://jcenter.bintray.com/com/android/support/mediarouter/28.0.0/mediarouter-28.0.0.jar
    https://dl.google.com/dl/android/maven2/com/android/support/mediarouter/28.0.0/mediarouter-28.0.0.pom
    https://dl.google.com/dl/android/maven2/com/android/support/mediarouter/28.0.0/mediarouter-28.0.0.jar
Required by:
    project :app

Please install the Android Support Repository from the Android SDK Manager.
Open Android SDK Manager

我已经安装了Android支持存储库

enter image description here

此外,在我的build.gradle文件中的依存关系中,appCompat 28.0.0用红色下划线标出。当我将鼠标悬停在它上面时,它指出所有Android支持库必须使用完全相同的版本规范。我已指定28.0.0,但已找到传递依赖项24.0.0(mediarouter),我敢肯定还会有其他依赖项。

要找到所有可传递依赖项,我在此帖子后加上cricket_007:

transitive dependencies

我无法发布输出,因为它超过了StackOverflow上的30000个字符限制,但是下面是一个例外,它显示mediarouter是v24。

Executing tasks: [androidDependencies]

Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
The CompileOptions.bootClasspath property has been deprecated and is scheduled to be removed in Gradle 5.0. Please use the CompileOptions.bootstrapClasspath property instead.
:app:androidDependencies
debug
debugCompileClasspath - Dependencies for compilation
+--- C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\libs\activation.jar
+--- C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\libs\additionnal.jar
+--- C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\libs\core.jar
+--- C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\libs\mail.jar
+--- C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\libs\ndeftools-1.2.3.jar
+--- C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\libs\gcm.jar
+--- com.android.support:design:28.0.0@aar
+--- com.google.android.gms:play-services:9.8.0@aar
+--- com.google.android.gms:play-services-cast-framework:9.8.0@aar
+--- com.google.android.gms:play-services-cast:9.8.0@aar
+--- com.android.support:mediarouter-v7:24.0.0@aar
+--- com.android.support:appcompat-v7:28.0.0@aar

当我清理项目时,我得到以下信息(我不得不将屏幕一分为二,因为它们很小):

enter image description here enter image description here

在上面的屏幕截图中,它指出:

org.gradle.tooling.BuildException: Failed to execute aapt

我已经读到,这可能与资源和名称中的长名称或特殊字符有关。

有人对我如何无误地构建这个项目有任何想法吗?

[编辑1] 这是我的build.gradle

> apply plugin: 'com.android.application'
> 
> android {
>     compileSdkVersion 28
>     buildToolsVersion '28.0.0'
> 
> 
>     useLibrary 'org.apache.http.legacy'
> 
>     lintOptions {
>         checkReleaseBuilds false
>         // Or, if you prefer, you can continue to check for errors in release builds,
>         // but continue the build even when errors are found:
>         abortOnError false
>     }
> 
>     defaultConfig {
>         applicationId "com.xxxx"
>         minSdkVersion 16
>         targetSdkVersion 28
>         versionName "5.1"
>         setProperty("archivesBaseName", "RR4v$versionName")
>     }
> 
>     buildTypes {
>         release {
>             minifyEnabled true
>             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-project.txt'
>         }
>     } }
> 
> dependencies {
>     compile files('libs/activation.jar')
>     compile files('libs/additionnal.jar')
>     compile files('libs/core.jar')
>     compile files('libs/mail.jar')
>     compile files('libs/ndeftools-1.2.3.jar')
>     compile files('libs/gcm.jar')
>     compile 'com.android.support:appcompat-v7:28.0.0'
>     compile 'com.google.android.gms:play-services:9.8.0'
>     compile 'joda-time:joda-time:2.1'
>     compile 'net.zetetic:android-database-sqlcipher:3.5.6@aar'
>     compile 'com.android.support:support-v4:28.0.0'
>     compile 'org.apache.directory.studio:org.apache.commons.io:2.4'
>     compile 'com.android.support:design:28.0.0'
> 
> 
> }

[EDIT2]

dependencies {
    compile files('libs/activation.jar')
    compile files('libs/additionnal.jar')
    compile files('libs/core.jar')
    compile files('libs/mail.jar')
    compile files('libs/ndeftools-1.2.3.jar')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.google.android.gms:play-services:9.8.0'
    implementation 'joda-time:joda-time:2.1'
    implementation 'net.zetetic:android-database-sqlcipher:3.5.6@aar'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'org.apache.directory.studio:org.apache.commons.io:2.4'
    implementation 'com.android.support:design:28.0.0'

[EDIT3]

C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3>gradlew clean :app:processDebugResources

> Configure project :app 
Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
The CompileOptions.bootClasspath property has been deprecated and is scheduled to be removed in Gradle 5.0. Please use the CompileOptions.bootstrapClasspath property instead.

C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\src\main\res\layout\drop_down_list_row.xml:7: AAPT: error: '@+DropDownList/checkbox' is incompatible with attribute id (attr) reference.

C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\src\main\res\layout\drop_down_list_row.xml:12: AAPT: error: '@+DropDownList/SelectOption' is incompatible with attribute id (attr) reference.

C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\src\main\res\layout\messagerecipientlayout.xml:15: AAPT: error: '@+DropDownList/SelectBox' is incompatible with attribute id (attr) reference.

C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\src\main\res\layout\messagerecipientlayout.xml:25: AAPT: error: '@+DropDownList/create' is incompatible with attribute id (attr) reference.

C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\src\main\res\layout\pop_up_window.xml:9: AAPT: error: '@+DropDownList/dropDownList' is incompatible with attribute id (attr) reference.

C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\src\main\res\layout\drop_down_list_row.xml:7: error: '@+DropDownList/checkbox' is incompatible with attribute id (attr) reference.
C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\src\main\res\layout\drop_down_list_row.xml:12: error: '@+DropDownList/SelectOption' is incompatible with attribute id (attr) reference.
C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\src\main\res\layout\messagerecipientlayout.xml:15: error: '@+DropDownList/SelectBox' is incompatible with attribute id (attr) reference.
C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\src\main\res\layout\messagerecipientlayout.xml:25: error: '@+DropDownList/create' is incompatible with attribute id (attr) reference.
C:\Users\MatthewW\AndroidStudioProjects\nfcscanner3\app\src\main\res\layout\pop_up_window.xml:9: error: '@+DropDownList/dropDownList' is incompatible with attribute id (attr) reference.
error: failed linking file resources.


> Task :app:processDebugResources FAILED

1 个答案:

答案 0 :(得分:0)

最好获得最新版本。只是有些担心。对于您而言,红线表示 com.android.support:animated-vector-drawable com.android.support:mediarouter-v7 库都包含在< strong> com.android.support:appcompat-v7:28.0.0 ,并且在您使用的某些第三方(使用不同版本)中。您需要排除旧的。问题是如何确定要排除的库。这并不困难。

我将举例说明模块 com.android.support:customtabs 发生冲突的情况。

首先,我们需要使用命令列出依赖项:

./gradlew app:dependencies

您将看到这样的图形:

+--- io.reactivex:rxandroid:1.2.1
|    \--- io.reactivex:rxjava:1.1.6
+--- io.reactivex:rxjava:1.1.0 -> 1.1.6
+--- com.github.mmin18:flexlayout:1.2.6
+--- com.android.support:customtabs:27.1.1
|    +--- com.android.support:support-compat:27.1.1 (*)
|    +--- com.android.support:support-annotations:27.1.1
|    \--- com.android.support:support-core-ui:27.1.1 (*)
+--- com.microsoft.identity.client:msal:0.1.3
|    +--- com.android.support:customtabs:25.3.1 -> 27.1.1 (*)
|    \--- com.google.code.gson:gson:2.8.1

在此示例中,红线带有冲突的模块 com.android.support:customtabs ,因为它都包含在 com.android.support 中com.microsoft.identity.client:msal:0.1.3 。因此,我们需要将其从Microsoft模块中排除。只需在gradle中使用此配置:

implementation('com.microsoft.identity.client:msal:0.1.3') {
    exclude group: 'com.android.support', module: 'customtabs'
}

然后只需重建gradle即可解决与模块 com.android.support:customtabs 的冲突。 您可以对其他模块应用相同的步骤。