Run android unit tests & instrumentation tests on Jenkins (Gradle) - android

I have some unit and instrumentation tests in my androidTest folder.
I'm trying to run these tests on my local Jenkins.
I've successfully configured my project with Jenkins and I've also created an emulator for the instrumentation tests.
So far all the resources I've seen only focus on ant and earlier versions of Gradle. The syntax for writing tasks has slightly changed in the current version and I'm confused about this.
Here is my build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "myPackageName"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.google.android.gms:play-services:6.5.87'
}

You can write a jenkis script like this:
stage('Build & Install') {
//Build the apk and the test apk which will run the tests on the apk
sh 'chmod +x gradlew && ./gradlew --no-daemon --stacktrace clean :app:assembleDevDebug :app:assembleDevDebugAndroidTest'
}
stage('Tests') {
//Start all the existing tests in the test package
sh './gradlew --no-daemon --debug :app:connectedDevDebugAndroidTest'
}
This should install the apk and the test apk into the device and start the test cases in the test apk on installation.
Here I have given DevDebug as I have a flavor constant called Dev and build type is Debug. If you don't have any of those, you don't use them.

Create a job in junkins and (configure adb path) add this command to build steps as execute shell command or as a windows bat cmd
$ adb shell am instrument -w com.xyz.abc.test/android.test.InstrumentationTestRunner
P.S :- For better automation use robotium and spoon with junkins you can automate everything, you push a commit on git and you will get test results in your mail inbox it's that cool.
Edit
Running tests using spoon
Add these commands to build steps
./gradlew assembleDebugAndroidTest
./gradlew assembleDebug
specify debug-build.apk path and test-unaligned.apk path in spoon command properly
java -jar C:\Users\Shivam\Downloads\spoon-runner-1.1.1-jar-with-dependencies.jar --apk C:\Users\Shivam\Downloads\SpoonAndRobotiumTest\app\build\outputs\
apk\app-debug.apk --testapk C:\Users\Shivam\Downloads\SpoonAndRobotiumTest\
app\build\outputs\apk\app-debug androidTest-unaligned.apk --sdk E:\sdk

At first, you must install the Gradle Plugin on your Jenkins-CI installation. You can search it at http://yourhost/jenkins/pluginManager/
To continue integrating it, you can take a look at this presentation, specially on the lastest slides

Related

Integration test and Cucumber test

I have a project where I'm running android instrumentation tests with an AndroidJunitRunner.
I'm now adding UI automated tests using Cucumber. How can I keep both Cucumber tests that use a runner that extends MonitoringInstrumentation and the other instrumentation tests that use a runner that extends `AndroidJunitRunner?
In the build.gradle I used to have
testInstrumentationRunner "com.packagename.packagename2.MockedTestRunner"
now I have:
testApplicationId "com.packagename.packagename2.test"
testInstrumentationRunner "com.packagename.packagename2.test.Instrumentation"
Do I need to create a second module to run the cucumber tests?
To enable Android Test Orchestrator using the Gradle command-line tool, complete these steps:
android {
defaultConfig {
...
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// The following argument makes the Android Test Orchestrator run its
// "pm clear" command after each test invocation. This command ensures
// that the app's state is completely cleared between tests.
testInstrumentationRunnerArguments clearPackageData: 'true'
}
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
}
dependencies {
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestUtil 'androidx.test:orchestrator:1.1.0'
}
Run Android Test Orchestrator by executing the
./gradlew connectedCheck
Enable from Android Studio
Support for Android Test Orchestrator is available with Android Studio 3.0 and higher. To enable Android Test Orchestrator in Android Studio, add the statements shown in Enable from Gradle to your app's build.gradle file.

Android Studio 2.3 doesn't have Test Instrumentation specifier in UI

After updating to Android Studio 2.3 when I try to run some espresso tests I get the following error:
Test running failed: Unable to find instrumentation info for: ComponentInfo{com.example.android/android.test.InstrumentationTestRunner}
Empty test suite.
This was easily fixable in the past where in the Run Configuration I could specify my own InstrumentationRunner. Now I can't seem to find this option so I can't really specify my runner class now.
Note that my build gradle does contain
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
resConfigs "en", "es"
}
I ran into this problem this morning. I deleted the old run configuration (the one that was created before you specified the new runner via build.gradle). I re-ran the tests and the new runner was picked up by Android Studio.
On Android Studio 2.3 this feature is not available as this was an optional to specify the Instrumentation Runner package/class in previous versions of Studio. But Android 2.3 is smart enough to pick this from your build.gradle file provided you have defined Instrumentation package runner there.
The following setup fixed the issue
productFlavors {
doTheTests {
minSdkVersion 18
testInstrumentationRunner "com.company.app.test.TestRunner"
if (System.getenv('CONTINUOUS_INTEGRATION').equals("true")) {
testInstrumentationRunnerArguments(package: "com.company.app.test")
}
}
}
Because Android Studio doesn't allow the testInstrumentationRunnerArguments but executing the tests through terminal indeed requires it!
Obviously on the terminal / CI system, set an environment variable like:
export CONTINUOUS_INTEGRATION=true
What helped to me - starting the test from command line then syncing the project.

Android Studio: Multiple APKs for Multiple Modules

In Android is there a way to generate signed APKs for all modules in a project.
E.g. I have following project
Project
-- Library Module
-- Module 1
-- Module 2
-- Module 3
I want to generate APKs for all 3 modules in one command. Currently I have to separately use Generate Dialog for all 3 which takes a lot of time.
Yes you can generate multiple apk files with gradlew.
Open Terminal Window in Android Studio and run following commands:
1- Navigate to root folder of the project, where gradlew file is located
cd ..
2- Give executable permissions to gradlew (this needs to be done only once, no need to repeat again)
chmod a+x gradlew
3- Generate debuggable apks of all underlying modules.
./gradlew assembleDebug
You can also generate release apk files for all modules, by using this command instead
./gradlew assembleRelease
for more details, run the following command to see list of all tasks that can be run on gradlew
./gradlew tasks
Note: Running ./gradlew first time might result in terminal downloading the gradle files from server, wait for the downloading to complete before moving forward!
Hope that helps!
Update:
For providing signing information in grade file, Open your module specific build.grade file and update it to contain this code:
signingConfigs {
playstore {
keyAlias 'KEY_ALIS_NAME_HERE'
storeFile file('/PATH_TO_KEYSTORE_FILE_HERE/app.keystore')
keyPassword 'KEY_PASSWORD_HERE'
storePassword 'STORE_PASSWORD_HERE'
}
}
buildTypes {
release {
minifyEnabled true
proguardFiles 'proguard-file.txt'
proguardFile 'proguard-file.txt'
debuggable false
signingConfig signingConfigs.playstore
}
}
After that you can simply run ./gradlew assembleRelease to do the work :)

Android Studio, Fire TV - Issues compiling with Amazon SDK

I'm still exceptionally new to Android Development, about 2 weeks in to my first project. So, a resolution may be obvious, and I can only hope this eventually helps another in my shoes.
The goal was to build an app for FireTV that would stream media (on-demand or live video stream).
I started research on Amazon's developer portal, downloaded AS, Amazon SDK, and anything else I was told I would need to continue. My first thoughts were to familiarize myself with Lollipop, using Google TV's for emulator, and using the TV Activity template, which from what I see is heavily based on the Leanback library. I built and tested and modified to my hearts content, and had a working app, using emulators that can handle api21. Then came the time to 'retro-fit' what I had working with Amazon's Fire TV sdk Add-On (API 17). Learned a bit about AppCompat and a few other errors along the way, most I could figure out on my own. But now, I'm stuck.
Gradle won't compile now that I'm using the FireTV add-on, and the console output isn't helpful to me, at this level.
" What went wrong:
Execution failed for task ':app:processDebugResources'.
com.android.ide.common.internal.LoggedErrorException: Failed to run command:
C:\path\to\sdk\build-tools\21.1.2\aapt.exe package -f --no-crunch -I C:\path\to\sdk\platforms\android-17\android.jar -M C:\path\to\app\build\intermediates\manifests\full\debug\AndroidManifest.xml -I C:\path\to\sdk\add-ons\addon-amazon_fire_tv_addon-amazon-17\libs\notification.jar -I C:\path\to\sdk\add-ons\addon-amazon_fire_tv_addon-amazon-17\libs\gamecontroller.jar -S C:\path\to\app\build\intermediates\res\debug -A C:\path\to\app\build\intermediates\assets\debug -m -J C:\path\to\app\build\generated\source\r\debug -F C:\path\to\app\build\intermediates\res\resources-debug.ap_ --debug-mode --custom-package com.d53e.rbeal.fntsysportsnetwork -0 apk --output-text-symbols C:\path\to\app\build\intermediates\symbols\debug
Error Code:
1
Output:
ERROR: Asset package include 'C:\path\to\sdk\add-ons\addon-amazon_fire_tv_addon-amazon-17\libs\notification.jar' not found.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.*
"
Been stuck here for two days, running short on time and am urgently needing progress. Can't figure out how to include --stacktrace or --info as suggested.
In panic, i tried to port the project to eclipse, and as an end result am stuck with "Cannot Resolve 'R'" errors, the only fix I can find is to change the build target, but I think I need to use Amazon's.
I'd rather use Android Studio, but at this point any help would be greatly appreciated.
build.gradle
apply plugin: 'com.android.application'
android {
// compileSdkVersion 21
compileSdkVersion "Amazon.com:Amazon Fire TV SDK Addon:17"
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.d53e.myname.appname"
minSdkVersion 17
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:recyclerview-v7:21.0.3'
compile 'com.android.support:leanback-v17:21.0.2'
compile 'com.squareup.picasso:picasso:2.3.2'
compile 'com.android.support:support-v4:21.0.3'
compile 'com.amazon.device.tools.build:gradle:1.0.0'
}
(project) build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.amazon.device.tools.build:gradle:1.0.0'
//classpath 'com.android.tools.build:gradle:1.0.0'
}
}
allprojects {
repositories {
jcenter()
}
}
One of those moments where i want to slam my head against a wall...
Manually copy the file into the "projectName/app/libs" folder,
Change the project explorer view to project, so you can see the libs folder
Right click on the item[s] within, and select "Add as Library"
Much like that last step in eclipse.

Gradle doesn't package gdbserver into apk. ndk-gdb results in "ERROR: Non-debuggable application installed on the target device"

In Android Studio 0.6 I created an android test project with a very simple C function wrapped with the JNI. I set android:debuggable="true" in the AndroidManifest.xml.
I ran ndk-build NDK_DEBUG=1. This generated a gdbserver and a gdb.setup file in the correct location.
However, when I built the project in Android studio, and ran ndk-gdb --debug I got the following output:
Android NDK installation path: /usr/local/android-ndk-r9d
Using default adb command: /Applications/Android Studio.app/sdk/platform-tools/adb
ADB version found: Android Debug Bridge version 1.0.31
Using ADB flags:
Using JDB command: /usr/bin/jdb
Using auto-detected project path: .
Found package name: com.example.testndk.app
ABIs targetted by application: armeabi-v7a
Device API Level: 19
Device CPU ABIs: armeabi-v7a armeabi
Compatible device ABI: armeabi-v7a
Using gdb setup init: ./libs/armeabi-v7a/gdb.setup
Using toolchain prefix: /usr/local/android-ndk-r9d/toolchains/arm-linux-androideabi-4.8/prebuilt/darwin-x86_64/bin/arm-linux-androideabi-
Using app out directory: ./obj/local/armeabi-v7a
Found debuggable flag: true
Found gdb.setup under libs/armeabi-v7a, assuming app was built with NDK_DEBUG=1
Found data directory: '/data/data/com.example.testndk.app'
ERROR: Non-debuggable application installed on the target device.
Please re-install the debuggable version!
I looked inside the apk gradle generated to make sure gdbserver was there, but I couldn't find the gdbserver or gdb.setup files! Where did they go?? It looks like gradle isn't packaging them in my apk!
Here's my build.gradle:
apply plugin: 'android'
android {
compileSdkVersion 19
buildToolsVersion "19.1.0"
defaultConfig {
applicationId "com.example.testndk.app"
minSdkVersion 9
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
sourceSets.main {
jniLibs.srcDir 'src/main/libs' // use the jni .so compiled from the manual ndk-build command
jni.srcDirs = [] //disable automatic ndk-build call
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
}
How can I get gradle to properly package the gdbserver in my apk? If you want to try it yourself, I put the full source on github. https://github.com/cypressf/testndk
I've experimented the same issue.
Setting this:
android{
buildTypes {
debug {
jniDebuggable true
}
}
}
wouldn't help since you don't rely on the android-plugin NDK integration.
One (not-so-good) solution is to manually copy the gdbserver and gdb.setup files to your app's nativeLibraryPath directory on the device, after your apk has been installed.
I also get the same issue.
=Way 1=
Due to gdbserver is named 'gdbserver' but not 'gbdserver.so', it woulud not be installed into APK. We can do some thing like this:http://ivansotof.com/2013/12/including-native-so-libraries-in-android/, finally we can make sure the APK contain the gdbserver in APK/lib/[ABI]/gdbserver.
=Way 2=
#ph0b's answer is useful for my project and it works! My gradle is v1.2, com.android.tools.build:gradle:0.10.+'
Good luck my friend.
Some helpful link:
http://ph0b.com/android-studio-gradle-and-ndk-integration/
I experienced same problem and solved it by modifying "package[ProductFlavor][BuildType]JniLibs" task this way (after android section in build.gradle):
Sync packageTask = project.getTasks().findByName("packageDebugJniLibs")
packageTask.include(['**/gdbserver', '**/gdb.setup'])
If you use product flavors for ex.: demo, full - you have to modify two tasks with names packageDemoDebugJniLibs and packageFullDebugJniLibs.
// EDIT : you also should add an import:
import org.gradle.api.tasks.Sync
but possibly it will also work if you write def packageTask = ...
I faced the same issue in two different cases.
1) If you are using Samsung phone for debug. They are not compatible with native debugging with gdb. Check this : http://developer.samsung.com/forum/thread/ndk-debugging-with-gdb/77/178834
2) If you are using any other phone( Sony xperia Z in my case), make sure your phone allows it to debug your app. It means go to developer options > select debug app: xyz.app
There is another way, using apktool:
✓ Decompile your .apk using apktool d path/to/your.apk
✓ Copy gdbserver and gdb.setup over to the your.apk/libs folder
✓ Rebuild your .apk with apktool b -o out.apk path/to/decompiled
✓ Resign your apk with:
jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1 -keystore ~/.android/debug.keystore your.apk androiddebugkey
✓ Install your apk with adb install -r your.apk
I'm not sure if my answer will answer to your question, but I had similar issue - .apk did not contain /lib/armeabi-v7a/gdbserver, and apparently my own debugger (Visual studio) required that one.
I've searched in
%NDK_ROOT% (C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r11c\) and found that gdbserver, which I require was under path:
"C:\ProgramData\Microsoft\AndroidNDK64\android-ndk-r11c\prebuilt\android-arm\gdbserver\gdbserver"
(I've binary compared that it's same file as packaged into .apk file)
I've copied that file into folder specified by gradle:
sourceSets {
...
debug.jniLibs.srcDirs = [
'libs/debug'
]
release.jniLibs.srcDirs = [
'libs/release'
]
So into libs/debug, and gdbserver ended up into .apk file.
From Visual studio I've reconfigured as launch activity as "Launcher activity" and after that debug session launched successfully.
But I guess I will need to add more visual studio projects in order to enable actual source code debugging - now at least break and threads windows seemed to fill up.

Categories

Resources