0

我有一個應用程序,我正在爲它編寫單元測試用例。
我的測試情況給予警告,巨大的名單和1個錯誤
跟着下面的鏈接和子頁Building Instrumented Unit TestsInstrumented Android單元測試和ProGuard

錯誤和一些警告

Warning:there were 10 unresolved references to classes or interfaces. You may need to add missing library jars or update their versions. If your code works fine without the missing classes, you can suppress the warnings with '-dontwarn' options. (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedclass) Warning:there were 5 instances of library classes depending on program classes. You must avoid such dependencies, since the program classes will be processed, while the library classes will remain unchanged. (http://proguard.sourceforge.net/manual/troubleshooting.html#dependency) Warning:there were 3 unresolved references to program class members. Your input classes appear to be inconsistent. You may need to recompile the code. (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember) Warning:Exception while processing task java.io.IOException: Please correct the above warnings first. :app:transformClassesAndResourcesWithProguardForDebugAndroidTest FAILED

Error:Execution failed for task ':app:transformClassesAndResourcesWithProguardForDebugAndroidTest'. java.io.IOException: Please correct the above warnings first.

如果我從調試刪除ProGuard的,我得到64K錯誤!

Error:The number of method references in a .dex file cannot exceed 64K. Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

我在做什麼錯?

應用的build.gradle

android { 
compileSdkVersion 24 
buildToolsVersion "23.0.3" 

defaultConfig { 
    applicationId "com.tyagiabhinav.xyz" 
    minSdkVersion 16 
    targetSdkVersion 24 
    versionCode 1 
    versionName "1.0" 
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
} 
buildTypes { 
    debug { 
     minifyEnabled true 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
    release { 
     minifyEnabled true 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
    } 
} 
dependencies 
{ 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    testCompile 'junit:junit:4.12' 
    compile 'com.android.support:appcompat-v7:24.2.0' 
    compile 'com.android.support:design:24.2.0' 
    compile 'com.android.support:support-annotations:24.2.0' 
    ...... 
    androidTestCompile 'com.android.support:support-annotations:24.2.0' 
    androidTestCompile 'com.android.support.test:runner:0.5' 
    androidTestCompile 'com.android.support.test:rules:0.5' 
} 

測試Java類

package com.tyagiabhinav.xyz; 

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

import com.tyagiabhinav.xyz.Util.PrefHelper; 

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

/** 
* Created by abhinavtyagi on 24/08/16. 
*/ 


@RunWith(AndroidJUnit4.class) 
public class PrefTester { 

    Context mMockContext; 

    private PrefTester() { 
     super(); 
    } 

    @Before 
    public void setUp() { 
     mMockContext = InstrumentationRegistry.getTargetContext(); 
     PrefHelper.init(mMockContext); 
    } 

    @Test 
    public void testSharedPref(){ 
     Assert.assertEquals(false,PrefHelper.isLoggedIn()); 
    } 

} 

回答

0

你對你的調試版本運行ProGuard的。 Proguard可以從java代碼中移除類。通過在build.gradle文件中設置「minifyEnabled false」關閉proguard

buildTypes { 
    debug { 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 
+0

如果我刪除了proguard,我收到64k錯誤。我的代碼不是很大。這是一個小應用程序。但在編寫這個測試用例之後,我在編譯時看到64k錯誤。 –

0

做了幾個更改和問題得到解決!

測試代碼
打開一個閃屏

@RunWith(AndroidJUnit4.class) 
public class PrefTester { 

    Context mMockContext; 

    @Rule 
    public ActivityTestRule<SplashActivity> mActivityRule = new ActivityTestRule<>(SplashActivity.class); 

    @Before 
    public void setUp() { 
     mMockContext = mActivityRule.getActivity().getApplicationContext(); 
     PrefHelper.init(mMockContext); 
    } 

    @Test 
    public void testSharedPref(){ 
     Assert.assertEquals(false,PrefHelper.isLoggedIn()); 
    } 

} 

的build.gradle(APP)在調試
集proguard的假變爲越來越上下文的方式 - >minifyEnabled false
啓用multidex - >multiDexEnabled true
增加依賴關係 - >compile 'com.android.support:multidex:1.0.1'

我在發佈這個問題之前測試了build.gradle方法,我認爲我是以錯誤的方式獲取上下文的。上述變化的組合現在已經適用於我。

0

你在你的proguard-rules.pro文件中有什麼?你可以嘗試刪除它,只需使用默認的Android規則。

作爲一個說明,我認爲你可能想重新評估你的依賴關係。從你顯示的你的gradle文件的片段看來,你不太可能僅僅依靠這些依賴來達到64K的dex限制。你包括更多的庫,你沒有在上面列出?項目中是否存在jar文件中的庫? (這很容易解釋方法的數量)

Multidex是一種破解 - 你真的不應該使用它,除非你沒有選擇。有什麼性能影響/等等,以及一些奇怪的陷阱,可以意外彈出哪些類生活在哪個dex文件。例如 - 從清單中引用的任何類必須以您的主dex文件結尾,這可能會給您帶來奇怪的限制。

+0

它已經解決了......清理了幾次代碼然後重新編譯它。 –

+0

絕對慶幸它是固定的! :-)但是我在這裏回答的理由是指出multidex似乎不是解決您提供的信息的適當方法。 – tmtrademark