How to run (not only install) an android application using .apk file? - android

Is there any command on cmd.exe that would allow me to start the main activity of a particular android application using the .apk file of that application. Please note that I know this command which only installs an android application:
adb install myapp.apk
This command will only install myapp onto the emulator and I have to manually run this application from the emulator (by performing single click on its icon).
What I want to do is use a command which not only installs the application but also starts its main activity (please note that I have only its .apk file so I don't know what the package name or any activity name is).

You can't install and run in one go - but you can certainly use adb to start your already installed application. Use adb shell am start to fire an intent - you will need to use the correct intent for your application though. A couple of examples:
adb shell am start -a android.intent.action.MAIN -n com.android.settings/.Settings
will launch Settings, and
adb shell am start -a android.intent.action.MAIN -n com.android.browser/.BrowserActivity
will launch the Browser.
If you want to point the Browser at a particular page, do this
adb shell am start -a android.intent.action.VIEW -n com.android.browser/.BrowserActivity http://www.google.co.uk
If you don't know the name of the activities in the APK, then do this
aapt d xmltree <path to apk> AndroidManifest.xml
the output content will includes a section like this:
E: activity (line=32)
A: android:theme(0x01010000)=#0x7f080000
A: android:label(0x01010001)=#0x7f070000
A: android:name(0x01010003)="com.anonymous.MainWindow"
A: android:launchMode(0x0101001d)=(type 0x10)0x3
A: android:screenOrientation(0x0101001e)=(type 0x10)0x1
A: android:configChanges(0x0101001f)=(type 0x11)0x80
E: intent-filter (line=33)
E: action (line=34)
A: android:name(0x01010003)="android.intent.action.MAIN"
XE: (line=34)
That tells you the name of the main activity (MainWindow), and you can now run
adb shell am start -a android.intent.action.MAIN -n com.anonymous/.MainWindow

if you're looking for the equivalent of "adb run myapp.apk"
you can use the script shown in this answer
(linux and mac only - maybe with cygwin on windows)
linux/mac users can also create a script to run an apk with something like the following:
create a file named "adb-run.sh" with these 3 lines:
pkg=$(aapt dump badging $1|awk -F" " '/package/ {print $2}'|awk -F"'" '/name=/ {print $2}')
act=$(aapt dump badging $1|awk -F" " '/launchable-activity/ {print $2}'|awk -F"'" '/name=/ {print $2}')
adb shell am start -n $pkg/$act
then "chmod +x adb-run.sh" to make it executable.
now you can simply:
adb-run.sh myapp.apk
The benefit here is that you don't need to know the package name or launchable activity name. Similarly, you can create "adb-uninstall.sh myapp.apk"
Note: This requires that you have aapt in your path. You can find it under the new build tools folder in the SDK

This is a solution in shell script:
apk="$apk_path"
1. Install apk
adb install "$apk"
sleep 1
2. Get package name
pkg_info=`aapt dump badging "$apk" | head -1 | awk -F " " '{print $2}'`
eval $pkg_info > /dev/null
3. Start app
pkg_name=$name
adb shell monkey -p "${pkg_name}" -c android.intent.category.LAUNCHER 1

When you start the app from the GUI, adb logcat might show you the corresponding action/category/component:
$ adb logcat
[...]
I/ActivityManager( 1607): START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.browser/.BrowserActivity} from pid 1792
[...]

I put this in my makefile, right the next line after adb install ...
adb shell monkey -p `cat .identifier` -c android.intent.category.LAUNCHER 1
For this to work there must be a .identifier file with the app's bundle identifier in it, like com.company.ourfirstapp
No need to hunt activity name.

First to install your app:
adb install -r path\ProjectName.apk
The great thing about the -r is it works even if it wasn’t already installed.
To launch MainActivity, so you can launch it like:
adb shell am start -n com.other.ProjectName/.MainActivity

I created terminal aliases to install and run an apk using a single command.
// I use ZSH, here is what I added to my .zshrc file (config file)
// at ~/.zshrc
// If you use bash shell, append it to ~/.bashrc
# Have the adb accessible, by including it in the PATH
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:path/to/android_sdk/platform-tools/"
# Setup your Android SDK path in ANDROID_HOME variable
export ANDROID_HOME=~/sdks/android_sdk
# Setup aapt tool so it accessible using a single command
alias aapt="$ANDROID_HOME/build-tools/27.0.3/aapt"
# Install APK to device
# Use as: apkinstall app-debug.apk
alias apkinstall="adb devices | tail -n +2 | cut -sf 1 | xargs -I X adb -s X install -r $1"
# As an alternative to apkinstall, you can also do just ./gradlew installDebug
# Alias for building and installing the apk to connected device
# Run at the root of your project
# $ buildAndInstallApk
alias buildAndInstallApk='./gradlew assembleDebug && apkinstall ./app/build/outputs/apk/debug/app-debug.apk'
# Launch your debug apk on your connected device
# Execute at the root of your android project
# Usage: launchDebugApk
alias launchDebugApk="adb shell monkey -p `aapt dump badging ./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name' | cut -d \' -f 2` 1"
# ------------- Single command to build+install+launch apk------------#
# Execute at the root of your android project
# Use as: buildInstallLaunchDebugApk
alias buildInstallLaunchDebugApk="buildAndInstallApk && launchDebugApk"
Note: Here I am building, installing and launching the debug apk which is usually in the path: ./app/build/outputs/apk/debug/app-debug.apk, when this command is executed from the root of the project
If you would like to install and run any other apk, simply replace the path for debug apk with path of your own apk
Here is the gist for the same.
I created this because I was having trouble working with Android Studio build reaching around 28 minutes, so I switched over to terminal builds which were around 3 minutes. You can read more about this here
Explanation:
The one alias that I think needs explanation is the launchDebugApk alias.
Here is how it is broken down:
The part aapt dump badging ./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name basically uses the aapt tool to extract the package name from the apk.
Next, is the command: adb shell monkey -p com.package.name 1, which basically uses the monkey tool to open up the default launcher activity of the installed app on the connected device. The part of com.package.name is replaced by our previous command which takes care of getting the package name from the apk.

Related

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

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

How to install an app in system/app while developing from android studio

Is there a way to make an app install directly in the system/app folder while developing on Android Studio (the device is rooted)?
Meaning, when I press on the 'Run app' button, I want the apk to be placed in system/app.
If this is not possible, what is the recommended most convenient way to work on building and testing a system app?
Deploy automatically system app from AS
You can create a script that will do the job, and run it automatically each time you hit run in AS.
1. Create the script
You can adapt this script that I've created from my needs. Place it in: project_directory/installSystem.sh
#!/bin/bash
# CHANGE THESE FOR YOUR APP
app_package="com.example"
dir_app_name="MySysApp"
MAIN_ACTIVITY="SysAppMainActivity"
ADB="adb" # how you execute adb
ADB_SH="$ADB shell" # this script assumes using `adb root`. for `adb su` see `Caveats`
path_sysapp="/system/priv-app" # assuming the app is priviledged
apk_host="./app/build/outputs/apk/app-debug.apk"
apk_name=$dir_app_name".apk"
apk_target_dir="$path_sysapp/$dir_app_name"
apk_target_sys="$apk_target_dir/$apk_name"
# Delete previous APK
rm -f $apk_host
# Compile the APK: you can adapt this for production build, flavors, etc.
./gradlew assembleDebug || exit -1 # exit on failure
# Install APK: using adb root
$ADB root 2> /dev/null
$ADB remount # mount system
$ADB push $apk_host $apk_target_sys
# Give permissions
$ADB_SH "chmod 755 $apk_target_dir"
$ADB_SH "chmod 644 $apk_target_sys"
#Unmount system
$ADB_SH "mount -o remount,ro /"
# Stop the app
$ADB shell "am force-stop $app_package"
# Re execute the app
$ADB shell "am start -n \"$app_package/$app_package.$MAIN_ACTIVITY\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER"
2. Bind it with AS Run
Go to Run -> Edit Configurations
Do the following changes on General tab (of your module)
Installation Options->Deplay: Nothing
Launch Options->Launch: Nothing
Before launch: press +, then Run External Tool, to select your script.
In the new dialog:
set any name.
On 'Tool Settings'->Program: navigate to the project's dir, and select your script
Caveats :
First installation
The device needs to be restarted (adb reboot) only once, on the very first installation of your app. Afterwards, you can simply press Run and everything will happen automatically.
This is because the host compiler (dex2oat) is not invoked automatically. Somehow the OS is not yet informed for this new system app. Calling dex2oat manually should solve this, but I had no luck. If anyone solves it please share.
adb root issues
Sometimes (usually the initial execution after the restart) the call to adb root does not find the device. You can simply re-play from AStudio, or sleep for a second after a successful adb root.
using su instead of adb root
adb push won't be working despite mounting system and giving permissions. To make it work replace the ADB_SH variable and the install section of the script with the following:
..
ADB_SH="$ADB shell su -c"
..
# Install APK: using adb su
$ADB_SH "mount -o rw,remount /system"
$ADB_SH "chmod 777 /system/lib/"
$ADB_SH "mkdir -p /sdcard/tmp" 2> /dev/null
$ADB_SH "mkdir -p $apk_target_dir" 2> /dev/null
$ADB push $apk_host /sdcard/tmp/$apk_name 2> /dev/null
$ADB_SH "mv /sdcard/tmp/$apk_name $apk_target_sys"
$ADB_SH "rmdir /sdcard/tmp" 2> /dev/null
Windows script for those interested:
Store this file the same way: in the root of your project directory (installSysPrivApp.bat)
::WIN BATCH SCRIPT
:: CHANGE THESE
set app_package=com.example.package
set dir_app_name=app
set MAIN_ACTIVITY=MainActivity
set ADB="adb"
::ADB_SH="%ADB% shell" # this script assumes using `adb root`. for `adb su`
see `Caveats`
set path_sysapp=/system/priv-app
set apk_host=.\Application\build\outputs\apk\Application-debug.apk
set apk_name=%dir_app_name%.apk
set apk_target_dir=%path_sysapp%/%dir_app_name%
set apk_target_sys=%apk_target_dir%/%apk_name%
:: Delete previous APK
del %apk_host%
:: Compile the APK: you can adapt this for production build, flavors, etc.
call gradlew assembleDebug
set ADB_SH=%ADB% shell su -c
:: Install APK: using adb su
%ADB_SH% mount -o rw,remount /system
%ADB_SH% chmod 777 /system/lib/
%ADB_SH% mkdir -p /sdcard/tmp
%ADB_SH% mkdir -p %apk_target_dir%
%ADB% push %apk_host% /sdcard/tmp/%apk_name%
%ADB_SH% mv /sdcard/tmp/%apk_name% %apk_target_sys%
%ADB_SH% rmdir /sdcard/tmp
:: Give permissions
%ADB_SH% chmod 755 %apk_target_dir%
%ADB_SH% chmod 644 %apk_target_sys%
::Unmount system
%ADB_SH% mount -o remount,ro /
:: Stop the app
%ADB% shell am force-stop %app_package%
:: Re execute the app
%ADB% shell am start -n \"%app_package%/%app_package%.%MAIN_ACTIVITY%\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
To bypass reboot issue from the #paschalis answer reinstall application with a help of package manager before remounting system to read only:
# Reinstall app
$ADB_SH "pm install -r $apk_target_sys"
# Unmount system
$ADB_SH "mount -o remount,ro /"
Package manager will invoke dex2oat by itself.
(Android Q >> Windows)
::WIN BATCH SCRIPT
::setup emulator https://stackoverflow.com/a/64397712/13361987
:: CHANGE THESE
set app_package=com.project.package
set dir_app_name=NewApkName
set MAIN_ACTIVITY=Package.MainActivity
set ADB="adb"
set path_sysapp=/system/priv-app
set apk_host=.\app\build\outputs\apk\debug\app-debug.apk
set apk_name=%dir_app_name%.apk
set apk_target_dir=%path_sysapp%/%dir_app_name%
set apk_target_sys=%apk_target_dir%/%apk_name%
:: Delete previous APK
del %apk_host%
:: Compile the APK: you can adapt this for production build, flavors, etc.
call gradlew assembleDebug
set ADB_SH=%ADB% shell su 0
:: Install APK: using adb su
%ADB_SH% mount -o remount,rw /system
%ADB_SH% chmod 777 /system/lib/
%ADB_SH% mkdir -p /sdcard/tmp
%ADB_SH% mkdir -p %apk_target_dir%
%ADB% push %apk_host% /sdcard/tmp/%apk_name%
%ADB_SH% mv /sdcard/tmp/%apk_name% %apk_target_sys%
%ADB_SH% rm -r /sdcard/tmp
:: Give permissions
%ADB_SH% chmod 755 %apk_target_dir%
%ADB_SH% chmod 644 %apk_target_sys%
:: Unmount system
%ADB_SH% mount -o remount,ro /
:: Stop the app
%ADB% shell am force-stop %app_package%
:: Re execute the app
%ADB% shell am start -n \"%app_package%/%app_package%.%MAIN_ACTIVITY%\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
:: from >> https://stackoverflow.com/questions/28302833/how-to-install-an-app-in-system-app-while-developing-from-android-studio
For mac
By using the script of #Paschalis I got 3 problems first I couldn't mount the system from adb so I did it with "terminal emulator for android" from jack palevich only (needed once) https://play.google.com/store/apps/details?id=jackpal.androidterm
mount -o rw,remount /system
the second problem was the JRE that was not the same as Android studio.
so I added
export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home/
to have the same version of java that android studio was using ("ctrl + ;" in android studio to get this path)
And the last problem because of adb root that can not run in production build so I flashed this zip with magisk
https://github.com/evdenis/adb_root
Android: adbd cannot run as root in production builds
but then my phone was not detected anymore so I removed adb root and this time all works well.
Also try to run the script manually line by line in a terminal to debug this script android studio does not give all the error.
I think adb push *.apk /system/app/*.apk should do just fine.
I don't know about Android Studio, but if you are on Linux you can get try to create an alias for
adb install
that points to that command, It should work!

Get Application Name/ Label via ADB Shell or Terminal

I'm developing an application that uses ADB Shell to interface with android devices, and I need some way of printing out the application name or label of an application, given maybe their package name.
In short, I need a way of getting app names (i.e. "Angry Birds v1.0.0") for user installed applications through adb shell.
Any light on the matter? Any help is appreciated on this.
adb shell pm list packages will give you a list of all installed package names.
You can then use dumpsys | grep -A18 "Package \[my.package\]" to grab the package information such as version identifiers etc
just enter the following command on command prompt after launching the app:
adb shell dumpsys window windows | find "mCurrentFocus"
if executing the command on linux terminal replace find by grep
If you know the app id of the package (like org.mozilla.firefox), it is easy.
First to get the path of actual package file of the appId,
$ adb shell pm list packages -f com.google.android.apps.inbox
package:/data/app/com.google.android.apps.inbox-1/base.apk=com.google.android.apps.inbox
Now you can do some grep|sed magic to extract the path : /data/app/com.google.android.apps.inbox-1/base.apk
After that aapt tool comes in handy :
$ adb shell aapt dump badging /data/app/com.google.android.apps.inbox-1/base.apk
...
application-label:'Inbox'
application-label-hi:'Inbox'
application-label-ru:'Inbox'
...
Again some grep magic to get the Label.
A shell script to accomplish this:
#!/bin/bash
# Remove whitespace
function remWS {
if [ -z "${1}" ]; then
cat | tr -d '[:space:]'
else
echo "${1}" | tr -d '[:space:]'
fi
}
for pkg in $(adb shell pm list packages -3 | cut -d':' -f2); do
apk_loc="$(adb shell pm path $(remWS $pkg) | cut -d':' -f2 | remWS)"
apk_name="$(adb shell aapt dump badging $apk_loc | pcregrep -o1 $'application-label:\'(.+)\'' | remWS)"
apk_info="$(adb shell aapt dump badging $apk_loc | pcregrep -o1 '\b(package: .+)')"
echo "$apk_name v$(echo $apk_info | pcregrep -io1 -e $'\\bversionName=\'(.+?)\'')"
done
Inorder to find an app's name (application label), you need to do the following:
(as shown in other answers)
Find the APK path of the app whose name you want to find.
Using aapt command, find the app label.
But devices don't ship with the aapt binary out-of-the-box.
So you will need to install it first. You can download it from here:
https://github.com/Calsign/APDE/tree/master/APDE/src/main/assets/aapt-binaries
Check this guide for complete steps:
How to find an app name using package name through ADB Android?
(Disclaimer: I am the author of that blog post)
This is what I just came up with. It gives a few errors but works well enough for my needs, matching package names to labels.
It pulls copies of all packages into subdirectories of $PWD, so keep that in mind if storage is a concern.
#!/bin/bash
TOOLS=~/Downloads/adt-bundle-linux-x86_64-20130717/sdk/build-tools/19.1.0
AAPT=$TOOLS/aapt
PMLIST=adb_shell_pm_list_packages_-f.txt
TEMP=$(echo $(adb shell mktemp -d -p /data/local/tmp) | sed 's/\r//')
mkdir -p packages
[ -f $PMLIST ] || eval $(echo $(basename $PMLIST) | tr '_' ' ') > $PMLIST
while read line; do
package=${line##*:}
apk=${package%%=*}
name=${package#*=}
copy=packages$apk
mkdir -p $(dirname $copy)
if [ ! -s $copy ]; then # copy it because `adb pull` doesn't see /mnt/expand/
adb shell cp -f $apk $TEMP/copy.apk
adb pull $TEMP/copy.apk $copy
fi
label=$($AAPT dump badging $copy || echo ERROR in $copy >&2 | \
sed -n 's/^application-label:\(.\)\(.*\)\1$/\2/p')
echo $name:$label
done < <(sed 's/\r//' $PMLIST)
adb shell rm -rf $TEMP
So I extremely grateful to jcomeau_ictx for providing the info on how to extract application-label info from apk and the idea to pull apk from phone directly!
However I had to make several alteration to script it self:
while read line; do done are breaking as a result of commands within while loop interacting with stdin/stdout and as a result while loop runs only once and then stops, as it is discussed in While loop stops reading after the first line in Bash - the comment from cmo I used solution provided and switched while loop to use unused file descriptor number 9.
All that the script really need is a package name and adb shell pm list packages -f is really excessive so I changed it to expect a file with packages list only and provided example on how one can get one from adb.
jcomeau_ictx script variant do not take in to account that some packages may have multiple apk associated with them which breaks the script.
And the least and last, I made every variable to start with underscore, it's just something that makes it easier to read script.
So here another variant of the same script:
#!/bin/bash
_TOOLS=/opt/android-sdk-update-manager/build-tools/29.0.3
_AAPT=${_TOOLS}/aapt
#adb shell pm list packages --user 0 | sed -e 's|^package:||' | sort >./packages_list.txt
_PMLIST=packages_list.txt
rm ./packages_list_with_names.txt
_TEMP=$(echo $(adb shell mktemp -d -p /data/local/tmp) | sed 's/\r//')
mkdir -p packages
[ -f ${_PMLIST} ] || eval $(echo $(basename ${_PMLIST}) | tr '_' ' ') > ${_PMLIST}
while read -u 9 _line; do
_package=${_line##*:}
_apkpath=$(adb shell pm path ${_package} | sed -e 's|^package:||' | head -n 1)
_apkfilename=$(basename "${_apkpath}")
adb shell cp -f ${_apkpath} ${_TEMP}/copy.apk
adb pull ${_TEMP}/copy.apk ./packages
_name=$(${_AAPT} dump badging ./packages/copy.apk | sed -n 's|^application-label:\(.\)\(.*\)\1$|\2|p' )
#'
echo "${_package} - ${_name}" >>./packages_list_with_names.txt
done 9< ${_PMLIST}
adb shell rm -rf $TEMP

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