我使用Android Studio将android项目迁移到AndroidX,但是现在使用Robolectric启动单元测试时出现错误:'java.lang.NoClassDefFoundError:无法初始化com.google.android.gms.common类。 GooglePlayServicesUtil' 但是'com.google.android.gms.common.GooglePlayServicesUtil'存在,我可以在文件中使用此导入。
我试图 -将gradle升级到最新版本5.1.1 -清理,删除缓存,重新启动Android Studio -升级Robolectric和地图库
这是我的测试功能:
@Test
public void launchGeographicRestrictionEditionActivity() {
ActivityController<GeographicRestrictionEditionActivity> controller = Robolectric.buildActivity(GeographicRestrictionEditionActivity.class);
geographicRestrictionEditionActivity = controller.create()
.start()
.resume()
.visible()
.get();
GooglePlayServicesUtil.getRemoteContext(null);
Assert.assertNotNull(geographicRestrictionEditionActivity);
}
这是我启动测试时的错误:
java.lang.NoClassDefFoundError: Could not initialize class com.google.android.gms.common.GooglePlayServicesUtil
at com.google.android.gms.maps.internal.zzbz.zza(Unknown Source)
at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$zzb.zzd(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment$zzb.createDelegate(Unknown Source)
at com.google.android.gms.dynamic.DeferredLifecycleHelper.zaa(Unknown Source)
at com.google.android.gms.dynamic.DeferredLifecycleHelper.onInflate(Unknown Source)
at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
at androidx.fragment.app.Fragment.onInflate(Fragment.java:1493)
at androidx.fragment.app.FragmentManagerImpl.onCreateView(FragmentManagerImpl.java:3174)
at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:186)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:772)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at com.deveryware.notico.mapsnew.DeverywareMapFragment.onCreateView(DeverywareMapFragment.java:148)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2612)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:874)
at androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1228)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1293)
at androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2646)
at androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2600)
at androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
at androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
at androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at com.deveryware.notico.widget.DeverywareActivity.onStart(DeverywareActivity.java:159)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
at androidx.test.runner.MonitoringInstrumentation.callActivityOnStart(MonitoringInstrumentation.java:686)
at android.app.Activity.performStart(Activity.java:7029)
at org.robolectric.util.ReflectionHelpers$6.run(ReflectionHelpers.java:247)
at org.robolectric.util.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:399)
at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:241)
at org.robolectric.android.controller.ComponentController$1.run(ComponentController.java:53)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:396)
at org.robolectric.android.controller.ComponentController.invokeWhilePaused(ComponentController.java:50)
at org.robolectric.android.controller.ActivityController.start(ActivityController.java:126)
at com.deveryware.notico.myUnitTests.ui.map.MapFragmentTests.launchGeographicRestrictionEditionActivity(MapFragmentTests.java:45)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:256)
at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
这是我的项目中的代码,这会导致问题:充气机
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
mView = inflater.inflate(getMapLayoutResId(), container, false);
.......
我想在没有出现此错误的情况下启动测试。