eclipse 如何使用命令行生成apk文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12667741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 19:06:35  来源:igfitidea点击:

how to generate apk file using a command line?

androideclipsecommand-lineapkcommand-prompt

提问by sam

please tell me how to build apk files of a project without the use of ECLIPSE ide. i found some infos about using a batch file but i don't know how to remake it.

请告诉我如何在不使用 ECLIPSE ide 的情况下构建项目的 apk 文件。我找到了一些关于使用批处理文件的信息,但我不知道如何重新制作它。

echo on

SET PREV_PATH=%CD%
cd /d %0\..

REM Clear bin folder
rmdir "bin" /S /Q
rmdir "gen" /S /Q
mkdir "bin" || goto EXIT
mkdir "gen" || goto EXIT

REM Set your application name
SET APP_NAME=SecureSms

REM Define minimal Android revision
SET ANDROID_REV=android-8

REM Define aapt add command
SET ANDROID_AAPT_ADD="%ANDROID-SDK%\platforms\%ANDROID_REV%\tools\aapt.exe" add

REM Define aapt pack and generate resources command
SET ANDROID_AAPT_PACK="%ANDROID-SDK%\platforms\%ANDROID_REV%\tools\aapt.exe" package -v -f -I "%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar"

REM Define class file generator command
SET ANDROID_DX="%ANDROID-SDK%\platform-tools\dx.bat" --dex

REM Define Java compiler command
SET JAVAC="%JAVABIN%\javac.exe" -classpath "%ANDROID-SDK%\platforms\%ANDROID_REV%\android.jar"
SET JAVAC_BUILD=%JAVAC% -sourcepath "src;gen" -d "bin"

REM Generate R class and pack resources and assets into resources.ap_ file
call %ANDROID_AAPT_PACK% -M "AndroidManifest.xml" -A "assets" -S "res" -m -J "gen" -F "bin\resources.ap_" || goto EXIT

REM Compile sources. All *.class files will be put into the bin folder
call %JAVAC_BUILD% src\org\secure\sms\*.java || goto EXIT

REM Generate dex files with compiled Java classes
call %ANDROID_DX% --output="%CD%\bin\classes.dex" %CD%\bin || goto EXIT

REM Recources file need to be copied. This is needed for signing.
copy "%CD%\bin\resources.ap_" "%CD%\bin\%APP_NAME%.ap_" || goto EXIT

REM Add generated classes.dex file into application package
call %ANDROID_AAPT_ADD% "%CD%\bin\%APP_NAME%.ap_" "%CD%\bin\classes.dex" || goto EXIT

REM Create signed Android application from *.ap_ file. Output and Input files must be different.
call "%JAVABIN%\jarsigner" -keystore "%CD%\keystore\my-release-key.keystore" -storepass "password" -keypass "password" -signedjar "%CD%\bin\%APP_NAME%.apk" "%CD%\bin\%APP_NAME%.ap_" "alias_name" || goto EXIT

REM Delete temp file
del "bin\%APP_NAME%.ap_"

:EXIT
cd "%PREV_PATH%"
ENDLOCAL
exit /b %ERRORLEVEL%

i got this codes from a site. (http://www.apriorit.com/our-company/dev-blog/233-how-to-build-apk-file-from-command-line)

我从一个网站上得到了这个代码。(http://www.apriorit.com/our-company/dev-blog/233-how-to-build-apk-file-from-command-line)

i downloaded the source code sample and opened the batch file in there but it didn't generate it's apk file. usually the apk file is located at its bin\ right? but when i opened the folder, the file is not in there. please help me how to use this one. i'd appreciate you help.

我下载了源代码示例并在那里打开了批处理文件,但它没有生成它的 apk 文件。通常 apk 文件位于其 bin\ 中,对吗?但是当我打开文件夹时,文件不在那里。请帮助我如何使用这个。我很感激你的帮助。

采纳答案by axis

The apk by default is located under bin and this is correct, but when you distribute source code it's better to not add any apk because a "fresh" compiled apk is always a better solution.

默认情况下,apk 位于 bin 下,这是正确的,但是当您分发源代码时,最好不要添加任何 apk,因为“新鲜”编译的 apk 始终是更好的解决方案。

if you have a file called build.xmlin the root of your project just do

如果您build.xml在项目的根目录中调用了一个文件,请执行

ant debug

otherwise you need to update your project with the minimum informations required for the building phase with

否则,您需要使用构建阶段所需的最少信息更新您的项目

android update project -t android-10 -p .

in this case android-10is a target for your apk/app/api and you can customize this option for your targeted device.

在这种情况下android-10是您的 apk/app/api 的目标,您可以为目标设备自定义此选项。

After this you get your build.xmland everything is in place for generating an apk.

在此之后,您将获得build.xml生成 apk 的所有内容。

回答by Shrikant

You'll have to have Apache ant for this one:

你必须有 Apache ant 来完成这个:

ant debug

This will build and sign the necessary .apkfiles.

这将构建并签署必要的.apk文件。

For more info, please see this: http://codeseekah.com/2012/02/09/command-line-android-development-basics/

有关更多信息,请参阅:http: //codeseekah.com/2012/02/09/command-line-android-development-basics/

EDIT:

编辑:

ant is not a part of standard Android SDK setup. You'll have to install it.

ant 不是标准 Android SDK 设置的一部分。你必须安装它。

Download the latest ant zip file from The Apache Ant Project.

从 The Apache Ant Project下载最新的 ant zip 文件。

Extract the zip file to a folder, say c:\ant\ 

Add c:\ant to your path environment variable

Once these are done, you'll be able to run ant from the command line

完成这些后,您将能够从命令行运行 ant

回答by Valen

ant debug install

is actually viable if you create project with (note that you should specify the virtual device with "adb -s" command while installing if you're running multiple devices)

如果您创建项目实际上是可行的(请注意,如果您正在运行多个设备,则在安装时应使用“adb -s”命令指定虚拟设备)

android create project -n <project_name>
        -t <target_version> -p <path> -k <package> -a <activity>

and use this command to run on the avd (if you've started one)

并使用此命令在 avd 上运行(如果您已启动)

adb -e shell "am start -a android.intent.action.MAIN -n com.your.package/.YourActivity"

(change -e option to -d if you're running on devices)

(如果您在设备上运行,请将 -e 选项更改为 -d)

reference

参考

better use "AVD Manager.exe" to start a virtual device if you don't know those command or parameters, as there're quite a lot.

如果您不知道这些命令或参数,最好使用“AVD Manager.exe”来启动虚拟设备,因为有很多。

回答by Rupesh Kumar

One of the best example I found on Internet for creating Android APK is https://geosoft.no/development/android.html.Also you can use assembleDebug command in root directory (make sure setting gradle as environment variable), if you are using Gradle.

我在 Internet 上找到的用于创建 Android APK 的最佳示例之一是https://geosoft.no/development/android.html。此外,您还可以在根目录中使用 assembleDebug 命令(确保将 gradle 设置为环境变量),如果您是使用摇篮。