gpt4 book ai didi

java - NoClassDefFoundError : . .. 将 Robolectric 与 Android 库 (AAR) 结合使用时 R$styleable

转载 作者:太空狗 更新时间:2023-10-29 14:09:26 26 4
gpt4 key购买 nike

我们正在开发一个提供 Activity 的库,我希望能够对该 Activity 执行一些功能测试。

我的 build.gradle 包括:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'

testCompile 'org.mockito:mockito-core:1.+'
testCompile 'junit:junit:4.12'
testCompile('org.robolectric:robolectric:3.0-rc2') {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
testCompile('org.robolectric:shadows-httpclient:3.0-rc2') {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
testCompile('org.robolectric:shadows-support-v4:3.0-rc2') {
exclude group: 'commons-logging', module: 'commons-logging'
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
}

我的 LibraryActivityTest 包括:

@RunWith(RobolectricGradleTestRunner.class)
public class LibraryActivityTest {

@Test
public void testOnCreate() throws Exception {
LibraryActivity libraryActivity = Robolectric.buildActivity(LibraryActivity.class).create().get();


}
}

MyRobolectricGradleTestRunner 覆盖 getAppManifest 以恢复指定特定于测试的 AndroidManifest.xml 的能力,而不是使用应用程序版本。

当我尝试从 Android Studio 或 gradle 命令行运行 testOnCreate 时,我收到 android/support/v7/appcompat/R$styleable 的 class def not found 错误>:

android/support/v7/appcompat/R$styleable
java.lang.NoClassDefFoundError: android/support/v7/appcompat/R$styleable
at android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.java:109)
at android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.java:146)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:59)
at com.moxiesoft.mylibrary.LibraryActivity.onCreate(LibraryActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5133)
at org.robolectric.util.ReflectionHelpers$3.run(ReflectionHelpers.java:144)
at org.robolectric.util.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:241)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:138)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:122)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:309)
at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:47)
at org.robolectric.util.ActivityController.create(ActivityController.java:118)
at org.robolectric.util.ActivityController.create(ActivityController.java:129)
at com.moxiesoft.mylibrary.LibraryActivityTest.testOnCreate(LibraryActivityTest.java:20)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:235)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:168)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:360)
at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:64)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

同样值得注意的是,安装和配置似乎是合理的,因为不涉及 ActivityController.create 的测试似乎工作正常。

我该如何解决这个错误?

最佳答案

所以问题的根源似乎是 gradle 如何处理(或不处理)创建 android/support/v7/appcompat/R.java对于 com.android.library模块。

线索来自here通过将这个 block 放在我的 build.gradle 中,我能够让命令行和 Android 都工作。

def package_namespace = "com.moxiesoft.netagent.androidchatcustomerclient"
def package_namespace_path = package_namespace.replaceAll("\\.", "/")

afterEvaluate { project ->
android.libraryVariants.each { variant ->
// workaround for missing R class for aar dependencies
def copyTaskName = "copy${variant.name.capitalize()}AppCompat"
task(copyTaskName, type:Copy) {
dependsOn "process${variant.name.capitalize()}Resources"
from "build/generated/source/r/${variant.name}/$package_namespace_path"
// into "build/generated/source/r/${variant.name}/android/support/v7/appcompat"
into "src/test/java/android/support/v7/appcompat"
include 'R.java'
filter { line -> line.contains("package ${package_namespace};") ? 'package android.support.v7.appcompat;' : line }
outputs.upToDateWhen { false }
}
tasks.getByName("compile${variant.name.capitalize()}UnitTestJava") dependsOn copyTaskName
}
}

基本上它只是将生成的应用程序 R.java 文件复制到测试构建文件夹中,以便在测试编译阶段使用它。我尝试将副本放入生成的源文件夹中,但测试编译阶段似乎没有在那里提取它。

关于java - NoClassDefFoundError : . .. 将 Robolectric 与 Android 库 (AAR) 结合使用时 R$styleable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30177171/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com