Java – Appium + Android Studio 3.0 + Java 8 cannot be used

Appium + Android Studio 3.0 + Java 8 cannot be used… here is a solution to the problem.

Appium + Android Studio 3.0 + Java 8 cannot be used

I’m trying my Android UI test using the appium java client. However, I can’t get it to run. Here is my build.gradle and my error message.

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
defaultConfig {
    applicationId "com.example.wpjtest2"
    minSdkVersion 26
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
compileOptions {
    sourceCompatibility JavaVersion.VERSION_1_8
    targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'
androidTestImplementation 'io.appium:java-client:5.0.4'
}

Mistake:

Information:Gradle tasks [:app:assembleDebug, :app:assembleDebugAndroidTest]
Error:java.lang.IllegalAccessException: no such method:     org.springframework.core.io.buffer.DataBufferUtils.lambda$read$0(ReadableByteChannel)ReadableByteChannel/invokeStatic
Error:java.lang.NoClassDefFoundError: org/reactivestreams/Publisher
Error:java.lang.ClassNotFoundException: Class org.reactivestreams.Publisher not found
Error:java.nio.file.DirectoryNotEmptyException: C:\Users\zil\AppData\Local\Temp\lambdas5516872364251960030\org\springframework\core\io
Error:java.lang.IllegalAccessException: no such method: org.springframework.beans.factory.config.YamlMapFactoryBean.lambda$createMap$0(Map,Properties,Map)void/invokeSpecial
Error:java.lang.NoClassDefFoundError: org/yaml/snakeyaml/reader/UnicodeReader
Error:java.lang.ClassNotFoundException: Class org.yaml.snakeyaml.reader.UnicodeReader not found
Error:Execution failed for task     ':app:transformClassesWithDesugarForDebugAndroidTest'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: Error while executing java process with main class com.google.devtools.build.android.desugar.Desugar with arguments {@C:\Users\xxx\project\WPJTest2\app\build\intermediates\tmp\desugar_args221997254795871866}
Information:BUILD FAILED in 15s
Error:java.lang.ClassNotFoundException: Class javax.validation.Validator not found
Information:9 errors
Information:0 warnings
Information:See complete output in console

Can anyone check if I have correct and sufficient dependencies? There are many tutorials on adding Appium, but none of them worked for me.

On the other hand, if I can integrate with Appium using Java 7, what should I do?

Best Solution

While Appium is well-documented and versatile, the Android Studio portfolio of work took me a day. In my case, it’s an alternative to Espresso because it doesn’t currently support multi-feature testing for Instant Apps .

  1. Install Appium Server as UI .
  2. Set up an Appium server for your localhost:
    • Host: 127.0.0.1 Port: 4723
    • Edit Configuration -> Set ANDROID_HOME and JAVA_HOME paths.
    • Start the server. You’ll see a console to keep this window open.
      Start Appium Server
  3. Run the Android Emulator from Android Studio.
  4. Start the Appium session for your emulator in the Appium UI (File -> New Session window):
    Start Appium Session
  5. If everything looks fine and the console does not display log errors, the Appium Inspector window opens. In addition, your app will run in the emulator. In the Inspector, find the element by clicking the screenshot. Record your actin with the buttons at the top and get the auto-generated code:
    enter image description here
  6. Add libraries to your projectapp ormy_feature gradle file. Appium and Selenium versions should take care to avoid “no such method error” – see an answer :

    dependencies {
      androidTestImplementation 'junit:junit:4.12'
      androidTestImplementation 'io.appium:java-client:5.0.1'
      androidTestImplementation 'org.seleniumhq.selenium:selenium-java:3.4.0'
    }
    
  7. Create a JUnit functional test class in your Android project .../src/androidTest/java/ folder. You can also use a simple JUnit test wrapper. For testing purposes, you can use main methods in your Android project to create a separate Java project or Java class, but it is more convenient to integrate Appium testing into the Android Studio testing feature. Also check that you haven’t imported other test libraries or mixed it in your code (e.g., @Test can be used for TestNG and JUnit). Use code automatically generated by Appium Inspector in your methods, such as:

        package com.example.my_project;
    
        import org.junit.After;
        import org.junit.Before;
        import org.junit.Test;
        import org.openqa.selenium.remote.DesiredCapabilities;
    
        import java.net.MalformedURLException;
        import java.net.URL;
    
        import io.appium.java_client.MobileElement;
        import io.appium.java_client.android.AndroidDriver;
    
        public class SampleTest {
    
          private AndroidDriver<MobileElement> driver;
    
          @Test
          public void testPlan()
            throws MalformedURLException {
    
            setUp();
    
            testWorkUnit_WithCertainState_ShouldDoSomething();
    
            abotherTestWorkUnit_WithCertainState_ShouldDoSomething();
    
            tearDown();
    
          }
    
          public void setUp()
            throws MalformedURLException {
    
            DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
            URL remoteUrl = new URL("http://localhost:4723/wd/hub");
    
            desiredCapabilities.setCapability(
              "platformName",
              "Android");
    
            desiredCapabilities.setCapability(
              "deviceName",
              "Android Emulator");
    
            desiredCapabilities.setCapability(
              "appPackage",
              "com.example.my_project");
    
            desiredCapabilities.setCapability(
              "appActivity",
              "com.example.my_project.MyActivity");
    
            driver =
              new AndroidDriver<>(
                remoteUrl,
                desiredCapabilities);
          }
    
          public void testWorkUnit_WithCertainState_ShouldDoSomething() {
    
            MobileElement el1 = 
             driver.findElementById(
               "com.example.my_project:id/urlField");
    
            el1.sendKeys("example.com");
          }
    
          public void tearDown() {
    
            driver.quit();
          }
        }
    

As for comments in the code:@Before and for before and after for each test case in the class (@BeforeClass and need to be static and@After@AfterClass inconvenient). So it restarts the application every time and it is not convenient to do link testing. So it is better to have a method with@Test comments, it will call other uncommented methods to set, do the test cases in the required order and complete.

Related Problems and Solutions