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

然後你可以有你的包文件夾(一個或多個)以滿足您的應用程序包

希望,這有助於爲社會!