Jenkins can not use adb when outside directory - android

As the title says, Jenkins can correctly switch directories to the adb and then execute it. But it can not use it when it is outside of adb's directory.
The following code :
.//opt/android-sdk-linux//platform-tools/adb --help
results in the following output :
+ .//opt/android-sdk-linux//platform-tools/adb
/tmp/jenkins882020622874679741.sh: rad 18: .//opt/android-sdk-linux//platform-tools/adb: Filen eller katalogen finns inte
Build step 'Execute shell' marked build as failure
Finished: FAILURE
While this code :
cd /opt/android-sdk-linux//platform-tools/
ls
./adb --help
correctly outputs the adb help manual.
Additionally, entering
adb --help
gives the same error even when the path given by "which adb" is appended to the $PATH variable.
ls /opt/android-sdk-linux//platform-tools/adb
also shows that it can see adb from its current directory.
How do I make jenkins run adb without switching directories to the adb directory?
edit 1:
Adding an alias to adb resulted in this error:
+ alias adb=.//opt/android-sdk-linux//platform-tools/adb
+ adb shell am instrument -w -r -e debug false -e class se.***** se.*******/android.support.test.runner.AndroidJUnitRunner
/tmp/jenkins523172794505644997.sh: rad 24: adb: kommandot finns inte
Build step 'Execute shell' marked build as failure
Finished: FAILURE

Clearly, something is still wrong with the PATH inclusion so maybe have Jenkins print the PATH variable to debug. A temporary solution could be using an alias:
$ alias adb=".//opt/android-sdk-linux//platform-tools/adb"
This command will result in .//opt/android-sdk-linux//platform-tools/adb being executed every time you execute adb.

After working for a while on this I found that Jenkins allows you to inject environment variables in the device configuration. You can also inject environment variables for each build separately. I do not know why exporting them through a bash script does not work but this is what worked for me. In summary, injecting the $PATH variable using Jenkins' built in tool fixed everything.

Related

Android R.string ids mix in multi-module tests

My app is implemented as a multi-module project (with dynamics module), where there is a shared module with espresso tests. These espresso tests give me different outcomes depending if I run them from Android Studio or from the command line.
The issue I find when I run the tests from the command line is that it tries to assert against the wrong string resources.
The actual tests code is asserting this, where R.string.home_activity_title value is "Hello":
assertDisplayed(R.string.home_activity_title)
However when it is run from the command line, the assertion fails with this output
No views in hierarchy found matching: with string from resource id: <2131886676>[send_message_hint] value: Type a messageā€¦
Notice how the tests from the command line is trying to assert against a different string that the one is defined on the test.
I tried to understand the difference between running the tests from AndroidStudio and running them from the command line but I am clearly missing something here. This is how I run the tests from the command line
apk="path/to/apk"
testApk="path/to/test/apk"
testRunner="my.custom.test.runner"
# Generate universal APK
echo "Generating universal APK"
sh build_develop.sh
# Install universal APK on emulator
echo "Installing universal APK"
adb uninstall <appPackage>
adb install -t $apk
echo "Building test APK"
./gradlew :testModule:assembleDebugAndroidTest
echo "Installing Test APK"
adb uninstall <testApkPackage>
adb install -t $testApk
echo "Running Acceptance Tests"
# Run all acceptance tests
adb shell am instrument -w $testRunner
Try specifying the module for which you want the string to be obtained.
E.g. com.example.module.R.string.home_activity_title instead of R.string.home_activity_title

Command found at the command line but not in script

I am trying to create a new android project using a script on mac. For this, I followed the following steps:
Add PATH of tools and platform-tools in my .bash_profile
Verify that the android command works in the shell.
Write a script to create a new project.
Here is the script I wrote:
NAME=$1
PATH=$2
PACKAGE=$3
echo $1
echo $2
echo $3
function create_new_android_project()
{
android create project -n "$NAME" -t 7 -p "$PATH" -k "$PACKAGE" -a MainActivity
}
create_new_android_project
echo
echo "******** Complete!!!"
The android command runs in the shell. But when I run the script with
sh script.sh project_name project_path package_name
it gives up with an error saying android: command not found.
The PATH variable has a special meaning to the shell. The shell expects it to contain a colon separated list of directories where it can look for programs, when you do not supply a complete path for them.
To solve your problem, use another name than PATH in your program.
As a general advice, I invite you to use a stronger discipline quoting arguments and avoiding the echo command:
There is few reasons why a variable name should not appear between double quotes. Therefore, unless you want to achieve something special, you should always use double quotes to control variable expansion.
Prefer printf over echo because it is easier to use, more reliable and more portable (also, a clear winner).

What can I do to run monkey on Jenkins

I would like to run monkey on Jenkins but after use configuration:
https://wiki.jenkins-ci.org/pages/viewpage.action?pageId=57181910
I see in monkey.txt file:
No activities found to run, monkey aborted.
$ E:\server\AndroidSDK/platform-tools/adb.exe -s localhost:46881
shell monkey -v -v -p package.name -s 0 --throttle 0 50
$E:\server\AndroidSDK/platform-tools/adb.exe disconnect
localhost:46881 [android] Stopping Android emulator [android]
Archiving emulator log
I run this command on my PC, but I cannot run it on server.
If I run the command: adb shell monkey -v -v 50 -p package.name -s --throttle 0 on cmd on server it will work.
How can I configure Jenkins to run monkey properly?
I'm using the newest plugins for Jenkins.
Are you really using "package.name" in the monkey command line rather than your actual package name?
Did you ensure the APK was installed on the emulator before running monkey?
Otherwise, is there any output in the logcat.txt which indicates what's going wrong?
Also, I'm not sure why you linked to a really old version of the Android Emulator Plugin wiki page, but since then there's a Jenkins build step that will run monkey for you, without you having to manually write it into a batch script step.
Like Thomas pointed out in Christopher's comment:
You also have to think about installing the apk to the smartphone.
This can be done by using the "install android package" build step before "Run android monkey tester".
After that you can add another build step "uninstall android package"

Android Debug Bridge (adb) command line tool exists in $PATH, but "command not found" in linux

sudo echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/lubuntu/Tools/android-sdk-linux/platform-tools
adb exists in /home/lubuntu/Tools/android-sdk-linux/platform-tools
Then I executed adb start-server:
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
Then sudo adb install test.apk
sudo: adb: command not found
I have added adb in the environment variable $PATH, but sudo couldn't find it.
Why?
sudo means you're switching to root, and that does not necessarily mean that your environment comes along. That could be dangerous.
At your risk, add the -E option to inherit the calling environment.
The problem is the PATH var is set for that user, and root don't have the aditional PATH entry, so it can't found the program.
You should setup adb for root too:
adb Environmental Variables
Open ~/.bashrc and add the following to the very bottom
export PATH=${PATH}:<sdk>/tools:<sdk>/platform-tools
Change <sdk> to the actual path. ie /home/user/android-sdk-linux
Close and re-open your terminal to refresh variables.
from adb setup
If doesn't work, make symbolic links from adb and other binaries to /usr/local/bin
Since you are running adb as root in the second example, adb would also have to be on root's path.
completely agree with answer of #unwind.You must not do this using "sudo".Give it a try without sudo and i guess it would work for you.Also, you should add the path to folder named "tools" present in the sdk, though its not connected with your problem, but a suggestion.
Completely agree.
The problem was the Android Debug Bridge (adb) command line tool that was not linked to the path. After setting it in the path, it worked for me.
$ gedit ~/.bashrc
set the path as follows in the .bashrc
export PATH=${PATH}:~/adt-bundle/tools
export PATH=${PATH}:~/adt-bundle/platform-tools

Install an apk file from command prompt?

I want to install a file using the Windows command line. First I want to build after compiling all the .jar files to create an .apk file for an Android application without using Eclipse.
Does anyone know how this can be done without the use of Eclipse & only by making use of command line.
You can use the code below to install application from command line
adb install example.apk
this apk is installed in the internal memory of current opened emulator.
adb install -s example.apk
this apk is installed in the sd-card of current opened emulator.
You can also install an apk to specific device in connected device list to the adb.
adb -s emulator-5554 install myapp.apk
Refer also to adb help for other options.
You can build on the command line with ant. See this guide.
Then, you can install it by using adb on the command line.
adb install -r MyApp.apk
The -r flag is to replace the existing application.
Use the Android Debug Bridge command line tool adb eg: adb install filename.apk.
A good reference for adb is Here
install [options] <PATH> Installs a package (specified by <PATH>) to the system.
Options:
-l: Install the package with forward lock.
-r: Reinstall an exisiting app, keeping its data.
-t: Allow test APKs to be installed.
-i <INSTALLER_PACKAGE_NAME>: Specify the installer package name.
-s: Install package on the shared mass storage (such as sdcard).
-f: Install package on the internal system memory.
-d: Allow version code downgrade.
uninstall [options] <PACKAGE> Removes a package from the system.
Options:
-k: Keep the data and cache directories around after package removal.
You can install an apk to a specific device/emulator by entering the device/emulator identifier before the keyword 'install' and then the path to the apk. Note that the -s switch, if any, after the 'install' keyword signifies installing to the sd card. Example:
adb -s emulator-5554 install myapp.apk
The simple way to do that is by command
adb install example.apk
and if you want to target connect device you can add parameter " -d "
adb install -d example.apk
if you have more than one device/emulator connected you will get this error
adb: error: connect failed: more than one device/emulator
- waiting for device - error: more than one device/emulator
to avoid that you can list all devices by below command
adb devices
you will get results like below
C:\Windows\System32>adb devices
List of devices attached
a3b09hh3e device
emulator-5334 device
chose one of these devices and add parameter to adb command as " -s a3b09hh3e " as below
adb -s a3b09a6e install example.apk
also as a hint if the path of the apk long and have a spaces, just add it between double quotes like
adb -s a3b09a6e install "c:\my apk location\here 123\example.apk"
Commands for install APK files like it does in Android Studio you can see below.
1) To push your app:
adb push /pathOfApk/com.my.awesome.apk /data/local/tmp/com.my.awesome
where com.my.awesome is your package.
2) To install:
adb shell pm install -t -r "/data/local/tmp/com.my.awesome"
Open Terminal in Android Studio
You might see
C:\Users\nikhil\AppData\Local\Android\Sdk\platform-tools>
copy and paste your apk which you want to install on above path inside platform-tools.
In my case app-qa-debug.apk I kept inside platform-tools folder.
install command
adb install app-qa-debug.apk
so in the terminal you could see something
C:\Users\nikhil\AppData\Local\Android\Sdk\platform-tools>adb install app-qa-debug.apk
post-installation you could get the message as
Performing Streamed
Install Success
Press Win+R > cmd
Navigate to platform-tools\ in the android-sdk windows folder
Type adb
now follow the steps writte by Mohit Kanada (ensure that you mention the entire path of the .apk file for eg. d:\android-apps\test.apk)
It is so easy!
for example my apk file location is: d:\myapp.apk
run cmd
navigate to "platform-tools" folder(in the sdk folder)
start your emulator device(let's say its name is 5556:MyDevice)
type this code in the cmd:
adb -s emulator-5556 install d:\myapp.apk
Wait for a while and it's DONE!!
You're likely here because you want to build it too!
Build
gradlew
(On Windows gradlew.bat)
Then Install
adb install -r exampleApp.apk
(The -r makes it replace the existing copy, add an -s if installing on an emulator)
Bonus
I set up an alias in my ~/.bash_profile
alias bi="gradlew && adb install -r exampleApp.apk"
(Short for Build and Install)
To install a debug (test) apk, use -t:
Run Build-Make Project
Look for the last generated apk in the app folder.
Example:
adb install -t C:\code\BackupRestore\app\build\outputs\apk\debug\app-debug.apk
You can do this by using adb command line tools OR gradle commands:
See this Guide.
Setup command line adb
export PATH=/Users/mayurik/Library/Android/sdk/platform-tools/adb:/Users/mayurik/Library/Android/sdk/tool
Gradle commands to build and install.
#Start Build Process
echo "\n\n\nStarting"
./gradlew clean
./gradlew build
./gradlew assembleDebug
#Install APK on device / emulator
echo "installDebug...\n"
./gradlew installDebug
You can also uninstall any previous versions using
`./gradlew uninstallDebug`
You can launch your main activity on device/emulator like below
#Launch Main Activity
adb shell am start -n "com.sample.androidbuildautomationsample/com.sample.androidbuildautomationsample.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
I use this script on my windows machine ( insall all apks in current folder to all available devices )
Write-Host "Listing APKs..."
$List_Apks = New-Object System.Collections.ArrayList
Get-ChildItem -Path .\ -Filter *.apk -File -Name| ForEach-Object {
$apk_filename = [System.IO.Path]::GetFileName($_)
$List_Apks+=$apk_filename
$apk_filename
}
Write-Host "Found apks "$List_Apks.Length
Write-Host ""
$raw_list = adb devices
$array_lines = $raw_list.Split("\n")
Write-Host "Listing devices "
$List_Device_Ids = New-Object System.Collections.ArrayList
1..($array_lines.Length-2) | foreach {
$device_id = $array_lines[$_].Split([char]0x9)[0]
$List_Device_Ids+=$device_id
$device_id
}
Write-Host "Found devices "$List_Device_Ids.Length
0..($List_Device_Ids.Length-1) | foreach {
$device_id = $List_Device_Ids[$_]
0..($List_Apks.Length-1) | foreach {
$apk_file_name = $List_Apks[$_]
Write-Host "Installing " $apk_file_name "->" $device_id
adb -s $device_id install -r $apk_file_name
}
}
Write-Host "Endo"
Save this as install-apks.ps1
Then from the powershell :
powershell -executionpolicy bypass -File .\install-apks.ps1
For people who wants to load apk from Linux system with React native application running on it.
I have given the path in which the android application resides as well. So that those who need to find the apk file can go to view it.
adb -s 434eeads install android/app/build/outputs/apk/debug/app-debug.apk
For reinstalling the android app on phone
adb -s 434eeads install -r android/app/build/outputs/apk/debug/app-debug.apk
-s -> source/serialNumber
r -> Re-install
path + file name : android/app/build/outputs/apk/debug/app-debug.apk
It is for react native applications.

Categories

Resources