java.lang.LinkageError:加载器约束违规:解析方法" com.example.demo.utils.R.init(Landroid / content / Context;)V"当前类的类加载器(org / robolectric / internal / bytecode / InstrumentingClassLoader的实例),com / example / test / FirstTest,以及方法'的类加载器(sun / misc / Launcher $ AppClassLoader的实例)。定义类,com / example / demo / utils / R,对于签名中使用的android / content / Context类型有不同的Class对象
测试代码:
@runwith(RobolectricGradleTestRunner.class)
@config(constants = BuildConfig.class,sdk = 21,application = TestApplication.class)
public class FirstTest{
@test
public void testCase01(){
MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);
....
}
}
的build.gradle:
dependencies {
testCompile 'junit:junit:4.12'
testCompile "org.robolectric:robolectric:3.0"
testCompile 'org.robolectric:shadows-httpclient:3.0'
...
}
的src /测试/ JAVA /.../ TestApplication:
public class TestApplication extends Application {
}
COM /示例/演示/ MainActivity:
public class MainActivity extends Activity {
@override
protected void onCreate(Bundle savedInstanceState) {
...
com.example.demo.utils.R.init(getApplicationContext());
...
}
}
COM /示例/演示/ utils的/ R:
public final class R {
public static Resources resources;
private static String packageName;
public static void init(Context paramContext) {
resources = paramContext.getResources();
packageName = paramContext.getPackageName();
}
...
}
答案 0 :(得分:0)
I find the reason why tests run fail is using a class named com.example.demo.utils.R
, which has a conflict with an R
class
autogenerated by the Android System. If you rename R
to another name, like Ra
, it works.