I am having trouble testing native applications (such as Contacts and Settings) using Cucumber-JVM for Android with JUnit. Initially, I got the following message:
"Test run failed: Permission Denial: starting instrumentation ComponentInfo???{com.test.contacts/android.test.InstrumentationTestRunner???} from pid=673, uid=673 not allowed because package com.test.contacts does not have a signature matching the target com.android.contacts"
To solve this problem, I signed the test application with the same keys of the Contacts native android application (shared.x509.pem and shared.pk8) and I also added the following line to the AndroidManifest.xml file (as suggested in How can I sign my application with the system signature key?):
“android:sharedUserId="android.uid.shared"
This seemed to solve the problem.
However, after this change, I only manage to run the first test from a test suite. When the second test is running, it gets lost in the getActivity() method from the class ActivityInstrumentationTestCase2, which my steps definitions class extends. More precisely, it doesn't get out of the mSync.wait() call in the method startActivitySync(Intent intent) from Instrumentation.java. The call to getActivity() calls launchActivityWithIntent(), from InstrumentationTestCase.java, which calls startActivitySync(Intent intent).
I found a similar issue on Why does getActivity() block during JUnit test when custom ImageView calls startAnimation(Animation)?, but the workaround described there doesn’t solve my problem.
My test application is really simple and it only checks the content of the buttons in the activity. I don’t have this problem if I use the same test application to test my own apps, only with native android applications such as Contacts and Settings.
Does anyone know something about this issue and could give me a light on how could I solve it?
Thanks in advance
Related
I need to detect at runtime from the code if the application is run using an Instrumented Test. I'm looking for a solution that works without knowing the Test class.
You can read logcat logs in launcher activity of your application under test and find the instrumented test launch command in logs.
I get Crash Analytics from Google for my Android app and I have been seeing this popup once in a while:
java.lang.NoClassDefFoundError
with regards to trying to open an Intent.
The scenario is, user clicks on button, they my code in the app is:
Intent intent = new Intent(nameOfCurrentActivity.this, nameOfNewActivity.class);
startActivity(intent);
Pretty straightforward stuff. It works fine on nearly all devices, including all the ones I own and have tested on. This new class being started isn't unique in that it requires any weird hardware (IE, not a camera activity), but it does access the internet via an Http request.
I have already researched the following links without gaining a hint towards a solution:
Why am I getting a NoClassDefFoundError in Java?
How to solve java.lang.NoClassDefFoundError?
My question is, how is it possible that this exception is being thrown on some devices (IE, a Samsung tablet), but not other devices? Shouldn't a new intent work on all devices if it works on one?
Thanks!
A lot of times this can be caused by classes running code that is dependent upon it being a certain API level, IE Marshmallow, but the device using it is on a previous API and the check for permissions is either ignored or not included; Like when you click disable inspection.
An example would be, say you are running something like a View.OnScrollChangeListener for a recyclerview. If you are coding and set it:
if(Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
myRecyclerview.setOnScrollChangeListener(this);
}
But don't include the build if check, it will throw the error. Another example would be if you are using the class itself as the context (this) for setting the scroll listener. If you use the class itself as the context for extending the Scroll listener but are on a device Pre-API 21, it will throw the error when the class loads. The error it throws is, as you probably guessed, NoClassDefError.
This happens because the Class mentioned in the 'future' API doesn't exist yet in the old phone and so it cannot find the class defined.
Check your code to see if anything in the class is requiring a certain API level to function and if it is, check to confirm you included the if checks for build version. Many times before I have disabled inspection because the red lines were bugging me and forgot to go back and add the checks.
I just wanted to add my own experience today. Apparently Supplier<T> exists in Java 8, however implementation of it and running it on Marshmallow generates NoClassDefFoundError. I was able to solve the problem by declaring custom MySupplier<T>. I tried to add as a comment but failed. Thanks for the explanation given by user7293284 above.
I am developing an Android application with Xamarin.Android.
My application is running perfectly, but when I add GCM Component it gives the following error:
Android application is debugging.
The application could not be started. Ensure that the application has been installed to the target device and has a launchable activity (MainLauncher = true).
Additionally, check Build->Configuration Manager to ensure this project is set to Deploy for this configuration.
If I remove the component then it works fine.
Here is the image for the error:
I have tried many solutions from Google, but nothing has helped.
How can I prevent this error from happening?
You need to make sure that your package name does not start with an uppercase letter - from your screenshot, it looks like it does "RestaurantAPP".
This is a known issue with GCM itself and is not a bug in the Xamarin component: https://code.google.com/p/android/issues/detail?id=37658
I'm new to Android development and just experimented with unit testing inside Android Studio. I have 2 questions,
each time I need to run tests I need to create a "Run/Debug Configurations" for the test class that derives from InstrumentationTestCase, and in this configuration I can only specify 1 test class and 1 test method at a time. Is there a way to break this limitation so I can run a bunch of test classes and their test methods altogether?
I have Log statements in my tests, but I could not find where it logged the output messages to, I checked logcat and event log but didn't find it there.
Thanks.
1) You should be able to right/control click either a test method, or a test class (suite) and run the test from the context menu.
After you run it once, the run config should be, permanently, in the list of runnable configurations, and available in all of the handy ways.
2) The most likely reason that your log messages are not showing up, is that Studio has this annoying habit of automatically installing a filter, on a run, so that it only shows log messages that are from the package of the app you are running. Since your test and your app are in different packages, it is probably filtering some of the messages away.
Since I've started developing an application I haven't received any "Unable to instantiate application" exceptions , however, after the launch there've been quite a number of bug-reports that stated this exception.
My application uses custom MyApplication class inherited from Application, manifest's record looks like .application.MyApplication and I still can't reproduce the same exception neither on 2.3 nor on 4.0. Can you suggest what might be happening there ?
UPD
Thanks.