script/electric-sheep-install-app.pl untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 81 CC 1 SCAR 6.9
CC
1
Cov
0%
usage
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 #!/usr/bin/env perl
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.09';
7  
8 use lib ('blib/lib');
9  
10 use Getopt::Long qw(:config no_ignore_case);
11 use Data::Roundtrip qw/perl2dump perl2json no-unicode-escape-permanently/;
12  
13 use Android::ElectricSheep::Automator;
14  
15 my $VERBOSITY = 0; # we need verbosity of 10 (max), so this is not used
16 my $DEVICE;
17 my $CONFIGFILE;
18 my @APKFILENAMES;
19 my @INSTALL_PARAMS;
20  
21 if( ! Getopt::Long::GetOptions(
22 'device|d=s' => \$DEVICE,
23 'verbosity|v=i' => \$VERBOSITY,
24 'apk-filename|i=s', sub { if( ! -f $_[1] ){ die "$0 : error, input apk filename (".$_[1].") does not exist." } push(@APKFILENAMES, $_[1]); },
25 'configfile|c=s' => \$CONFIGFILE,
26 'install-parameter|p=s' => sub { push @INSTALL_PARAMS, $_[1] },
27 'help|h' => sub { print STDOUT usage(); exit(0); }
28 ) ){ die usage() }
29  
30 if( 0 == scalar @APKFILENAMES ){ print STDERR usage(); print STDERR "\n$0 : error, an input APK filename to be installed must be specified with '--apk-filename'.\n"; exit(1); }
31 if( ! defined $CONFIGFILE ){ print STDERR usage(); print STDERR "\n$0 : error, a configuration file must be specified with '--configfile'.\n"; exit(1); }
32 if( ! -f $CONFIGFILE ){ die "$0 : failed to find config file '$CONFIGFILE'." }
33  
34 my $params = {
35 'configfile' => $CONFIGFILE,
36 'verbosity' => $VERBOSITY,
37 };
38 # we assume there is a device connected which the user
39 # must specify by serial, of if just one, we connect to
40 # it without the serial
41 if( defined $DEVICE ){ $params->{'device-serial'} = $DEVICE }
42 else { $params->{'device-is-connected'} = 1 }
43  
44 my $client = Android::ElectricSheep::Automator->new($params);
45 if( ! defined($client) ){ die "$0 : failed to instantiate the automator." }
46  
47 my $sparams = {
48 'apk-filename' => \@APKFILENAMES,
49 'install-parameters' => \@INSTALL_PARAMS,
50 };
51 my $res = $client->install_app($sparams);
52 if( 1 == $res ){ die perl2dump($sparams)."$0 : failed to install APK file(s) '".join("','", @APKFILENAMES)."'." }
53 print STDOUT "$0 : done, success! APK is now installed on the device.\n";
54  
55 sub usage {
56 return "Usage $0 --apk-filename FILENAME [--apk-filename FILENAME2 ...] --configfile CONFIGFILE [--install-parameter|-p 'PARAM'] [--device DEVICE] [--verbosity v]\n"
57 . "\nThis script will install the specified APK file(s) onto the device. It accepts extra parameters, preferably quoted, using -p 'PARAM', to be passed on to the installation command as per the documentation of adb here: https://developer.android.com/tools/adb. Note that multiple APK files can be part of the same app and need to be specified here one after the other with multiple '--apk-filename' parameters. Order is not important.\n"
58 . "\nExample:\n"
59 . " It installs the specified APK file with -r and -g extra installation parameters (-r: reinstall existing app and keep its existing data, -g: grant all permissions it requires in manifest):\n"
60 . "$0 --configfile config/myapp.conf --apk-filename Gallery2.apk -p '-r' -p '-g'\n"
61 . " It installs multiple APK files part of the same app:\n"
62 . "$0 --configfile config/myapp.conf --apk-filename myapp/base.apk --apk-filename my-app/config.apk\n"
63 . "\nProgram by Andreas Hadjiprocopis (c) 2025 / bliako at cpan.org / andreashad2 at gmail.com\n\n"
64 ;
65 }
66  
67 1;