zipalign: command not found - Ubuntu - android

I am trying to zipalign an "input.apk" file on an Ubuntu 14.04 LTS system using the command line as I do not have access to the source code just yet. If I'm not mistaken I should be able to do this with the following command
zipalign [-f] [-v] 4 intput.apk output.apk
but I am getting the following output
zipalign: command not found
I have made sure that the zipalign file is in my ...sdk/tools directory which I had to copy over from my ...build-tools/android-4.4W folder because it was originally missing. When I input this line as suggested in another question
./zipalign [-f] [-v] 4 intput.apk output.apk
I get the following output
Zip alignment utility
Copyright (C) 2009 The Android Open Source Project
Usage: zipalign [-f] [-v] <align> infile.zip outfile.zip
zipalign -c [-v] <align> infile.zip
<align>: alignment in bytes, e.g. '4' provides 32-bit alignment
-c: check alignment only (does not modify file)
-f: overwrite existing outfile.zip
-v: verbose output
Does this mean that I need a .zip file instead of my .apk to zipalign?

When a usage message contains an argument in brackets, the brackets mean that that argument is optional and can be left out of the final command; the brackets are not themselves part of the command syntax.
In your case, correct usage might look like:
./zipalign -v 4 intput.apk output.apk

In terminal,
cd /opt/android-sdk/build-tools/21.1.2
sudo ln -sf zipalign /usr/bin/
zipalign -v 4 platforms/android/ant-build/MainActivity-release-unsigned.apk platforms/android/ant-build/YOUR_APP.apk

Open terminal (CTRL + t)
cd YOUR_PATH/android-sdk-linux/build-tools/XX.X.X
sudo cp zipalign /usr/bin/
Open the folder where is located your apk in the terminal.
Execute zipalign -v 4 YOUR_APK.apk YOUR_APK.apk

I had the same issue. This is how i solved it, all you need is here.
1. Get zipalign path by getting sdk path on android studio. which is /Users/s****/Library/Android/sdk for me.
2. Paste the path on your terminal and cd to "build-tools/28.0.3/zipalign" now command will be/Users/s****/Library/Android/sdk/build-tools/28.0.3/zipalign
3. zip the apk on the same command line by adding -v 4 app-release-unsigned.apk my.apkto the command, now command will be
/Users/s****/Library/Android/sdk/build-tools/28.0.3/zipalign -v 4 app-release-unsigned.apk my.apk
4. Done.

Related

Publishing Ionic app - Zipalign not working

I am trying to get my Ionic app published. However, when trying to zipalign the apk with the following command
/Users/bertcarremans/Library/Android/sdk/build-tools/23.0.3/zipalign zipalign -v 4 android-release-unsigned.apk chartly.apk
I get the message below:
Zip alignment utility
Copyright (C) 2009 The Android Open Source Project
Usage: zipalign [-f] [-p] [-v] [-z] <align> infile.zip outfile.zip
zipalign -c [-v] <align> infile.zip
<align>: alignment in bytes, e.g. '4' provides 32-bit alignment
-c: check alignment only (does not modify file)
-f: overwrite existing outfile.zip
-p: page align stored shared object files
-v: verbose output
-z: recompress using Zopfli
How can I make the zipalign tool to work? Thanks!
I found the solution.
Copy the zipalign tool to the apk folder of your app. On my computer the zipalign tool was located in /Users/bertcarremans/Library/Android/23.0.3
Then run the command ./zipalign -v 4 android-release-unsigned.apk chartly.apk
There is no need to copy zipalign file anywhere, if it is installed through command line a symbolic link will be created and then you can run it from anywhere.
You are not using the right syntax of the zipalign tool. Please check again, you have written zipalign twice so you are passing zipalign as a parameter to zipalign command which is not correct.

Android SDK path issue when using calabash-android

I'm getting a "Could not find an Android SDK please make sure it is installed" error when trying to run calabash-android commands. I've installed the Android SDK via brew. Here are the contents of my .bash_profile:
ANDROID_HOME=/usr/local/Cellar/android-sdk/24.2
PATH=$PATH:$ANDROID_HOME/tools
PATH=$PATH:$ANDROID_HOME/platform-tools
PATH=$PATH:$ANDROID_HOME/build-tools
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
[[ -s "$HOME/.profile" ]] && source "$HOME/.profile" # Load the default .profile
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
PATH=$PATH:/usr/local/apache-ant-1.9.4/bin
PATH=$PATH:/usr/local/share/npm/bin
JAVA_HOME=/usr/libexec/java_home
export PATH
Here's my $PATH:
echo $PATH
/Users/mchumak/.rvm/gems/ruby-2.1.1/bin:/Users/mchumak/.rvm/gems/ruby-2.1.1#global/bin:/Users/mchumak/.rvm/rubies/ruby-2.1.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/Cellar/android-sdk/24.2/tools:/usr/local/Cellar/android-sdk/24.2/platform-tools:/usr/local/Cellar/android-sdk/24.2/build-tools:/Users/mchumak/.rvm/bin:/Users/mchumak/.rvm/bin:/opt/local/bin:/opt/local/sbin:/Users/mchumak/.rvm/bin:/usr/local/apache-ant-1.9.4/bin:/usr/local/share/npm/bin
"which android" produces:
/usr/local/bin/android
The contents of that particular file are:
#!/bin/bash
TOOL="/usr/local/Cellar/android-sdk/24.2/tools/android"
exec "$TOOL" "$#"
I can run the Android SDK Manager from the command line just fine, and it shows the correct SDK path (/usr/local/Cellar/android-sdk/24.2).
I'm stumped. Any ideas why calabash-android can't find the SDK?
This will fix your issue,
echo "export PATH=$PATH:/Users/#username/Library/Android/sdk/platform-tools/sdk/platform-tools/" >> ~/.bash_profile
install android-platform-tools -> brew install android-platform-tools
- Also ensure yourself have admin rights
Looking at your $PATH output seems like android sdk path isnt loaded into PATH. Add below to your .bash_profile
PATH=$PATH:$ANDROID_HOME
calabash-android run /Users/marcg/Downloads/app-news.apk
I received that following error
Could not find any platform directory in '/Users/marcg/Library/Android/sdk/platforms'
I corrected this with the symlink below:
/Users/marcg/Library/Android/sdk/platforms
platforms$ ln -s ../platform-tools/ platform
I ran again and get an error that the android jar was missing from a standard directory of /Users/marcg/Library/Android/sdk/platforms
I then copied that android.jar to this platforms dir
Dir now looks like:
/Users/marcg/Library/Android/sdk/platforms
us164912:platforms marcg$ ls -l
total 49640
-rwxr-xr-x 1 marcg Users 25409822 Oct 5 09:26 android.jar
lrwxr-xr-x 1 marcg Users 18 Oct 5 09:14 platform -> ../platform-tools/
Still getting
Did not find 'android.jar' in any standard directory of '/Users/marcg/Library/Android/sdk/platforms'. Calabash will therefore take longer to load
I copied the android.jar to the platform-tools directory and remaining error went away.
Steps to correct:
Make a symlink in platforms that points to platform-tools
ln -s ../platform-tools/ platform
Copy the android.jar to the platform-tools dir
Would be better if the framework allowed you configure via file or additional env vars where to find each of these. My earlier attempt to set ANDROID_HOME to the platform-tools dir didn't work and why I did a symlink.

Build Android Studio app via command line

I want to build an Android Studio app (the Gradle build system), but I want to do this via the command line.
Android Studio automatically creates a Gradle wrapper in the root of your project, which is how it invokes Gradle. The wrapper is basically a script that calls through to the actual Gradle binary and allows you to keep Gradle up to date, which makes using version control easier. To run a Gradle command, you can simply use the gradlew script found in the root of your project (or gradlew.bat on Windows) followed by the name of the task you want to run. For instance, to build a debug version of your Android application, you can run ./gradlew assembleDebug from the root of your repository. In a default project setup, the resulting apk can then be found in app/build/outputs/apk/app-debug.apk. On a *nix machine, you can also just run find . -name '*.apk' to find it, if it's not there.
there are two build types to build your application using the Gradle build settings: one for debugging your application — debug — and one for building your final package for release — release mode.
Building in Debug Mode
First Navigate to Android studio project Root folder using CMD
run this command gradlew.bat assembleDebug
Output window look like this
Build signed apk in Release Mode
Edit the build.gradle file to build your project in release mode:
android {
...
defaultConfig { ... }
signingConfigs {
release {
storeFile file("myreleasekey.keystore")
storePassword "password"
keyAlias "MyReleaseKey"
keyPassword "password"
}
}
buildTypes {
release {
...
signingConfig signingConfigs.release
}
}}
run this command gradlew.bat assembleRelease
Try this (OS X only):
brew install homebrew/versions/gradle110
gradle build
You can use gradle tasks to see all tasks available for the current project. No Android Studio is needed here.
1. Install Gradle and the Android SDK
Either
Install these however you see fit
Run ./gradlew, or gradlew.bat if on Windows
chmod +x ./gradlew may be necessary
From this point onwards, gradle refers to running Gradle whichever way you've chosen.
Substitute accordingly.
2. Setup the Android SDK
If you've manually installed the SDK
export ANDROID_HOME=<install location>
You may want to put that in your ~/.profile if it's not done automatically
Accept the licenses: yes | sdkmanager --licenses
sdkmanager can be found in $ANDROID_HOME/tools/bin
sdkmanager may have to be run as root
Try running gradle
If there are complaints about licenses or SDKs not being found, fix the
directory permissions
chown -R user:group $ANDROID_HOME
If you're reckless and/or the only user: chmod 777 -R $ANDROID_HOME
3. Building
gradle tasks lists all tasks that can be run
:app:[appname] is the prefix of all tasks, which you'll see in the Gradle
logs when you're building
This can be excluded when running a task
Some essential tasks
gradle assemble: build all variants of your app
Resulting .apks are in app/[appname]/build/outputs/apk/[debug/release]
gradle assembleDebug or assembleRelease: build just the debug or release versions
gradle installDebug or installRelease build and install to an attached device
Have adb installed
Attach a device with USB debugging and USB file transfer enabled
Run adb devices, check that your device is listed and device is
beside it
Automatically build and install upon changes
This avoids having to continuously run the same commands
gradle -t --continue installDebug
-t: aka --continuous, automatically re-runs the task after a file is changed
--continue: Continue after errors. Prevents stopping when errors occur
Run gradle -h for more help
You're likely here because you want to install 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, to make it a 2char command.
alias bi="gradlew && adb install -r exampleApp.apk"
(Short for Build and Install)
Cheatsheet for running Gradle from the command line for Android Studio projects on Linux:
cd <project-root>
./gradlew
./gradlew tasks
./gradlew --help
Should get you started..
I faced the same problem and seems that there have been many changes by google.
I can tell you the steps for installing purely via command line from scratch.
I tested it on Ubuntu on 22 Feb 2021.
create sdk folder
export ANDROID_SDK_ROOT=/usr/lib/android-sdk
sudo mkdir -p $ANDROID_SDK_ROOT
install openjdk
sudo apt-get install openjdk-8-jdk
download android sdk
Go to https://developer.android.com/studio/index.html
Then down to Command line tools only
Click on Linux link, accept the agreement and instead of downloading right click and copy link address
cd $ANDROID_SDK_ROOT
sudo wget https://dl.google.com/android/repository/commandlinetools-linux-6858069_latest.zip
sudo unzip commandlinetools-linux-6858069_latest.zip
move folders
Rename the unpacked directory from cmdline-tools to tools,
and place it under $ANDROID_SDK_ROOT/cmdline-tools,
so now it should look like: $ANDROID_SDK_ROOT/cmdline-tools/tools.
And inside it, you should have: NOTICE.txt bin lib source.properties.
set path
PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/cmdline-tools/tools/bin
This had no effect for me, hence the next step
browse to sdkmanager
cd $ANDROID_SDK_ROOT/cmdline-tools/tools/bin
accept licenses
yes | sudo sdkmanager --licenses
create build
Finally, run this inside your project
chmod 777 gradlew
sudo ./gradlew assembleDebug
This creates an APK named -debug.apk at //build/outputs/apk/debug
The file is already signed with the debug key and aligned with zipalign,
so you can immediately install it on a device.
FINAL STEPS
Here are the final steps. Make 2 .sh files with these contents. Use chmod 777 before on both. No sudo required.
Download_APK_Code_NOSUDO4.sh
# Don't forget to do chmod 777 Download_APK_Code_NOSUDO2.sh
#!/bin/bash
if [ -d "camera-samples" ]; then
echo "############################# Deleting older code base. ######################################"
rm -rf camera-samples
fi
echo "########################### Download Source Code: Start ... #####################################"
git clone git://git.quicinc.com/camera-samples -b iot-concam-apk.lnx.1.1
echo "########################## Download Source Code: Done . . . ####################################"
Build_App_NOSUDO4.sh
# Don't forget to do chmod 777 Build_App_NOSUDO2.sh
#!/bin/bash
currentDir=$(pwd)
export ANDROID_SDK_ROOT=$(pwd)
# echo "############################################ Install JDK ... ################################################"
# apt-get install openjdk-8-jdk
if [ -e "commandlinetools-linux-6858069_latest.zip" ]; then
echo "############################# Deleting older zip file. ######################################"
rm -rf commandlinetools-linux-6858069_latest.zip
fi
echo "########################################### Download Command Line Tools .. ###################################"
wget https://dl.google.com/android/repository/commandlinetools-linux-6858069_latest.zip
echo "########################################### Download Command Line Tools Done .. ##############################"
if [ -d "cmdline-tools" ]; then
echo "############################# Deleting older cmdline-tools. ######################################"
rm -rf cmdline-tools
fi
echo "########################################### Unzip Command Line Tools Start .. #################################"
unzip commandlinetools-linux-6858069_latest.zip
echo "########################################### Unzip Command Line Tools Done .. #################################"
echo "########################################### Creating Directory Structure .. #################################"
mv cmdline-tools tools
mkdir cmdline-tools
cp -r tools cmdline-tools/
rm -rf tools/
PATH=$PATH:$ANDROID_SDK_ROOT/cmdline-tools/latest/bin:$ANDROID_SDK_ROOT/cmdline-tools/tools/bin
echo "########################################## Updated Path : $PATH ###############################################"
cd $ANDROID_SDK_ROOT/cmdline-tools/tools/bin
echo "########################################## Accept All Licenses .. #############################################"
yes | sdkmanager --licenses
cd $currentDir/camera-samples/Camera2Video
echo "sdk.dir = $ANDROID_SDK_ROOT" > local.properties
echo "######################################## Building APK . . . #################################################"
chmod 777 gradlew
./gradlew assembleDebug
echo "####################################### Building APK Done. . . ############################################"
echo "##################################### APK generated here: $currentDir/app/build/outputs/apk/debug/app-debug.apk ###################################"
##########################################################
Run these commands.
chmod 777 Download_APK_Code_NOSUDO4.sh
chmod 777 Build_App_NOSUDO4.sh
./Download_APK_Code_NOSUDO4.sh
./Build_App_NOSUDO4.sh
REFERENCES
https://gist.github.com/guipmourao/3e7edc951b043f6de30ca15a5cc2be40
Android Command line tools sdkmanager always shows: Warning: Could not create settings
"Failed to install the following Android SDK packages as some licences have not been accepted" error
https://developer.android.com/studio/build/building-cmdline#sign_cmdline
///////////////////////////////////
WINDOWS
Here are the steps for Windows via Powershell. Tested on 6th March 2021. You can start completely from scratch.
Prerequisites
Download git
Install JDK. I used jdk-8u281-windows-x64.exe
Make a file DownloadAndBuild.ps1 with these contents.
$location = Get-Location
if (Test-Path "$location\camera-samples") {
Write-Host "########################### Deleting older code base: Start. ################################"
Remove-Item -Force -Recurse -Path "$location\camera-samples"
Write-Host "########################### Deleting older code base: Done. #################################"
}
Write-Host "########################## Download Source Code: Start. #####################################"
git clone https://source.codeaurora.org/quic/la/camera-samples -b iot-concam-apk.lnx.1.1
Write-Host "########################## Download Source Code: Done. ####################################"
if (Test-Path "$location\commandlinetools-win-6858069_latest.zip") {
Write-Host "########################### Deleting older zip file: Start. ################################"
Remove-Item -Force -Recurse -Path "$location\commandlinetools-win-6858069_latest.zip"
Write-Host "########################### Deleting older zip file: Done. #################################"
}
Write-Host "########################## Download Command Line Tools: Start. #####################################"
$client = new-object System.Net.WebClient
$client.DownloadFile("https://dl.google.com/android/repository/commandlinetools-win-6858069_latest.zip","commandlinetools-win-6858069_latest.zip")
Write-Host "########################## Download Command Line Tools: End. #####################################"
if (Test-Path "$location\cmdline-tools") {
Write-Host "########################### Deleting older folder: Start. ################################"
Remove-Item -Force -Recurse -Path "$location\cmdline-tools"
Write-Host "########################### Deleting older folder: Done. #################################"
}
Write-Host "########################## Extract Command Line Tools: Start. #####################################"
Expand-Archive "$location\commandlinetools-win-6858069_latest.zip" -DestinationPath "$location"
Write-Host "########################## Extract Command Line Tools: End. #####################################"
Write-Host "########################## Create Directory Structure: Start. #####################################"
Rename-Item -Path "$location\cmdline-tools" -newName "$location\tools"
New-Item -ItemType Directory -Force -Path "$location\cmdline-tools"
Move-Item -Path "$location\tools" -Destination "$location\cmdline-tools"
Write-Host "########################## Create Directory Structure: End. #####################################"
Write-Host "########################## Accept Licenses: Start. #####################################"
Set-Location -Path $location/cmdline-tools/tools/bin
for($i=0;$i -lt 100;$i++) { $response += "y`n"}; $response | ./sdkmanager.bat --licenses
Write-Host "########################## Accept Licenses: End. #####################################"
Write-Host "########################## Build APK: Start. #####################################"
Set-Location -Path $location/camera-samples/Camera2Video
$Env:ANDROID_SDK_ROOT = $location
.\gradlew assembleDebug
Write-Host "########################## Build APK: End. #####################################"
Write-Host "##################################### APK generated here: $location/camera-samples/Camera2Video/app/build/outputs/apk/debug/app-debug.apk ###################################"
PAUSE
Right-click and run via Powershell.
This will download an Android project via git, install the SDK and build the Android App.
Edit as per your convenience.
For Mac use this command
./gradlew task-name
MacOS variant
./gradlew <moduleName>:assemble<build_variant>
//e.g
./gradlew <moduleName>:assembleDebug
*./ means current directory
[More info]
Only for MAC Users
Extending Vji's answer.
Step by step procedure:
Open Terminal
Change your directory to your Project(cd PathOfYourProject)
Copy and paste this command and hit enter:
chmod +x gradlew
As Vji suggested:
./gradlew task-name
DON'T FORGOT TO ADD .(DOT) BEFORE /gradlew
Official Documentation is here:
To build a debug APK, open a command line and navigate to the root of your project directory. To initiate a debug build, invoke the assembleDebug task:
gradlew assembleDebug
This creates an APK named module_name-debug.apk in project_name/module_name/build/outputs/apk/.
note, you can also do this within Android Studio by clicking the gradle window, and then the 'elephant' button. This will open a new window called "run anything" (can also be found by searching for that name in 'search everywhere') where you can manually type any gradle command you want in. Not "quite" command line, but often provides more of what I need than windows command line.
This allows you to give optional params to gradle tasks, etc.
enter code hereCreate script file with below gradle and adb command, Execute script file
./gradlew clean
./gradlew assembleDebug
./gradlew installDebug
adb shell am start -n applicationID/full path of launcher activity
Adding value to all these answers,
many have asked the command for running App in AVD after build sucessful.
adb install -r {path-to-your-bild-folder}/{yourAppName}.apk

zipalign' is not recognized as an internal or external command

I Wrote an android app.
I type zipalign myApp.apk on cmd at windows 7 and I get:
zipalign' is not recognized as an internal or external command operable program or batch file
What is the problem
zipalign.exe can be found in the build tools (my path in my PC is C:\Users\[user name]\Developer\adt-bundle-windows-x86_64-20131030\sdk\build-tools), in my builds 19.1.0 and 20.0.0 folders. If you can't find it, fire up Windows Explorer and search for zipalign.exe, likely in C:\, unless you have installed in another drive.
Copy zipalign.exe to the tools folder, same level as build-tools (in my case, it is C:\Users\[user name]\Developer\adt-bundle-windows-x86_64-20131030\sdk\tools). Once done, I could compile successfully.
Go to
Android/sdk/build-tools/VERSION/zipalign
copy the zipalign.exe into the current directory
and then run it.
Example:
Now to optimize the APK , run the following command
E:\app publish\J\JQuery Reference>zipalign -v 4 jquery-reference-unsigned.apk jq
ueryReference.apk
An example on how to run zipalign without adding the path variable from CMD prompt:
C:\Users\mywindowsusername\AppData\Local\Android\Sdk\build-tools\27.0.3\zipalign.exe -v -p 4 my-app-unsigned.apk my-app-unsigned-aligned.apk
You should navigate to the directory wich has the zipalign executable in it.
Zipalign.exe is located in /path/to/android/sdk/build-tools/sdk-number
In command line:
Navigate to the location where zipalign.exe can be found
Run: zipalign -v -f 4 /full/path/to/apk/unaligned-apk.apk /full/path/to/new-aligned-apk.apk
you can also try this:
1.search for zipaligned in main search bar
2.copy it to current directory
3.run your commands without changing anything
4.it works! :)
Try this
zipalign -v 4 app-release-unsigned.apk xxx.apk
instead put infront of above command
./
while you run it in terminal
\platforms\android\app\build\outputs\apk\release>./zipalign -v 4 app-release-unsigned.apk xxx.apk
If you are using powershell like I was, exit and use standard windows command prompt and then it will execute :)
Copy zipalign exe file into the Android\Sdk\build-tools\ folder and add path in environment variables ...
im comming from ionic-doc, and thiew way works form me by adding this to PATH
/path/to/Android/sdk/build-tools/VERSION/zipalign

Asset access error in APK made by using shell script

I wrote the shell script to build APK to automate build process. It's like below.
rm -rf obj
rm -rf gen
rm -rf bin
rm -f build.xml
rm -f local.properties
rm -f proguard-project.txt
ndk-build
android update project --path ./
ant release
jarsigner -verbose -keystore path/keystore -storepass password -signedjar bin/myApp-release-signed.apk bin/myApp-release-unsigned.apk alias
Currently, I got the APK successfully. But I found sometimes app is crashed when it try to access special asset like texture image. LogCat shows the log like below.
04-10 12:46:25.080: E/szipinf(2255): Error reading asset data
04-10 12:46:25.080: E/szipinf(2255): Unable to access asset data: -1
Eclipse can build APK well. I tried to find the way to fox APK's issue. But I cannot find.
Currently, I'm using android-12 and ndk-r6b. Also I'm working on Machintosh. Is there any special guide to fix such a thing?

Categories

Resources