How to configure Appium and Android Studio? - android-studio

Hi am very new in appium , I have run my first test case (Java with TestNG) using eclipse but now i want switch to Android Studio. Please provide me step by step process to add jar's and other things into Android Studio

I recommend you to use IntelliJ IDEA. You can create project with gradle where you can insert dependencies, but you can include easly .jars by File -> Project structure -> Libraries -> (+) -> Java and then select your .jar file.
IntelliJ IDEA got testNG already installed so you don't have to install it.
For testing all you need are gson-2.2.2, java-client-4.0.0, selenium-java-2.53.1 jars.
To connect with your device use class
public class Setup {
private final String DEVICE_NAME = "deviceName";
private final String PLATFORM_NAME = "platformName";
private final String PLATFORM_VERSION = "platformVersion";
private final String APP_PACKAGE = "appPackage";
private final String APP_ACTIVITY = "appActivity";
private String deviceName = "Android SDK built for x86"; //device name can be found in device settings
private String platformName = "Android";
private String platformVersion = "6.0"; //version of your android
private String port = "4723"; //port from Appium server
private String url;
private String getIp() throws UnknownHostException {
InetAddress ip = InetAddress.getLocalHost();
return ip.getHostAddress();
}
public AndroidDriver establishConnection() throws MalformedURLException {
try {
url = String.format("http://%s:%s/wd/hub", getIp(), port);
} catch (UnknownHostException e) {
e.printStackTrace();
}
DesiredCapabilities capability = new DesiredCapabilities();
capability.setCapability(DEVICE_NAME, deviceName);
capability.setCapability(PLATFORM_NAME, platformName);
capability.setCapability(PLATFORM_VERSION, platformVersion);
capability.setCapability(APP_PACKAGE, "my.app.package");
capability.setCapability(APP_ACTIVITY, "my.app.activity");
return new AndroidDriver(new URL(url), capability);
}
}
After this you can create new class with #BeforeClass where you can create object of Setup class, call establishConnection(); and initialize driver and test your app UI with #Test methods. Don't forget to install .apk first on your device :)

You need for Appium setup in Android Studio -
Appium Server
Appium Java Client jar
Selenium client jars
You can view this Video Step By Step Appium Setup with Android Studio
Regards,
Anuja

Android Studio 3.1.2, Min SDK 21. If Min SDK lower, you can try using a lower version of appium java-client.
Add Java client Jar through gradle in the your gradle build file dependencies section. Make sure that the
client version you pick is usable for your minimum/target android SDKs.
dependencies {
testImplementation 'io.appium:java-client:4.1.2'
}
Your android project has a java folder for your source files. Search for a folder that has the word (test) in parentheses. Create a java file in there. If you place it in the (androidTest) folder, you will run into various problems.
Add your unit test code to that file. In my case I was using JUnit.
Install appium server through your preferred means. You can find install executables through https://github.com/appium/appium-desktop/releases/ or you can dabble with nodejs and its package manager.
Configure appium server's settings to match those of your unit test (i.e the ports should match).
Start appium server.
Right click on your test and click the Run 'XXXXX' where XXXXX is the name of your class.
Select the emulator/physical device you desire.
There is no need to manually download jars and add them as I was seeing in some tutorials. If you follow the steps above, you'll have what you need.

I did all the steps but I rebuild wasn't successful, then I got this as a solution:
the android {...} closure in the build.gradle for your app module to resolve the issue:
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/NOTICE'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
}
and all the errors are solved. my java version was 10 and it didn't work!!

Related

Android Studio transformClassesWithMultidexlistForDebug RemObjects library

After added one more library to project and enabled multiDex, Android studio is started showing this error:
Error:java.lang.RuntimeException: java.io.IOException: Can't read [MyApp\libs\com.remobjects.sdk.jar(;;;;;;**.class)] (Can't process class [com/remobjects/sdk/helpers/Logger.class] (Method must be overridden in [proguard.classfile.attribute.SourceFileAttribute] if ever called))
Error:java.io.IOException: Can't read [MyApp\libs\com.remobjects.sdk.jar(;;;;;;**.class)] (Can't process class [com/remobjects/sdk/helpers/Logger.class] (Method must be overridden in [proguard.classfile.attribute.SourceFileAttribute] if ever called))
Error:java.io.IOException: Can't process class [com/remobjects/sdk/helpers/Logger.class] (Method must be overridden in [proguard.classfile.attribute.SourceFileAttribute] if ever called)
Error:java.lang.UnsupportedOperationException: Method must be overridden in [proguard.classfile.attribute.SourceFileAttribute] if ever called
gradle console:
:MyApp:transformClassesWithDexBuilderForDebug
:MyApp:transformClassesWithMultidexlistForDebug FAILED
build.gradle:
defaultConfig {
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
signingConfig signingConfigs.config
}
debug {
minifyEnabled false
signingConfig signingConfigs.config
}
}
buildscript:
classpath 'com.android.tools.build:gradle:3.0.1'
I'm working on three applications and only this one is problematic. One app is only upgraded version of other and upgraded app generating apk file sucessfully. I tried to copy gradle file and edit it, but without any luck.
I already tried editing proguard.cfg with all possible commands I found on internet but error is still there.
Tomorrow I have to publish new verison and I can't build APK..
I found what was wrong.
Minimal SDK version in Manifest was set to 15 and after I changed it to 21, APK was successfully generated.

How to show full compile error messages info in Checker FrameWork with line numbers etc

I just started using Checker Framework and have a problem that is exactly reproducible on one of the example projects from authors of this framework. This project is available here:
https://github.com/typetools/checker-framework/tree/master/docs/examples/GradleExamples
When i run this command from root:
>gradle compileJava
i receive this compilation output:
public static /*#Nullable*/ Object nullable = null;
^
required: #Initialized #NonNull Object
list.add(null); // error on this line
^
required: #Initialized #NonNull String
2 errors
:compileJava FAILED
As you can see there is no any information about where errors occur like class name, line number in code etc.
I did not find any information in their official manual about any compiler parameters that can change output format appropriately. I want error messages look like this:
~\GradleExample.java:33 error: ';' expected
UPDATE:
I achieve this behaviour on 3 machines:
OS: Microsoft Windows 7 x64 Ultimate SP1 [version 6.1.7601];
Java: 1.8.0_73;
Gradle: 2.14.
OS: Microsoft Windows 10 x64 Pro [version 10.0.14393];
Java: 1.8.0_121;
Gradle: 3.4.1.
OS: Microsoft Windows 7 x64 Ultimate SP1 [version 6.1.7601];
Java: 1.8.0_121;
Gradle: 3.4.1.
The absence of line numbers and class names is experienced only when running with Gradle. I also tried to run checker with Maven and with Javac from command line and it worked perfectly.
To configure Checker Framework with Gradle i followed steps from manual. There are 3 steps:
Download framework;
Unzip it to create a checker-framework directory;
Configure Gradle to include Checker Framework on the classpath.
As i understand, Gradle will do steps 1 and 2 automatically when providing needed Checker Framework's jars through dependency management. Nevertheless i tried both options:
dependency management:
I simply downloaded example project and executed "gradle compileJava" from root
of the GradleJava7Example project.
manually writing paths in gradle build file:
allprojects {
tasks.withType(JavaCompile).all { JavaCompile compile ->
compile.options.compilerArgs = [
'-processor', 'org.checkerframework.checker.nullness.NullnessChecker',
'-processorpath', "C:\\checker-framework-2.1.10\\checker\\dist\\checker.jar",
"-Xbootclasspath/p:C:\\checker-framework-2.1.10\\checker\\dist\\jdk8.jar",
'-classpath', 'C:\\checker-framework-2.1.10\\checker\\dist\\checker.jar;C:\\checker-framework-2.1.10\\checker\\dist\\javac.jar'
]
}
}
I've found a workaround. I'll explain it later, but now if somebody has the same problem, add this line to you JavaCompile tasks configuration:
allprojects {
tasks.withType(JavaCompile).all { JavaCompile compile ->
System.setProperty("line.separator", "\n") // <<<<<< add this line
compile.options.compilerArgs = [
'-processor', 'org.checkerframework.checker.nullness.NullnessChecker',
'-processorpath', "${configurations.checkerFramework.asPath}",
"-Xbootclasspath/p:${configurations.checkerFrameworkAnnotatedJDK.asPath}"
]
}
}
First of all i must say that problem was not in Checker Framework at all. I managed to reproduce the same behavior as mentioned in question without Checker Framework. I have created a little custom Annotation Processor. Here is the code:
#SupportedSourceVersion(value = SourceVersion.RELEASE_8)
#SupportedAnnotationTypes(value = {"*"})
public class MyProcessor extends AbstractProcessor{
#Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
String sepr = System.getProperty("line.separator");
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "[error code] " + sepr + " catched!!!");
return true;
}
}
As you can see, all it does is printing a message right away from start. Note that i used a line separator provided by java.lang.System class to split message. When i registered this processor and tried to run "gradle compileJava" from gradle project it produced the following output:
:compileJava
catched!!!
1 error
:compileJava FAILED
The property "line.separator" for Windows OS returns CR+LF: "\r\n". I don't know why Messager.printMessage(Diagnostic.Kind kind, CharSequence msg) has this behaviour, because when i type System.err.print("[error code] " + sepr + " catched!!!") instead, everything works fine (note also that this problem occur only when i use Gradle, if i run manually javac with all arguments or use Maven everyting is fine).
I found that if i substitude the provided by system separator with simple "\n" symbol compiler error messages are displayed correctly.
For now i choose this solution as a workaround.

Android Studio shows inexplicable warnings for build.gradle

I've created an Android library project in Android Studio and prepared the build.gradle to automate deployment to the Maven Central repository, followed the official instructions from Sonatype.
Particularly, I've added metadata according the Metadata Definition and Upload section
uploadArchives {
repositories {
mavenDeployer {
beforeDeployment { MavenDeployment deployment -> signing.signPom(deployment) }
//...
pom.project {
name 'Example Application'
description 'A application used as an example on how to set up pushing its components to the Central Repository.'
url 'http://www.example.com/example-application'
scm {
connection 'scm:svn:http://foo.googlecode.com/svn/trunk/'
developerConnection 'scm:svn:https://foo.googlecode.com/svn/trunk/'
url 'http://foo.googlecode.com/svn/trunk/'
}
licenses {
license {
name 'The Apache License, Version 2.0'
url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
}
}
developers {
developer {
id 'manfred'
name 'Manfred Moser'
email 'manfred#sonatype.com'
}
}
}
}
}
}
Android Studio shows warnings like 'name' cannot be applied to '(java.lang.String)' for the entries
pom.project/name,
pom.project/description,
pom.project/licenses/license/name,
pom.project/organization/name and
pom.project/developers/developer/name.
Running ./gradlew --info clean uploadArchives shows no such warnings. The generated pom.xml contains the defined metadata.
These warnings are somewhat annoying, because Android Studio intercepts every commit that includes the build.gradle to inform me about the existence of warnings.
The question: It there actually a problem with the build.gradle or is there something wrong with Android Studio's interpretation? If it is a problem with the build.gradle, how do I fix it?

Create a Groovy executable jar with Spock test set as to be executed

I want to create jar with two groovy files, AppLogic.groovy which consists of two few groovy classes and another file, AppSpec that has Spock test suite and I would like to have this Spock class executed (set as executable). How can I create such jar with all dependencies? I found sth similar for jUnit here: how to export (JUnit) test suite as executable jar but could not adapt it for my needs.
I use gradle for build, here is my build.gradle file:
group 'someGroup'
version '1.0'
apply plugin: 'groovy'
apply plugin: 'java'
apply plugin:'application'
sourceCompatibility = 1.7
repositories {
//some repos here
maven { url "http://repo.maven.apache.org/maven2" }
}
dependencies {
//some dependencies here
}
I was browsing around and found SpockRuntime, but I do not know if and how I can use it to achive my goal.
And the winner is:
static void main(String[] args) {
EmbeddedSpecRunner embeddedSpecRunner = new EmbeddedSpecRunner()
embeddedSpecRunner.runClass(MySpec)
}
I do not advise using the EmbeddedSpecRunner from spock implementation as described in accepted answer.
This is what I found to work reliably with gradle 4.9. The basic approach is to use:
The gradle application plugin to create a single tarfile with all testRuntimeClasspath dependencies and shell scripts to run the spock tests
The gradle maven-publish plugin to publish the tar file as an artifact to your maven repo (in my case nexus)
The build.gradle file looks like this:
apply plugin: 'java'
apply plugin: 'groovy'
apply plugin: 'maven-publish'
apply plugin: 'application'
mainClassName = 'org.junit.runner.JUnitCore' // The junit 4 test runner class
applicationName = 'run-tests-cli' // Feel free to change
repositories {
...
}
dependencies {
...
testImplementation "org.codehaus.groovy:groovy-all:${groovyVersion}"
testImplementation "org.spockframework:spock-core:${spockVersion}"
}
// Package compiled spock / junit tests to <artifact>-test-<version>.jar
task testJar(type: Jar) {
classifier = 'tests'
from sourceSets.test.output.classesDirs
}
// Copy all testRuntimeClasspath dependencies to libs folder
task copyToLibs(type: Copy) {
from configurations.testRuntimeClasspath
into "$buildDir/libs"
}
// Make sure test jar is copied
copyToLibs.dependsOn('testJar')
// Make sure platform-specific shell scripts are created after copyToLibs
startScripts.dependsOn(copyToLibs)
// Configure what goes into the tar / zip distribution file created by gradle distribution plugin assembleDist task
distributions {
main {
contents {
// Include test jar
from(testJar) {
into "lib"
}
// Include all dependencies from testRuntimeClasspath
from(copyToLibs) {
into "lib"
}
}
}
}
startScripts {
// Ensure ethat all testRuntimeClasspath dependencies are in classpath used by shell scripts
classpath = project.tasks['testJar'].outputs.files + project.configurations.testRuntimeClasspath
}
publishing {
repositories {
maven {
def releasesRepoUrl = "https://nexus.yourcompany.com/repository/maven-releases/"
def snapshotsRepoUrl = "https://nexus.yourcompany.com/repository/maven-snapshots/"
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
credentials {
username = rootProject.getProperty('NEXUS_USERNAME')
password = rootProject.getProperty('NEXUS_PASSWORD')
}
}
}
publications {
maven(MavenPublication) {
groupId = 'com.yourgroupId'
version = "${rootProject.getVersion()}"
}
TestJar(MavenPublication) {
artifact(testJar)
}
RunTestsCliTar(MavenPublication) {
artifact(distTar)
artifactId "${applicationName}"
}
}
}
Now you can do the following:
To build the project (including the tar file) without running test task: gradle -x test clean build
To publish artifacts produced by project (including tar file to maven repo - in my case nexus): gradlew -x test publish. Note you will need to provide credentials to upload artifacts to repo. It is good practice to define them (NEXUS_USERNAME, NEXUS_PASSWORD in my example) in ~/.gradle/gradle.properties or specify them via -P options on the gradle command line.

Trying to make an Android Studio Application with Adobe Creative SDK Image Editing, cannot get libraries compiled in gradle

I've been trying to properly import this library to begin writing an image editing component for my application. I currently have the downloaded 'creativesdk-repo' folder in the root directory, and have followed instructions according to this tutorial:
https://creativesdk.adobe.com/docs/android/#/articles/gettingstarted/index.html.
And this tutorial as well:
https://creativesdk.adobe.com/docs/android/#/articles/imageediting/index.html
There are no problems building when I simply use the basic authorization library, but my application needs photo editing capability. My foremost problem (among many) lies within the build.gradle file of the application (not the encompassing project build.gradle file).
Here is the code in my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.0"
defaultConfig {
applicationId "com.example"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
compile 'com.adobe.creativesdk.foundation:auth:0.3.94'
compile 'com.adobe.creativesdk.image:4.0.0'
}
The very last line causes an error message to appear:
Error:Failed to resolve: com.adobe.creativesdk.image:4.0.0:
Open File
I believe these messages mean that the image editing portion of the Adobe Creative SDK libraries are not being recognized. I have even tested this with example projects from Adobe and it runs into the same problem. What can I do to fix this and start writing this portion of my application?
You need download creative-sdk repo from download links into your project folder. In the project gradle define creative-sdk repo url like this:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
mavenCentral()
jcenter()
maven{
url "${project.rootDir}/creativesdk-repo" //ADD THE CORRECT LOCATION OF THE CREATIVESDK LIBRARY FILES
}
}
}
In your app build.gradle define this:
compile 'com.adobe.creativesdk.foundation:auth:0.3.94'
compile 'com.adobe.creativesdk:image:4.0.0'
You should extend your Application class and implement this, something like this:
public class ExampleApplication extends MultiDexApplication implements IAdobeAuthClientCredentials , IAviaryClientCredentials {
private static final String CREATIVE_SDK_SAMPLE_CLIENT_ID = "62bbd145c3f54ee39151823358c83e28";
private static final String CREATIVE_SDK_SAMPLE_CLIENT_SECRET = "2522a432-dfc8-40c4-94fe-646e10223562";
#Override
public void onCreate() {
super.onCreate();
AdobeCSDKFoundation.initializeCSDKFoundation(getApplicationContext());
}
#Override
public String getClientID() {
return CREATIVE_SDK_SAMPLE_CLIENT_ID;
}
#Override
public String getClientSecret() {
return CREATIVE_SDK_SAMPLE_CLIENT_SECRET;
}
#Override
public String getBillingKey() {
return "";
}
}
In your AndroidManifest.xml inside Application tag put this:
<provider
android:name="com.aviary.android.feather.sdk.internal.cds.AviaryCdsProvider"
android:authorities="com.package.AviaryCdsProvider"
android:exported="false"
android:process=":aviary_cds" />
With this configuration you can call Aviary Sdk Activity with this:
Intent newIntent = new AviaryIntent.Builder(this);
//config values
startActivity(newIntent);
I configure SDK like this, and its working. Sorry for the delay.
UPDATE 10/10/2016:
Thanks to Ash Ryan:
Trying to make an Android Studio Application with Adobe Creative SDK Image Editing, cannot get libraries compiled in gradle
Use this:
//noinspection SpellCheckingInspection
repositories {
// ...
// For Adobe Creative SDK
maven { url 'https://repo.adobe.com/nexus/content/repositories/releases/' }
}
Source: https://creativesdk.adobe.com/docs/android/#/articles/gettingstarted/index.html
Please make sure under global project repo. If not gradle not able to find the path.
allprojects {
repositories {
compile 'com.adobe.creativesdk.foundation:auth:0.3.94'
compile 'com.adobe.creativesdk:image:4.0.0'
}
I think your error is here. Change:
complie 'com.adobe.creativesdk.image:4.0.0'
for this:
compile 'com.adobe.creativesdk.image:4.0.0'
It`s a simple sintax error.
UPDATE 10/10/2016:
Thanks to Ash Ryan:
Trying to make an Android Studio Application with Adobe Creative SDK Image Editing, cannot get libraries compiled in gradle
just use latest lib dependency in Module:app build.gradle file and update your android sdk and android studio
compile 'com.adobe.creativesdk:image:4.6.3'

Resources