java Gradle 导入 Powermock 和 Mockito

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26487737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:00:11  来源:igfitidea点击:

Gradle import Powermock and Mockito

javaandroidgradlemockitopowermock

提问by Jemil Riahi

It feels like i have tried everything. Every time i get the import to work without any compiler error there is missing some classes. I imported so i had everything but the PowerMockRunner.classand i do not know how i would use powerMock without it

感觉我什么都试过了。每次我在没有任何编译器错误的情况下导入工作时,都会丢失一些类。我导入了所以我拥有所有但PowerMockRunner.class我不知道如果没有它我将如何使用 powerMock

Here is my gradle file as it looks like right now

这是我现在的 gradle 文件

apply plugin: 'android'                                                                               

dependencies {                                                                                        
    compile fileTree(dir: 'libs', include: '*.jar')                                                   
    compile project(':google-play-services_lib')                                                      
    compile project(':facebook-sdk_lib')                                                              
    compile 'de.greenrobot:greendao:1.3.7'                                                            

    compile ('com.google.dexmaker:dexmaker:1.1')                                                      
    compile ('com.google.dexmaker:dexmaker-mockito:1.1'){                                             
        exclude group: 'org.hamcrest'                                                                 
        exclude group: 'org.javassist'                                                                
    }                                                                                                 

    compile('junit:junit:4.4'){                                                                       
       exclude group: 'org.hamcrest'                                                                  
    }                                                                                                 

    compile('org.mockito:mockito-core:1.9.5'){                                                        
        exclude group: 'org.hamcrest'                                                                 
    }                                                                                                 

    compile('org.powermock:powermock-api-mockito:1.4.12'){                                            
       exclude group: 'org.hamcrest'                                                                  
    }                                                                                                                                                                                                                                                                                                

}                                                                                                     

android {                                                                                             

    packagingOptions {                                                                                
        exclude 'META-INF/ASL2.0'                                                                     
        exclude 'META-INF/LICENSE'                                                                    
        exclude 'META-INF/LICENSE.txt'                                                                
        exclude 'META-INF/NOTICE'                                                                     
        exclude 'META-INF/NOTICE.txt'                                                                 


    }                                                                                                 

    compileSdkVersion 19                                                                              
    buildToolsVersion "19.1.0"                                                                        

    lintOptions {                                                                                     
        abortOnError false                                                                            
    }                                                                                                 

    sourceSets {                                                                                      
        main {                                                                                        
            manifest.srcFile 'AndroidManifest.xml'                                                    
            java {                                                                                    
                srcDir 'src'                                                                          
                srcDir 'src-gen'                                                                      
                srcDir 'tests'                                                                        
            }                                                                                         
            resources {                                                                               
                srcDir 'src'                                                                          
                srcDir 'src-gen'                                                                      
            }                                                                                         
            aidl.srcDirs = ['src','src-gen']                                                          
            renderscript.srcDirs = ['src','src-gen']                                                  
            res.srcDirs = ['res']                                                                     
            assets.srcDirs = ['assets']                                                               
        }                                                                                             
        androidTest.setRoot('tests')                                                                  

        debug.setRoot('build-types/debug')                                                            
        release.setRoot('build-types/release')                                                        
    }                                                                                                 
}  

Here is the compiler error that i get thrown into my face all day.

这是编译器错误,我整天都在面对它。

UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Multiple dex files define Lorg/hamcrest/Description; at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594) at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552) at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:533) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170) at com.android.dx.merge.DexMerger.merge(DexMerger.java:188) at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439) at com.android.dx.command.dexer.Main.runMonoDex(Main.java:287) at com.android.dx.command.dexer.Main.run(Main.java:230) at com.android.dx.command.dexer.Main.main(Main.java:199) at com.android.dx.command.Main.main(Main.java:103)

意外的顶级异常:com.android.dex.DexException:多个 dex 文件定义了 Lorg/hamcrest/Description;在 com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:594) 在 com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:552) 在 com.android.dx.merge.DexMerger.mergeClassDefs (DexMerger.java:533) at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:170) at com.android.dx.merge.DexMerger.merge(DexMerger.java:188) at com.android。 dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:439) 在 com.android.dx.command.dexer.Main.runMonoDex(Main.java:287) 在 com.android.dx.command.dexer.Main。在 com.android.dx.command.dexer.Main.main(Main.java:199) 在 com.android.dx.command.Main.main(Main.java:103) 运行(Main.java:230)

回答by Jemil Riahi

I sat all day yesterday figuring out how i should fix this issue. It is now solved and this is what i did with my gradle.buildfile to fix the issue

我昨天坐了一整天,想知道我应该如何解决这个问题。现在已经解决了,这就是我用我的gradle.build文件来解决这个问题的方法

apply plugin: 'android'                                                                              

repositories {                                                                                       
    mavenCentral()                                                                                   
}                                                                                                    

dependencies {                                                                                       
    compile fileTree(dir: 'libs', include: '*.jar')                                                  
    compile project(':google-play-services_lib')                                                     
    compile project(':facebook-sdk_lib')                                                             


    compile ('de.greenrobot:greendao:1.3.7'){                                                        
       exclude group: 'org.hamcrest'                                                                 
    }                                                                                                

    androidTestCompile('org.mockito:mockito-core:1.9.5'){                                            
        exclude group: 'org.hamcrest'                                                                
    }                                                                                                

    androidTestCompile('org.powermock:powermock-module-junit4:1.5'){                                 
        exclude group: 'org.hamcrest'                                                                
    }                                                                                                
}                                                                                                    

android {                                                                                            

    packagingOptions {                                                                               
        exclude 'META-INF/ASL2.0'                                                                    
        exclude 'META-INF/LICENSE'                                                                   
        exclude 'META-INF/LICENSE.txt'                                                               
        exclude 'META-INF/NOTICE'                                                                    
        exclude 'META-INF/NOTICE.txt'                                                                
    }                                                                                                

    compileSdkVersion 19                                                                             
    buildToolsVersion "19.1.0"                                                                       

    lintOptions {                                                                                    
        abortOnError false                                                                           
    }                                                                                                

    sourceSets {                                                                                     
        main                                                                                         
        {                                                                                            
            manifest.srcFile 'AndroidManifest.xml'                                                   
            resources                                                                                
            {                                                                                        
                srcDir 'src-gen'                                                                     
            }                                                                                        
            aidl.srcDirs = ['src/main/java', 'src-gen']                                              
            renderscript.srcDirs = ['src/main/java', 'src-gen']                                      
            res.srcDirs = ['res']                                                                    
            assets.srcDirs = ['assets']                                                              
            java.srcDirs = ['src/main/java', 'src/main/java/', 'src-gen']                            
        }                                                                                            

        androidTest {                                                                                
            java.srcDirs = ['tests']                                                                 
        }                                                                                            


        unitTest {                                                                                   
            java.srcDir file('tests')                                                                
            resources.srcDir file('tests/res')                                                       
        }                                                                                            


        debug.setRoot('build-types/debug')                                                           
        release.setRoot('build-types/release')                                                       
    }//-End of sourceSet                                                                             
}  //-End of android    

Hope this can help at least one person to not get stuck in the same gradle failure loop i went thru.

希望这可以帮助至少一个人不会陷入我经历的同一个 gradle 失败循环中。