【问题标题】:Test APK does not resolve / load classes from main APK ~ Cannot load class 'android.support.v4.os.ParcelableCompatCreatorHoneycombMR2'测试 APK 无法从主 APK 解析/加载类 ~ 无法加载类 'android.support.v4.os.ParcelableCompatCreatorHoneycombMR2'
【发布时间】:2012-08-23 08:46:07
【问题描述】:

我在使用 android maven 插件执行仪器测试时遇到问题。 但是,我已经按照“libraryprojects”示例进行到最后一个细节, 我的设置有一点不同〜我将android支持jar添加为 外部依赖。

我的设置如下:

  • APK lib 项目(具体为greendroid)

    <dependency>
      <groupId>com.google.android</groupId>
      <artifactId>support-v4</artifactId>
      <version>r6</version>
    </dependency>        
    
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <scope>test</scope>
    </dependency>
    
  • APK 项目

    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>greendroid</groupId>
        <artifactId>GreenDroidFragment</artifactId>
        <version>1.0.0-SNAPSHOT</version>   
        <type>apklib</type>                 
    </dependency>
    
  • APK 测试项目(这就是问题所在....)

    <dependency>
          <groupId>com.google.android</groupId>
          <artifactId>support-v4</artifactId>
          <version>r6</version>
                      <scope>provided</scope>
    </dependency>                   
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android-test</artifactId>
            <scope>provided</scope>
        </dependency>
    
        <dependency>
            <groupId>com.jayway.android.robotium</groupId>
            <artifactId>robotium-solo</artifactId>
            <version>3.2.1</version>
        </dependency>
    
        <dependency>
            <groupId>com.**</groupId>
            <artifactId>**</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>apk</type>
            <scope>provided</scope>
        </dependency>
    
        <dependency>
            <groupId>com.**</groupId>
            <artifactId>**</artifactId>
            <version>1.0.0-SNAPSHOT</version>
            <type>jar</type>
            <scope>provided</scope>
        </dependency>  
    

问题是 delvik 在运行检测测试时无法找到或链接支持 jar。

我的问题是,我如何告诉 delvik 将测试 APK 中所需的文件与主应用 APK 进行对比?从理论上讲,将所需依赖项(android-support jar)的范围设置为在测试 APK 中提供应该可以工作,但它不会

以下是我尝试过的一些场景:

  • 通过将 android 支持 jar 打包为 APK 测试项目的一部分,delvik 抱怨它无法解析已编译的代码。这是因为主 APK 的 dex 输出与测试 APK 的 dex 输出不同。

    W/dalvikvm(328): Class resolved by unexpected DEX:      
    Lcom/**/android/**/GenerateOtpFragment;(0x44edffb0):0x121de0 ref   
    [Landroid/support/v4/app/Fragment;] Landroid/support/v4/app/Fragment;(0x44edffb0)
    
  • 由于没有将 android 支持打包为 APK 测试项目的一部分,delvik 无法找到已编译的代码。在提供范围或完全排除依赖项时会发生这种情况。

    W/ClassPathPackageInfoSource(632): Cannot load class. Make sure it is in your apk.
    Class name: 'android.support.v4.os.ParcelableCompatCreatorHoneycombMR2'. 
    Message: android.support.v4.os.ParcelableCompatCreatorHoneycombMR2
    

我唯一能想到的就是以某种方式手动告诉 APK 测试项目使用 delvik 链接机制解决来自主 APK 项目的 android-support 依赖项。

任何想法将不胜感激, -迈克尔

【问题讨论】:

  • 我已经从模拟器设备中提取了主 APK,并确认支持 jar 确实打包在 APK 中。在测试 APK 中设置“提供”范围的支持 jar 后,delvik 仍然无法加载所需的文件......不知何故,由于某些奇怪的原因,测试 APK 没有引用/加载主目录中的所有类APK
  • ok ... 2 周后,我有了第一个重大见解。我编写了一个 ActivityInstrumentationTestCase2 测试,它引用了 GDActivity 作为它的超类。现在,GDActivity 是 FragmentActivity 的子类,它又扩展了 Instrumentation 测试用来执行的 Activity 类。我能够确认 android 支持 jar 确实在测试 APK 的类路径中……这使我得出了这个结论:仪器测试框架此时不支持 greendroid……所以现在我的目标是为片段活动添加测试能力...
  • 更多见解...我能够确定某些库运行良好,而其他库则不然。例如。 spring-android-rest-template 和 commons-lang 库适用于 linrary-main_apk-test_apk 设置。但是,spring-core、spring-context-support 和 android-support v4 不支持。原因是这些工件是使用“非标准编译器”构建的。我在mac上使用jdk6。可能是我正在使用的已安装的 JDK ......所以我现在正在尝试在 Fedora 上构建,看看它是否有什么不同......
  • 在 Fedora linux 上执行产生了相同的结果 ...
  • 新见解 ...我已经确定对于 API 级别

标签: java android maven


【解决方案1】:

我不确定,但我怀疑这可能是一种解释:

Instrumentation tests scanning apk...not an issue

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-03
    • 2012-08-21
    • 2015-04-27
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2011-10-18
    相关资源
    最近更新 更多