Install an apk file from command prompt? - android

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.

Related

Creating a .sh script to ADB Install a few APKs

I'm trying to make a script to install a few APKs using my Raspberry Pi. I can make a .bat that works fine on Windows, but I'm having an issue with Linux.
Here's my current script sample
#!/user/bin/X11 (location of where ADB is)
adb kill-server
adb remount
#ECHO Now installing 1
adb install /home/pi/Desktop/1.apk
#ECHO Now installing 2
adb install /home/pi/Desktop/2.apk
#ECHO Now installing 3
adb install /home/pi/Desktop/3.apk
pause
When I run the script, terminal pops up but does nothing. I did use chmod a+x on the .sh file.
Updated on the script with BASH syntax. Same issue.
#!/bin/bash
adb kill-server
adb remount
echo "Now installing 1"
adb install /home/pi/Desktop/1.apk
echo "Now installing 2"
adb install /home/pi/Desktop/2.apk
echo "Now installing 3"
adb install /home/pi/Desktop/3.apk
read
You can also use below command for install few apks at once:
adb install-multiple

getting Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI] in Android studio 3.0

As per this blogpost from CommonsWare, AndroidManifest.xml file can have an android:testOnly attribute.
In my AndroidManifest.xml it is set as "false"
android:testOnly="false"
And I am generating the apk file using the “Build APK(s)” menu option as shown below image,
And when i am trying to install app from command line, adb install -r myapp.apk, I am still getting error,
Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
Android studio version is as below,
What else shall I do to make my app run?
You can also use command like this :
adb install -r -t myapp.apk
it works for me:
PS C:\Users\languoguang> adb -P 12345 install -r D:\GreeneTrans\HelloWorld-signed.apk
adb: failed to install D:\GreeneTrans\HelloWorld-signed.apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
PS C:\Users\languoguang> adb -P 12345 install -t D:\GreeneTrans\HelloWorld-signed.apk
Success
PS C:\Users\languoguang> adb -P 12345 install -r -t D:\GreeneTrans\HelloWorld-signed.apk
Success
PS C:\Users\languoguang>
Just use the following command:
adb install -t app/build/outputs/apk/debug/app-debug.apk
You do not need to use -r, -r means Reinstall an existing app, keeping its data.
Install an app You can use adb to install an APK on an emulator or
connected device with the install command:
adb install path_to_apk
You must use the -t option with the install command when you install a
test APK. For more information, see -t.
https://developer.android.com/studio/command-line/adb#move
-t: Allow test APKs to be installed. Gradle generates a test APK when you have only run or debugged your app or have used the Android Studio
Build > Build APK command. If the APK is built using a developer
preview SDK (if the targetSdkVersion is a letter instead of a number),
you must include the -t option with the install command if you are
installing a test APK.
https://developer.android.com/studio/command-line/adb#-t-option
Or you could use the same command as you click Run in Android Studio
adb push {project dir}/app/build/outputs/apk/debug/app-debug.apk /data/local/tmp/{appId}
adb shell pm install -t /data/local/tmp/{appId}
appId is defined in the app/build.gradle.
defaultConfig {
applicationId appId
Now the app is installed from locally on the device
Launch the first activity.
adb shell am start -n "{package name}/{package name}.splash.SplashActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
2019-11-13 13:43 Gradle sync started
13:44 Project setup started
13:44 Executing tasks: [:app:generateDebugSources,
:vplus_explore:generateDebugSources,
:vplus_uibase:generateDebugSources,
:vplus_widget:generateDebugSources,
:vplus_runtime:generateDebugSources,
:vplus_cards:generateDebugSources,
:vplus_launcher:generateDebugSources,
:vplus_settings:generateDebugSources,
:vplus_transactions:generateDebugSources,
:vplus_payment:generateDebugSources,
:vplus_common:generateDebugSources,
:vplus_account:generateDebugSources,
:vplus_commonres:generateDebugSources,
:vplus_bootstrap:generateDebugSources,
:vplus_logger:generateDebugSources]
13:44 Gradle sync finished in 27 s 126 ms
13:44 Gradle build finished in 4 s 666 ms
13:45 * daemon not running; starting now at tcp:5037
13:45 * daemon started successfully
13:45 Executing tasks: [:app:assembleDebug]
13:46 Gradle build finished in 33 s 640 ms
If you really want to be able to remove the test flag from the APK generated in Android Studio, you could try adding the following to your gradle.properties file:
android.injected.testOnly = false
Solution 1
Click drop-down menu with your configuration and choose Edit Configurations…
Select tab General and add -t to Install Flags field. Click Ok.
Now start the application again and it should work.
Solution 2
This means that, the shared application has some test packages, so
unless those has been removed and source is recompiled, you will not
be able to install this apk. But adb command provides a flag “-t”
using which you can install the apps with test packages.
$ adb install -r -t YourAndroidApp.apk
2566 KB/s (7266004 bytes in 2.764s)
Success
Solution 3
This error might occur if you moved the project from other computer where it was stored in different directory. To resolve the problem: Clean the project and build it again.
Solution 4
Go to “Settings” -> “Build, execution, deployment” and disable “instant run to hot swap code…”
Solution 5
Add this line to gradle.properties:
android.injected.testOnly = false
If you would like to manually install an APK or give it to someone for manual installation using the following adb command, then you should only build the APK from the Menu bar -> Build -> Build Bundle/APK.
adb install -r xyz.apk
Do not click on the play button as it builds the APK for test purposes only. Clicking on the play button overrides the APK in the default location which can then be installed manually by using the following command only.
adb install -r -t xyz.apk

Windows script to install multiple Android apps

In my Android Studio project I have some different apps and I want to build and install all of them at once. I didn't know if there is any way to do it without the terminal but with the terminal what I do is the following:
gradlew.bat assembleDebug
To build all the apps, and after:
adb -d install app1.apk
To install app1 in my device. If I do:
adb -d install app1.apk | adb -d install app2.apk
I will install app1 and app2 (app1.apk have the whole path of the apk location). So I want to build a script who have the four command for install my four apps but I don't know what to do on windows to do that. I think I can't just create my_script.sh like this:
adb -d install app1.apk
adb -d install app2.apk
adb -d install app3.apk
adb -d install app4.apk
and execute it... so I need your help. How can I do a command script like that on windows?
So, finally I found an answer. Scripts on windows are made it as in Linux but instead .sh there are .bat. Actually I found a better way to do what I want to do on Gradle instead of I was trying to do I use the install task of gradle called like this:
gradlew :app1:installRelease :app2:installRelease :app3:installRelease :app4:installRelease
Even you only need to put the fewer character needed to differenciate tasks. For example if you have Release and Debug build variants you only need to write:
gradlew :app1:iR
To execute the installRelease of app1 in gradle.

How to get list of currently working commands in Cygwin?

Currently working with the linphone-android integration. I have installed some packages during installation of Cygwin.
When I try some of the commands like ls, rm, cd, wget, tar and etc.. are working fine. But few commands like shell, clean are not working. It gives error command not found.
So, my question is,
Is there any way to get list of working commands for Cygwin?
Is there any way to install packages for these commands rather than installing them manually?
In my opinion the most elegant solution is to use the compgen command:
compgen -c lists all the available commands
compgen -a lists all the available aliases
You can also try a more brute approach:
Get all the paths from cygwin using echo $PATH and then for each folder execute ls -h <folder_name>
For installing a package the best way is to install first the equavalent of apt-get: apt-cyg from https://github.com/transcode-open/apt-cyg and put it in /usr/local/bin:
wget raw.github.com/transcode-open/apt-cyg/master/apt-cyg<br>
chmod +x apt-cyg<br>
mv apt-cyg /usr/local/bin<br>
You can also try running the setup executable used to install cygwin setup.exe -q -n -N -d -R c:\cygwin -s http://mirror_site_to_use -l c:\local_package_folder for a local package or setup.exe -q -P package_name to let the setup download the package

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"

Categories

Resources