gpt4 book ai didi

java - Robolectric Resources$NotFoundException 找不到库 (aar) 资源

转载 作者:行者123 更新时间:2023-12-04 15:04:54 24 4
gpt4 key购买 nike

我已经为这个(几天)寻找了很多解决方案,但还没有找到解决方案。基本上,对于我使用 Robolectric 的单元测试,我在尝试从依赖项(作为 aar 拉入)中扩充资源时不断收到 Resources$NotFoundException。这是设置:

build.gradle(项目级别)

// Top Level
buildscript {
apply from: "${System.getenv('MY_REPOS')}", to: buildscript
println("REPOS: ${repositories.names}")
dependencies {
classpath "com.android.tools.build:gradle:3.3.2"
}
}

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

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

build.gradle(应用程序)

plugins {
id 'com.android.application'
}

android {
compileSdkVersion 28

defaultConfig {
applicationId "com.me.myapplication"
minSdkVersion 18
targetSdkVersion 28
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

testOptions {
animationsDisabled true
unitTests {
includeAndroidResources = true
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {

implementation 'com.me.android:thewidget:4.0.0' // pulled from private repo

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.navigation:navigation-fragment:2.2.2'
implementation 'androidx.navigation:navigation-ui:2.2.2'

androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation("com.google.guava:guava:27.0-jre") {
exclude group: 'com.google.guava', module: 'failureaccess'
}

//UnitTests
testImplementation 'junit:junit:4.13'
testImplementation 'org.robolectric:robolectric:4.3.1'
testImplementation 'org.robolectric:android-all:9-robolectric-4913185-2'
}

我在我的 gradle-wrapper.properties 中使用 distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip

和我的 gradle 属性:

org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
android.useAndroidX=true
android.enableJetifier=true
android.enableUnitTestBinaryResources = true //tried with and without this set
android.enableAapt2 = false // same here with and without this here

然后是 Android Studio 4.1.2 中的默认空白 Activity 模板。所以唯一触及的是主要的主要 Activity :

package com.me.myapplication;

import android.os.Bundle;

import com.me.thewidget.MyOtherView;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.view.View;

import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);



FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

public void testDialogSetup() {
MyOtherView myView = new MyOtherView(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

所以问题出在 MyOtherView 以及它在此类中膨胀 View 时。所以在 MyOtherView 中,它在遇到这一行时在测试中抛出异常:

...
inflater.inflate(layout.my_view, this);
...

至于测试本身:

package com.me.myapplication;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;

import static org.junit.Assert.*;

/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(RobolectricTestRunner.class)
public class ExampleUnitTest {

MainActivity activity;

@Before
public void setup() {
this.activity = Robolectric.buildActivity(MainActivity.class).get();
}

@Test
public void simpleTest() {
activity.testDialogSetup(); // not doing anything yet since it throws the error here
}
}

我尝试过使无效/重新启动、清除缓存,我尝试过不同版本的 gradle 构建工具和分发版。我阅读的所有内容都显示将 includeAndroidResources 标志设置为 true 但我有,并且我已经尝试过使用/不使用 enableUnitTestBinaryResources 以及 enableAapt2 ...

感谢任何帮助,因为我一直在努力让这个工作正常进行。我会认为它很简单但是......我在这里迷路了。它就像它根本看不到依赖项 (com.me.android:thewidget:4.0.0, MyOtherView) 资源。

最佳答案

好吧,我想出来了(实际上是一位同事做的)。我将 robolectric.properties 文件添加到 src/test/resources 区域。在我必须放入的文件中:

sdk=28
qualifiers=w1004dp-h654dp

我希望这对以后的人有所帮助。

关于java - Robolectric Resources$NotFoundException 找不到库 (aar) 资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66359009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com