在 Android Studio 中使用 Robolectric 和 AppCompat 时找不到 StyleData 的父级



我有一个Android项目,它使用:

    Android Studio Gradle结构
  • Android AppCompat库
  • robolelectric用于测试

测试不涉及任何活动(例如,只是断言1 == 1),工作得很好。但是,当我使用活动创建以下错误:

java.lang.RuntimeException: huh? can't find parent for StyleData{name='Theme_Sanactbar', parent='@style/Theme_AppCompat_Light_DarkActionBar'}
    at org.robolectric.shadows.ShadowAssetManager$StyleResolver.getParent(ShadowAssetManager.java:365)
    at org.robolectric.shadows.ShadowAssetManager$StyleResolver.getAttrValue(ShadowAssetManager.java:350)

我尝试https://github.com/robolectric/robolectric/issues/979,但它创建相同的结果。


供参考:

This is my PROJECT/build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    repositories {
        mavenCentral()
        // For Robolectric
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
        maven { url 'https://github.com/rockerhieu/mvn-repo/raw/master/' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.11.+'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2+'
        classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.10.1-SNAPSHOT'
    }
}
allprojects {
    repositories {
        mavenCentral()
        // For Robolectric
        maven { url 'https://oss.sonatype.org/content/repositories/snapshots' }
    }
}

我的PROJECT/app/build.gradle

apply plugin: 'android'
apply plugin: 'android-apt'
apply plugin: 'android-test' // Robolectric
repositories {
    mavenLocal()
}
android {
    compileSdkVersion 19
    buildToolsVersion "19.1.0"
    defaultConfig {
        applicationId "id.web.michsan.helloworld"
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 6
        versionName "1.1.1"
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
    if (System.getenv("KEYSTORE") != null) {
        signingConfigs {
            release {
                storeFile file(System.getenv("KEYSTORE"))
                storePassword System.getenv("KEYSTORE_PASSWORD")
                keyAlias System.getenv("KEY_ALIAS")
                keyPassword System.getenv("KEY_PASSWORD")
            }
        }
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
            if (System.getenv("KEYSTORE") != null) {
                signingConfig signingConfigs.release
            }
        }
    }
    /* For the sake of butterknife */
    lintOptions {
        disable 'InvalidPackage'
    }
    sourceSets {
        androidTest.setRoot('src/test') // For Robolectric
    }
}
ext {
    androidApiVersion = '19.1.0'
    butterknifeVersion = '4.0.1'
    robotiumVersion = '5.0.1'
}
dependencies {
    compile 'com.android.support:appcompat-v7:' + androidApiVersion
    compile 'com.android.support:support-v4:' + androidApiVersion
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile(project(':core')) {
        exclude group: 'org.apache.httpcomponents'
        exclude group: 'org.json'
    }
    compile fileTree(dir: '../core/libs', include: ['*.jar'])
    apt 'com.jakewharton:butterknife:' + butterknifeVersion
    compile 'com.jakewharton:butterknife:' + butterknifeVersion
    androidTestCompile 'com.jayway.android.robotium:robotium-solo:' + robotiumVersion
    // For Robolectric
    androidTestCompile 'junit:junit:4.11'
    androidTestCompile 'org.robolectric:robolectric:2.3'
    compile 'com.mopub.mobileads:mopub-android:+@aar'
}

// Robolectric: Add this definition in order to only include files that are suffixed with Test.class and set the max heap size
androidTest {
    include '**/*Test.class'
    maxHeapSize = "2048m"
}

调试完robolelectrical source后,我知道我需要创建项目。属性文件。

假设你使用的是Android Studio项目结构,文件的位置是PROJECT_X/app/src/main/project.properties

内容

android.library.reference.1=/../../../build/intermediates/exploded-aar/com.android.support/appcompat-v7/19.1.0

这是因为robolelectric的AndroidManifest.getBaseDir()给了你/path/into/AndroidStudioProjects/PROJECT_X/app/src/main

因此,这个基本目录附加了参考位置。在这种情况下,我可以确保/path/into/AndroidStudioProjects/PROJECT_X/app/src/main + /../../../build/intermediates/exploded-aar/com.android.support/appcompat-v7/19.1.0给我一个包含appcompat库的AndroidManifest.xml的目录。

对我来说,有效的是更新到roboletric:2.4-SNAPSHOT,并确保您的测试类与测试类在同一个"包"中。

我的测试类也需要这个配置注释:

@Config(emulateSdk = 18, reportSdk = 18)

我不需要project.properties文件

dependencies {
    repositories {
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }
    androidTestCompile('org.robolectric:robolectric:2.4-SNAPSHOT') {
        //excludes here
    }
}

此外,我必须确保我在gradle中有以下任务,以避免验证错误(只有半相关)

/**
 * Fixes: https://github.com/robolectric/robolectric/issues/1186
 * Java 7 and 8 have strict compiler verification, this doesn't apply to Android
 * Our error was related to AppCompat not Play Svcs, but it was the same error
 */
tasks.whenTaskAdded { theTask ->
    def taskName = theTask.name.toString()
    if ("testDebug".toString().equals(taskName)) {
        /**
         * Listen for when robolectric adds the 'testDebug' task and when it does, add the -noverify
         * option to that task's jvmArgs.  This allows us to turn off byte code verification when
         * running our unit tests.
         */
        theTask.jvmArgs('-noverify')
    }
}

相关内容

  • 没有找到相关文章

最新更新