2

I often compare my new build apk size with the production build and I am looking for options to automate this activity such that it compares both new and prod apk sizes and reports me. I am aware of APK Analyzer of Android Studio but I want to do that using command-line tools. This doc lists the usage of apkanalyzer but upon running this command

apkanalyzer -h apk file-size myapk.apk

It says "'apkanalyzer' is not recognized as an internal or external command, operable program or batch file." though I have already set the environment path to \Android\sdk\tools\bin.

Not sure why command-line is not recognizing this command, could you let me know where I could possibly have gone wrong or is there any other way to check apk file size using command-line?

Thanks for any help in advance.

Sudar
  • 81
  • 1
  • 1
  • 7

9 Answers9

14

apkanalyzer is unix shell script, here converted batch script for windows, make sure to change APP_HOME and CLASSPATH to match your setup. save it as apkanalyzer.cmd

@echo off

::##############################################################################
::##
::##  apkanalyzer start up script for Windows
::##
::##  converted by ewwink
::##
::##############################################################################

::Attempt to set APP_HOME

SET SAVED=%cd%
SET APP_HOME=C:\android\sdk\tools
SET APP_NAME="apkanalyzer"

::Add default JVM options here. You can also use JAVA_OPTS and APKANALYZER_OPTS to pass JVM options to this script.
SET DEFAULT_JVM_OPTS=-Dcom.android.sdklib.toolsdir=%APP_HOME%

SET CLASSPATH=%APP_HOME%\lib\dvlib-26.0.0-dev.jar;%APP_HOME%\lib\util-2.2.1.jar;%APP_HOME%\lib\jimfs-1.1.jar;%APP_HOME%\lib\annotations-13.0.jar;%APP_HOME%\lib\ddmlib-26.0.0-dev.jar;%APP_HOME%\lib\repository-26.0.0-dev.jar;%APP_HOME%\lib\sdk-common-26.0.0-dev.jar;%APP_HOME%\lib\kotlin-stdlib-1.1.3-2.jar;%APP_HOME%\lib\protobuf-java-3.0.0.jar;%APP_HOME%\lib\apkanalyzer-cli.jar;%APP_HOME%\lib\gson-2.3.jar;%APP_HOME%\lib\httpcore-4.2.5.jar;%APP_HOME%\lib\dexlib2-2.2.1.jar;%APP_HOME%\lib\commons-compress-1.12.jar;%APP_HOME%\lib\generator.jar;%APP_HOME%\lib\error_prone_annotations-2.0.18.jar;%APP_HOME%\lib\commons-codec-1.6.jar;%APP_HOME%\lib\kxml2-2.3.0.jar;%APP_HOME%\lib\httpmime-4.1.jar;%APP_HOME%\lib\annotations-12.0.jar;%APP_HOME%\lib\bcpkix-jdk15on-1.56.jar;%APP_HOME%\lib\jsr305-3.0.0.jar;%APP_HOME%\lib\explainer.jar;%APP_HOME%\lib\builder-model-3.0.0-dev.jar;%APP_HOME%\lib\baksmali-2.2.1.jar;%APP_HOME%\lib\j2objc-annotations-1.1.jar;%APP_HOME%\lib\layoutlib-api-26.0.0-dev.jar;%APP_HOME%\lib\jcommander-1.64.jar;%APP_HOME%\lib\commons-logging-1.1.1.jar;%APP_HOME%\lib\annotations-26.0.0-dev.jar;%APP_HOME%\lib\builder-test-api-3.0.0-dev.jar;%APP_HOME%\lib\animal-sniffer-annotations-1.14.jar;%APP_HOME%\lib\bcprov-jdk15on-1.56.jar;%APP_HOME%\lib\httpclient-4.2.6.jar;%APP_HOME%\lib\common-26.0.0-dev.jar;%APP_HOME%\lib\jopt-simple-4.9.jar;%APP_HOME%\lib\sdklib-26.0.0-dev.jar;%APP_HOME%\lib\apkanalyzer.jar;%APP_HOME%\lib\shared.jar;%APP_HOME%\lib\binary-resources.jar;%APP_HOME%\lib\guava-22.0.jar

SET APP_ARGS=%*
::Collect all arguments for the java command, following the shell quoting and substitution rules
SET APKANALYZER_OPTS=%DEFAULT_JVM_OPTS% -classpath %CLASSPATH% com.android.tools.apk.analyzer.ApkAnalyzerCli %APP_ARGS%

::Determine the Java command to use to start the JVM.
SET JAVACMD="java"
where %JAVACMD% >nul 2>nul
if %errorlevel%==1 (
  echo ERROR: 'java' command could be found in your PATH.
  echo Please set the 'java' variable in your environment to match the
  echo location of your Java installation.
  echo.
  exit /b 0
)

:: execute apkanalyzer

%JAVACMD% %APKANALYZER_OPTS%
ewwink
  • 18,382
  • 2
  • 44
  • 54
13

While checking the output of appium-doctor command - The following error was seen -

android, apkanalyzer could NOT be found in /Users/{user_name}/Library/Android/sdk!

The solution was to enable Android SDK Command-line tools from Android Studio Preferences

Checkout the screenshot enter image description here

Hope this helps

Xtraterrestrial
  • 651
  • 1
  • 6
  • 12
3

APKAnalyzer tool is fully loaded with the Android Studio.

APK Analyzer tool is available in android studio https://developer.android.com/studio/build/apk-analyzer.html.

Most the command executable options can be used just with the good UI available in the Android Studio.

Takermania
  • 1,345
  • 2
  • 12
  • 20
2

The apkanalyzer file (with no extension) in my sdk>tools>bin installed on my windows 10 pc is a Unix Shell script. Windows doesn't recognize this file as a valid command.

This appears to be a bug in the windows install of the Android SDK Tools 26.1.1.

1

This means that apkanalyzer does not exist in your SDK path.

To download it, just got to SDK Manager and click on Android SDK Tools, then click Ok to continue and download the missing tools.

SDK manager

suhaib
  • 29
  • 2
  • 6
  • 1
    I ensured the apkanalyzer exist in sdk/tools/bin and the sdk tools are updated as well but the problem still persist. – Sudar Nov 06 '17 at 09:19
1

As others have mentioned, make sure you have installed the Android SDK Tools via the SDK Manager. They should show up in <your_sdk_root>/tools/bin. A long time ago I added <your_sdk_root>/tools to my $PATH in my ~/.bash_profile, but never added <your_sdk_root>/tools/bin; apparently I missed the note that as of SDK Tools, Revision 25.3.0 tools were deprecated or otherwise moved to there among other places.

Seee also https://developer.android.com/studio/command-line/ for details on the regular Tools, Build, Platform, and Emulator tools.

qix
  • 7,228
  • 1
  • 55
  • 65
1

In Android Studio > Tools > sdkmanager > install Android SDK Command line tools

This worked for me!

soumya
  • 11
  • 1
0

I've submitted a bug report to Google: https://issuetracker.google.com/issues/124064881

Apparently it's broken on Windows.

Over17
  • 961
  • 7
  • 8
0
  1. be sure that Android Studio "Commandline Tools" installed on your system.
  2. Then, add the following information into your system path;

C:\Users\user\AppData\Local\Android\Sdk\tools C:\Users\user\AppData\Local\Android\Sdk\tools\bin C:\Users\user\AppData\Local\Android\Sdk\cmdline-tools\latest C:\Users\user\AppData\Local\Android\Sdk\cmdline-tools\latest\bin

It worked for me.. M. Yaşar Özden