xref: /aosp_15_r20/prebuilts/sdk/tools/windows/bin/apksigner.bat (revision 344a7f5ef16c479e7a7f54ee6567a9d112f9e72b)
1*344a7f5eSAndroid Build Coastguard Worker@echo off
2*344a7f5eSAndroid Build Coastguard WorkerREM Copyright (C) 2016 The Android Open Source Project
3*344a7f5eSAndroid Build Coastguard WorkerREM
4*344a7f5eSAndroid Build Coastguard WorkerREM Licensed under the Apache License, Version 2.0 (the "License");
5*344a7f5eSAndroid Build Coastguard WorkerREM you may not use this file except in compliance with the License.
6*344a7f5eSAndroid Build Coastguard WorkerREM You may obtain a copy of the License at
7*344a7f5eSAndroid Build Coastguard WorkerREM
8*344a7f5eSAndroid Build Coastguard WorkerREM     http://www.apache.org/licenses/LICENSE-2.0
9*344a7f5eSAndroid Build Coastguard WorkerREM
10*344a7f5eSAndroid Build Coastguard WorkerREM Unless required by applicable law or agreed to in writing, software
11*344a7f5eSAndroid Build Coastguard WorkerREM distributed under the License is distributed on an "AS IS" BASIS,
12*344a7f5eSAndroid Build Coastguard WorkerREM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*344a7f5eSAndroid Build Coastguard WorkerREM See the License for the specific language governing permissions and
14*344a7f5eSAndroid Build Coastguard WorkerREM limitations under the License.
15*344a7f5eSAndroid Build Coastguard Worker
16*344a7f5eSAndroid Build Coastguard WorkerREM don't modify the caller's environment
17*344a7f5eSAndroid Build Coastguard Workersetlocal
18*344a7f5eSAndroid Build Coastguard Worker
19*344a7f5eSAndroid Build Coastguard WorkerREM Locate apksigner.jar in the directory where apksigner.bat was found and start it.
20*344a7f5eSAndroid Build Coastguard Worker
21*344a7f5eSAndroid Build Coastguard WorkerREM Set up prog to be the path of this script, including following symlinks,
22*344a7f5eSAndroid Build Coastguard WorkerREM and set up progdir to be the fully-qualified pathname of its directory.
23*344a7f5eSAndroid Build Coastguard Workerset prog=%~f0
24*344a7f5eSAndroid Build Coastguard Worker
25*344a7f5eSAndroid Build Coastguard Worker@rem Find java.exe
26*344a7f5eSAndroid Build Coastguard Workerif defined JAVA_HOME goto findJavaFromJavaHome
27*344a7f5eSAndroid Build Coastguard Worker
28*344a7f5eSAndroid Build Coastguard Workerset JAVA_EXE=java.exe
29*344a7f5eSAndroid Build Coastguard Worker%JAVA_EXE% -version >NUL 2>&1
30*344a7f5eSAndroid Build Coastguard Workerif "%ERRORLEVEL%" == "0" goto init
31*344a7f5eSAndroid Build Coastguard Worker
32*344a7f5eSAndroid Build Coastguard Workerecho.
33*344a7f5eSAndroid Build Coastguard Workerecho ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
34*344a7f5eSAndroid Build Coastguard Workerecho.
35*344a7f5eSAndroid Build Coastguard Workerecho Please set the JAVA_HOME variable in your environment to match the
36*344a7f5eSAndroid Build Coastguard Workerecho location of your Java installation.
37*344a7f5eSAndroid Build Coastguard Workerexit /b 1
38*344a7f5eSAndroid Build Coastguard Worker
39*344a7f5eSAndroid Build Coastguard Worker:findJavaFromJavaHome
40*344a7f5eSAndroid Build Coastguard Workerset JAVA_HOME=%JAVA_HOME:"=%
41*344a7f5eSAndroid Build Coastguard Workerset JAVA_EXE=%JAVA_HOME%/bin/java.exe
42*344a7f5eSAndroid Build Coastguard Worker
43*344a7f5eSAndroid Build Coastguard Workerif exist "%JAVA_EXE%" goto init
44*344a7f5eSAndroid Build Coastguard Worker
45*344a7f5eSAndroid Build Coastguard Workerecho.
46*344a7f5eSAndroid Build Coastguard Workerecho ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
47*344a7f5eSAndroid Build Coastguard Workerecho.
48*344a7f5eSAndroid Build Coastguard Workerecho Please set the JAVA_HOME variable in your environment to match the
49*344a7f5eSAndroid Build Coastguard Workerecho location of your Java installation.
50*344a7f5eSAndroid Build Coastguard Workerexit /b 1
51*344a7f5eSAndroid Build Coastguard Worker
52*344a7f5eSAndroid Build Coastguard Worker:init
53*344a7f5eSAndroid Build Coastguard Workerset jarfile=apksigner.jar
54*344a7f5eSAndroid Build Coastguard Workerset "frameworkdir=%~dp0"
55*344a7f5eSAndroid Build Coastguard Workerrem frameworkdir must not end with a dir sep.
56*344a7f5eSAndroid Build Coastguard Workerset "frameworkdir=%frameworkdir:~0,-1%"
57*344a7f5eSAndroid Build Coastguard Worker
58*344a7f5eSAndroid Build Coastguard Workerif exist "%frameworkdir%\%jarfile%" goto JarFileOk
59*344a7f5eSAndroid Build Coastguard Worker    set "frameworkdir=%~dp0lib"
60*344a7f5eSAndroid Build Coastguard Worker
61*344a7f5eSAndroid Build Coastguard Workerif exist "%frameworkdir%\%jarfile%" goto JarFileOk
62*344a7f5eSAndroid Build Coastguard Worker    set "frameworkdir=%~dp0..\framework"
63*344a7f5eSAndroid Build Coastguard Worker
64*344a7f5eSAndroid Build Coastguard Worker:JarFileOk
65*344a7f5eSAndroid Build Coastguard Worker
66*344a7f5eSAndroid Build Coastguard Workerset "jarpath=%frameworkdir%\%jarfile%"
67*344a7f5eSAndroid Build Coastguard Worker
68*344a7f5eSAndroid Build Coastguard Workerset javaOpts=
69*344a7f5eSAndroid Build Coastguard Workerset args=
70*344a7f5eSAndroid Build Coastguard Worker
71*344a7f5eSAndroid Build Coastguard WorkerREM By default, give apksigner a max heap size of 1 gig and a stack size of 1meg.
72*344a7f5eSAndroid Build Coastguard Workerrem This can be overridden by using "-JXmx..." and "-JXss..." options below.
73*344a7f5eSAndroid Build Coastguard Workerset defaultXmx=-Xmx1024M
74*344a7f5eSAndroid Build Coastguard Workerset defaultXss=-Xss1m
75*344a7f5eSAndroid Build Coastguard Worker
76*344a7f5eSAndroid Build Coastguard WorkerREM Capture all arguments that are not -J options.
77*344a7f5eSAndroid Build Coastguard WorkerREM Note that when reading the input arguments with %1, the cmd.exe
78*344a7f5eSAndroid Build Coastguard WorkerREM automagically converts --name=value arguments into 2 arguments "--name"
79*344a7f5eSAndroid Build Coastguard WorkerREM followed by "value". apksigner has been changed to know how to deal with that.
80*344a7f5eSAndroid Build Coastguard Workerset params=
81*344a7f5eSAndroid Build Coastguard Worker
82*344a7f5eSAndroid Build Coastguard Worker:firstArg
83*344a7f5eSAndroid Build Coastguard Workerif [%1]==[] goto endArgs
84*344a7f5eSAndroid Build Coastguard Workerset "a=%~1"
85*344a7f5eSAndroid Build Coastguard Worker
86*344a7f5eSAndroid Build Coastguard Worker    if [%defaultXmx%]==[] goto notXmx
87*344a7f5eSAndroid Build Coastguard Worker    if "%a:~0,5%" NEQ "-JXmx" goto notXmx
88*344a7f5eSAndroid Build Coastguard Worker        set defaultXmx=
89*344a7f5eSAndroid Build Coastguard Worker    :notXmx
90*344a7f5eSAndroid Build Coastguard Worker
91*344a7f5eSAndroid Build Coastguard Worker    if [%defaultXss%]==[] goto notXss
92*344a7f5eSAndroid Build Coastguard Worker    if "%a:~0,5%" NEQ "-JXss" goto notXss
93*344a7f5eSAndroid Build Coastguard Worker        set defaultXss=
94*344a7f5eSAndroid Build Coastguard Worker    :notXss
95*344a7f5eSAndroid Build Coastguard Worker
96*344a7f5eSAndroid Build Coastguard Worker    if "%a:~0,2%" NEQ "-J" goto notJ
97*344a7f5eSAndroid Build Coastguard Worker        set javaOpts=%javaOpts% -%a:~2%
98*344a7f5eSAndroid Build Coastguard Worker        shift /1
99*344a7f5eSAndroid Build Coastguard Worker        goto firstArg
100*344a7f5eSAndroid Build Coastguard Worker
101*344a7f5eSAndroid Build Coastguard Worker    :notJ
102*344a7f5eSAndroid Build Coastguard Worker    set params=%params% %1
103*344a7f5eSAndroid Build Coastguard Worker    shift /1
104*344a7f5eSAndroid Build Coastguard Worker    goto firstArg
105*344a7f5eSAndroid Build Coastguard Worker
106*344a7f5eSAndroid Build Coastguard Worker:endArgs
107*344a7f5eSAndroid Build Coastguard Worker
108*344a7f5eSAndroid Build Coastguard Workerset javaOpts=%javaOpts% %defaultXmx% %defaultXss%
109*344a7f5eSAndroid Build Coastguard Workercall "%java_exe%" %javaOpts% -jar "%jarpath%" %params%
110*344a7f5eSAndroid Build Coastguard Worker
111