4

我正在尝试为我的应用程序添加loginfacebook。但是当我添加一个需要这样做的存储库时。它导致了一个错误。 AndroidJUnit4现在无法解析。无法解析符号AndroidJUnit4

ExampleInstrumentedTest.java

package com.example.user.enyatravelbataan; 

import android.content.Context; 
import android.support.test.InstrumentationRegistry; 
import android.support.test.runner.AndroidJUnit4; 

import org.junit.Test; 
import org.junit.runner.RunWith; 

import static junit.framework.Assert.assertEquals; 
import static org.junit.Assert.*; 

/** 
* Instrumentation test, which will execute on an Android device. 
* 
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a> 
*/ 
@RunWith(AndroidJUnit4.class) 
public class ExampleInstrumentedTest { 
@Test 
public void useAppContext() throws Exception { 
    // Context of the app under test. 
    Context appContext = InstrumentationRegistry.getTargetContext(); 

    assertEquals("com.example.user.enyatravelbataan", 
appContext.getPackageName()); 
} 
} 


apply plugin: 'com.android.application' 

,这是我的构建:gradle这个(APP)

android { 
compileSdkVersion 24 
buildToolsVersion "24.0.3" 
useLibrary 'org.apache.http.legacy' 


repositories { 
    mavenCentral() 
} 

defaultConfig { 
    applicationId "com.example.user.enyatravelbataan" 
    minSdkVersion 16 
    targetSdkVersion 24 
    versionCode 1 
    versionName "1.0" 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    multiDexEnabled true 
} 
buildTypes { 
    release { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
} 
} 

dependencies { 
compile fileTree(include: ['*.jar'], dir: 'libs') 
compile(name: 'wikitudesdk', ext: 'aar') 
// compile 'org.apache.httpcomponents:httpclient:4.5' 
// compile 'org.apache.httpcomponents:httpcore:4.4.1' 
compile files('libs/MD5Simply.jar') 
compile files('libs/GenAsync.1.2.jar') 
compile 'com.facebook.android:facebook-android-sdk:[4,5)' 
compile 'com.android.support:appcompat-v7:24.2.1' 
compile 'com.android.support:multidex:1.0.0' 
compile 'com.google.android.gms:play-services:9.8.0' 
compile 'com.google.android.gms:play-services-location:9.8.0' 
compile 'com.google.android.gms:play-services-appindexing:9.8.0' 
compile 'com.android.support:cardview-v7:24.2.1' 
compile 'com.squareup.picasso:picasso:2.5.2' 
compile 'com.android.support:design:24.2.1' 
compile 'com.android.volley:volley:1.0.0' 
compile 'com.android.support:support-v4:24.2.1' 

testCompile 'junit:junit:4.12' 
} 

repositories { 
flatDir { 
    dirs 'libs' 
} 
} 
apply plugin: 'com.google.gms.google-services' 

回答

4

尝试

androidTestCompile 'com.android.support:support-annotations:23.1.0' 
androidTestCompile 'com.android.support.test:runner:0.4.1' 
androidTestCompile 'com.android.support.test:rules:0.4.1' 

上面添加以下相关部分

configurations.all { 
    resolutionStrategy.force 'com.android.support:support-annotations:23.1.0' 
} 
+0

我试过了,但它显示一个错误:警告:与依赖'com.android.support:support-annotations'冲突。应用程序(25.0.0)和测试应用程序(23.1.1)的已解决版本不同。有关详细信息,请参阅http://g.co/androidstudio/app-test-app-conflict。 –

+0

已更新的答案。 http://stackoverflow.com/questions/33317555/conflict-with-dependency-com-android-supportsupport-annotations-resolved-ver – Pehlaj

+0

它现在的作品。谢谢 :) –

0

除了上面的回答,请确保您按照这些步骤严格:

我敲我的头撞在墙上和/或其它对象我在我的面前,为了使我的SQLite的功能单元见测试。无论我做什么,如何严格遵循这些建议,我的许多智者都无法通过互联网工作。

然后我不得不去幼儿园,并开始了解我的愚蠢的错误。我了解到,AndroidJUnit4只能用于Instrumentation Test,而JUnit必须用于本地测试。也就是说,该文件夹必须是src/androidTest/java。我有我的测试类直接在androidTest文件夹下,因此我不得不面对那个讨厌的错误。不过,现在我把它移动到src/androidTest/java的一瞬间,一切都变得非常清晰,就像“我现在可以清楚地看到雨不见了”。

Take a look at this article它说...

Run Instrumented Unit Tests To run your instrumented tests, follow these steps:

Be sure your project is synchronized with Gradle by clicking Sync Project in the toolbar. Run your test in one of the following ways: To run a single test, open the Project window, and then right-click a test and click Run . To test all methods in a class, right-click a class or method in the test file and click Run . To run all tests in a directory, right-click on the directory and select Run tests . The Android Plugin for Gradle compiles the instrumented test code located in the default directory (src/androidTest/java/), builds a test APK and production APK, installs both APKs on the connected device or emulator, and runs the tests. Android Studio then displays the results of the instrumented test execution in the Run window.

因此乡亲,为仪器测试的文件夹必须是(不要忘记的情况下)

src/androidTest/java

本地测试该文件夹必须是

src/test/java

然后你可以有你的包文件夹(一个或多个)以满足您的应用程序包

希望,这有助于为社会!