Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android-gradle – 在Android Studio和AppCompat中使用Robolectric时找不到StyleData的父级大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 Android项目,它使用:

> Android studio Gradle结构
> Android AppCompat库
> Robolectric进行测试

不涉及任何活动的测试(例如,断言1 == 1),工作正常.但是,当我使用activity时会产生以下错误

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,但它创造了相同的结果.

供参

这是我的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"
}

解决方法

在调试Robolectric源之后,我知道我需要创建project.properties文件.

假设您使用的是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

是因为Robolectric的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的目录.

大佬总结

以上是大佬教程为你收集整理的android-gradle – 在Android Studio和AppCompat中使用Robolectric时找不到StyleData的父级全部内容,希望文章能够帮你解决android-gradle – 在Android Studio和AppCompat中使用Robolectric时找不到StyleData的父级所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。