简体   繁体   中英

Gradle import Powermock and Mockito

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.class and i do not know how i would use powerMock without it

Here is my gradle file as it looks like right now

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)

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.build file to fix the issue

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM