对具有默认可见性的方法进行的仪器测试失败,并显示“找不到引用的方法”

时间:2018-08-27 14:07:38

标签: android android-espresso android-proguard

我编写了一个简单的Android应用程序,并为此进行了测试。我不明白为什么它会失败:

  

警告:com.sample.FooTest:找不到引用的方法   'android.content.Intent   getIntent(android.content.Context,java.lang.Class,java.lang.String)'   在库类com.sample.Foo中警告:有1个未解决   对库类成员的引用。            您可能需要更新库版本。            (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedlibraryclassmember

这是我的代码:

MainActivity

package com.sample

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val foo = Foo()
        val i = foo.getIntent(this, MainActivity::class.java, "aaa")
        Log.d("Tag", "foo=$i")
    }
}

Foo

package com.sample;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;

public class Foo {

    Intent getIntent(@NonNull final Context context, @NonNull Class<?> clazz, @NonNull final String action) {
        return new Intent(context, clazz).setAction(action);
    }
}

build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.2.60'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.4'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

app / build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {

    signingConfigs {
        ReleaseSigningConfig {
            keyAlias 'INSERT YOURS'
            keyPassword 'INSERT YOURS'
            storeFile file('INSERT YOURS')
            storePassword 'INSERT YOURS'
        }
    }

    compileSdkVersion 28
    testBuildType "release"
    buildToolsVersion '28.0.2'
    defaultConfig {
        applicationId "com.sample"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            signingConfig signingConfigs.ReleaseSigningConfig
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            testProguardFiles(getDefaultProguardFile('proguard-android.txt'), 'proguard-rules-test.pro')
        }
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0-rc01'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

app / src / androidTest / java / com / sample / FooTest.java

package com.sample;

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

import com.sample.Foo;
import com.sample.MainActivity;

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

import static org.junit.Assert.assertNotNull;

@RunWith(AndroidJUnit4.class)
public class FooTest {

    private Foo mFoo;
    private Context mContext;

    @Before
    public void setUp() {
        mFoo = new Foo();
        mContext = InstrumentationRegistry.getTargetContext();
    }

    @Test
    public void getIntent() {
        final Intent i = mFoo.getIntent(mContext, MainActivity.class, "action");
        assertNotNull(i);
    }
}

app / proguard-rules-test.pro

-dontwarn org.xmlpull.v1.**

当我公开Foo#getIntent()时,测试通过。为什么会这样?

0 个答案:

没有答案