如何从Android Studio 2.0 beta 5中的另一个模块导入类?

时间:2016-02-19 17:50:31

标签: android-studio android-gradle build.gradle

我正在尝试从另一个模块(A)导入一个类。还有另一个模块是android app(B)。所以我想把A导入B

settings.gradle

include ':A', ':B'

B build.gradle

  dependencies {
    compile project(':A')   
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'

}

当我尝试进行gradle同步时,它给了我以下错误:

  

错误:与依赖项'org.hamcrest:hamcrest-core'冲突。解决   app(1.1)和test app(1.3)的版本不同。看到   http://g.co/androidstudio/app-test-app-conflict了解详情。

     

错误:与依赖'junit:junit'冲突。已解决的版本   app(4.8.2)和test app(4.12)不同。看到   http://g.co/androidstudio/app-test-app-conflict了解详情。

如果我删除该行:

    compile project(':A')   

gradle同步没有任何错误,但我无法将A导入B。

有人可以告诉我为什么会导致错误吗?

1 个答案:

答案 0 :(得分:7)

我发现模块A gradle文件中已经添加了以下依赖项,因此我不需要在我的B gradle文件中再次添加它们。

我需要删除

testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'

然后gradle sync没有任何错误。

现在我可以将模块A导入B。