Android Studio/Gradle在测试中找不到AppCompat资源

12

我在使用Gradle在Android Studio中构建测试时遇到了问题。据我所知,它似乎没有包括AppCompat v7的资源在其测试编译中,这意味着我的自定义样式正在引用一大堆它不能“看到”的AppCompat样式。这导致下面的错误。

对于普通的非测试构建来说,它完全正常,样式被正确应用,项目可以构建/安装/执行,没有错误。如何解决这个错误?

完整的Gradle错误:

Execution failed for task ':processDebugTestResources'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    /home/me/Android Studio/sdk/build-tools/android-4.4/aapt package -f --no-crunch -I /home/me/Android Studio/sdk/platforms/android-19/android.jar -M /home/me/Development/Android/MyApp/build/manifests/test/debug/AndroidManifest.xml -S /home/me/Development/Android/MyApp/build/res/all/test/debug -A /home/me/Development/Android/MyApp/build/assets/test/debug -m -J /home/me/Development/Android/MyApp/build/source/r/test/debug -F /home/me/Development/Android/MyApp/build/libs/MyApp-debug-test.ap_ --debug-mode
  Error Code:
    1
  Output:
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values/values.xml:113: error: Error retrieving parent for item: No resource found that matches the given name '@style/Widget.AppCompat.ActionBar'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values/values.xml:116: error: Error: No resource found that matches the given name: attr 'titleTextStyle'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values-v11/values.xml:11: error: Error retrieving parent for item: No resource found that matches the given name '@style/Widget.AppCompat.ActionBar'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values-v14/values.xml:11: error: Error retrieving parent for item: No resource found that matches the given name '@style/Widget.AppCompat.ActionBar'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values/values.xml:119: error: Error retrieving parent for item: No resource found that matches the given name '@style/Widget.AppCompat.ActionButton'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values-v11/values.xml:17: error: Error retrieving parent for item: No resource found that matches the given name '@style/Widget.AppCompat.ActionButton'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values-v14/values.xml:17: error: Error retrieving parent for item: No resource found that matches the given name '@style/Widget.AppCompat.ActionButton'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values/values.xml:123: error: Error retrieving parent for item: No resource found that matches the given name '@style/Widget.AppCompat.ActionBar.TabView'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values/values.xml:127: error: Error: No resource found that matches the given name: attr 'dividerPadding'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values/values.xml:126: error: Error: No resource found that matches the given name: attr 'showDividers'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values-v11/values.xml:21: error: Error retrieving parent for item: No resource found that matches the given name '@style/Widget.AppCompat.ActionBar.TabView'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values-v14/values.xml:21: error: Error retrieving parent for item: No resource found that matches the given name '@style/Widget.AppCompat.ActionBar.TabView'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values/values.xml:131: error: Error retrieving parent for item: No resource found that matches the given name '@style/TextAppearance.AppCompat.Widget.ActionBar.Title'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values-v11/values.xml:29: error: Error retrieving parent for item: No resource found that matches the given name '@style/TextAppearance.AppCompat.Widget.ActionBar.Title'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values-v14/values.xml:29: error: Error retrieving parent for item: No resource found that matches the given name '@style/TextAppearance.AppCompat.Widget.ActionBar.Title'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values/values.xml:136: error: Error retrieving parent for item: No resource found that matches the given name '@style/Theme.AppCompat'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values/values.xml:139: error: Error: No resource found that matches the given name: attr 'actionBarStyle'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values/values.xml:141: error: Error: No resource found that matches the given name: attr 'actionBarTabBarStyle'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values/values.xml:140: error: Error: No resource found that matches the given name: attr 'actionModeBackground'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values-v11/values.xml:34: error: Error retrieving parent for item: No resource found that matches the given name '@style/Theme.AppCompat'.
    /home/me/Development/Android/MyApp/build/res/all/test/debug/values-v14/values.xml:34: error: Error retrieving parent for item: No resource found that matches the given name '@style/Theme.AppCompat'.

build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.0+'
    }
}

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile 'com.android.support:support-v4:18.0+'
    compile "com.android.support:appcompat-v7:18.0.+"
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 10
        targetSdkVersion 19
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
        instrumentTest {

            java.srcDirs = ['tests']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
    lintOptions {
        abortOnError false
    }
}

值/styles.xml:

<resources>

    <style name="CPTheme" parent="@style/Theme.AppCompat">
        <item name="android:textColor">@color/cp_text</item>
        <item name="android:windowBackground">@color/cp_background_light</item>
        <item name="actionBarStyle">@style/CPThemeActionBar</item>
        <item name="actionModeBackground">@color/cp_titlebar_background</item>
        <item name="actionBarTabBarStyle">@style/CPThemeTabBar</item>
    </style>

    <style name="CPThemeActionBar" parent="@style/Widget.AppCompat.ActionBar">
        <item name="android:background">@color/cp_titlebar_background</item>
        <item name="android:textColor">@color/cp_text</item>
        <item name="titleTextStyle">@style/CPThemeTitleTextStyle</item>
    </style>

    <style name="CPThemeTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/cp_text</item>
        <item name="android:background">@color/cp_titlebar_background</item>
    </style>

    <style name="CPThemeTabBar"
        parent="@style/Widget.AppCompat.ActionBar.TabView" >
        <item name="android:background">@color/cp_background_light</item>
        <item name="android:divider">?android:attr/actionBarDivider</item>
        <item name="showDividers">middle</item>
        <item name="dividerPadding">12dp</item>
        <item name="android:padding">0dp</item>
    </style>

    <style name="CPThemeActionBarButton" parent="@style/Widget.AppCompat.ActionButton">
        <item name="android:background">@color/cp_titlebar_background</item>
    </style>

</resources>

values/styles-v11.xml 和 values/styles-v14.xml(它们是彼此复制粘贴的):

<resources>

    <style name="CPTheme" parent="@style/Theme.AppCompat">
        <item name="android:textColor">@color/cp_text</item>
        <item name="android:windowBackground">@color/cp_background_light</item>
        <item name="android:actionBarStyle">@style/CPThemeActionBar</item>
        <item name="android:actionModeBackground">@color/cp_titlebar_background</item>
        <item name="android:actionBarTabBarStyle">@style/CPThemeTabBar</item>
    </style>

    <style name="CPThemeActionBar" parent="@style/Widget.AppCompat.ActionBar">
        <item name="android:background">@color/cp_titlebar_background</item>
        <item name="android:textColor">@color/cp_text</item>
        <item name="android:titleTextStyle">@style/CPThemeTitleTextStyle</item>
    </style>

    <style name="CPThemeTitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:textColor">@color/cp_text</item>
        <item name="android:background">@color/cp_titlebar_background</item>
    </style>

    <style name="CPThemeTabBar"
        parent="@style/Widget.AppCompat.ActionBar.TabView" >
        <item name="android:background">@color/cp_background_light</item>
        <item name="android:divider">?android:attr/actionBarDivider</item>
        <item name="android:showDividers">middle</item>
        <item name="android:dividerPadding">12dp</item>
        <item name="android:padding">0dp</item>
    </style>

    <style name="CPThemeActionBarButton" parent="@style/Widget.AppCompat.ActionButton">
        <item name="android:background">@color/cp_titlebar_background</item>
    </style>

    <style name="BorderlessButton">
        <item name="android:background">@color/cp_background_light</item>
    </style>

    <style name="ListViewCells">
        <item name="android:background">@color/cp_background_dark</item>
        <item name="android:textColor">@color/cp_text</item>
    </style>

</resources>

在你的构建脚本中尝试这个(使用19代替18): compile 'com.android.support:support-v4:19.0+' compile 'com.android.support:appcompat-v7:19.0.+' - Mohammad Reza Norouzi
@descent 你还需要关于这个问题的帮助吗? - Jared Burrows
应使用所有可用的构建工具的最新版本 classpath 'com.android.tools.build:gradle:1.1.0' buildToolsVersion "21.1.2" - imort
1个回答

1
  1. 确保你已经通过SDK管理器下载了最新的附加组件(Android支持库)
  2. 通过SDK管理器下载Android 5.0 SDK
  3. 打开你的应用模块的build.gradle文件并将compileSdkVersion更改为21

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接