在 Phonegap 3.x CLI 上构建 android release apk

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20402818/
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-08-20 02:47:57  来源:igfitidea点击:

Build android release apk on Phonegap 3.x CLI

androidcordovacordova-cli

提问by Guillermo Gutiérrez

How can I build an android app locally using the Phonegap 3.x CLI, ready to release? I check the bin folder generated inside the platforms/android directory of the project, and only has .debug APKs.

如何使用准备发布的 Phonegap 3.x CLI 在本地构建 android 应用程序?我检查了项目的platforms/android 目录中生成的bin 文件夹,并且只有.debug APK。

By the way, I use this command: phonegap local build android

顺便说一句,我使用这个命令: phonegap local build android

回答by Guillermo Gutiérrez

This is for Phonegap 3.0.x to 3.3.x. For PhoneGap 3.4.0 and higher see below.

这适用于 Phonegap 3.0.x 到 3.3.x。对于 PhoneGap 3.4.0 及更高版本,请参见下文

Found part of the answer here, at Phonegap documentation. The full process is the following:

在 Phonegap 文档中在这里找到了部分答案。完整流程如下:

  1. Open a command line window, and go to /path/to/your/project/platforms/android/cordova.

  2. Run build --release. This creates an unsigned release APK at /path/to/your/project/platforms/android/binfolder, called YourAppName-release-unsigned.apk.

  3. Sign and align the APK using the instructions at android developer official docs.

  1. 打开命令行窗口,然后转到 /path/to/your/project/ platforms/android/cordova

  2. 运行build --release。这会在 /path/to/your/project/ platforms/android/bin文件夹中创建一个未签名的发布 APK ,名为 YourAppName-release-unsigned.apk。

  3. 使用android developer official docs 中的说明对 APK 进行签名和对齐。

Thanks to @LaurieClark for the link (http://iphonedevlog.wordpress.com/2013/08/16/using-phonegap-3-0-cli-on-mac-osx-10-to-build-ios-and-android-projects/), and the blogger who post it, because it put me on the track.

感谢@LaurieClark 提供链接(http://iphonedevlog.wordpress.com/2013/08/16/using-phonegap-3-0-cli-on-mac-osx-10-to-build-ios-and- android-projects/),以及发布它的博主,因为它让我走上了正轨。

回答by Steven Anderson

In PhoneGap 3.4.0 you can call:

在 PhoneGap 3.4.0 中,您可以调用:

cordova build android --release

If you have set up the 'ant.properties' file in 'platforms/android' directory like the following:

如果您在“platforms/android”目录中设置了“ant.properties”文件,如下所示:

key.store=/Path/to/KeyStore/myapp-release-key.keystore
key.alias=myapp

Then you will be prompted for your keystore password and the output file (myapp-release.apk) ends up in the 'platforms/android/ant-build' directory already signed and aligned and ready to deploy.

然后系统将提示您输入密钥库密码,输出文件 (myapp-release.apk) 最终位于“platforms/android/ant-build”目录中,该目录已经签名并对齐并准备部署。

回答by KrIsHnA

In cordova 6.2.0

在科尔多瓦 6.2.0

cd cordova/ #change to root cordova folder
platforms/android/cordova/clean #clean if you want
cordova build android --release -- --keystore="/path/to/keystore" --storePassword=password --alias=alias_name #password will be prompted if you have any


Previous answer:

上一个答案:

According to cordova 5.0.0

根据科尔多瓦 5.0.0

{
     "android": {
         "release": {
             "keystore": "app-release-key.keystore",
             "alias": "alias_name"
         }
     }
 }

and run ./build --release --buildConfig build.jsonfrom directory platforms/android/cordova/

./build --release --buildConfig build.json从目录运行platforms/android/cordova/

keystore file location is relative to platforms/android/cordova/, so in above configuration .keystorefile and build.jsonare in same directory.

密钥库文件位置是相对于 的platforms/android/cordova/,所以在上面的配置.keystore文件中 并且build.json在同一目录中。

keytool -genkey -v -keystore app-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000

keytool -genkey -v -keystore app-release-key.keystore -alias alias_name -keyalg RSA -keysize 2048 -validity 10000

回答by Simone Avogadro

Following up to @steven-anderson you can also configure passwords inside the ant.properties, so the process can be fully automated

关注@steven-anderson,您还可以在 ant.properties 中配置密码,因此该过程可以完全自动化

so if you put in platform\android\ant.properties the following

所以如果你把 platform\android\ant.properties 放在下面

key.store=../../yourCertificate.jks
key.store.password=notSoSecretPassword
key.alias=userAlias
key.alias.password=notSoSecretPassword

回答by Freaktor

just wondered around a lot because I got the same issue but in my installation the command "cordova" was never available and "phone gap build android --release" just ignored the platform/android/ant.properties.

只是想了很多,因为我遇到了同样的问题,但在我的安装中,命令“cordova”永远不可用,“phone gap build android --release”只是忽略了平台/android/ant.properties。

so looking inside my platform filter I found a folder named "cordova" and inside of it there was an "build" binary that accepted the --release argument, it asked me for the key chains and I ended with a signed and ready for production APK.

所以查看我的平台过滤器,我发现了一个名为“cordova”的文件夹,里面有一个“build”二进制文件,它接受了 --release 参数,它要求我提供钥匙链,我以一个签名并准备好生产结束APK。

this was never documented in any part of the phone gap site and frankly speaking now I kinda hate phonegap :( it was supposed to make the things easier but everything was just complicated :(

电话间隙网站的任何部分从未记录过这一点,坦率地说,现在我有点讨厌 phonegap :( 它应该让事情变得更容易,但一切都变得复杂了 :(

回答by incorelabs

Building PhoneGap Android app for deployment to the Google Play Store

构建 PhoneGap Android 应用程序以部署到 Google Play 商店

These steps would work for Cordova, PhoneGap or Ionic. The only difference would be, wherever a call to cordovais placed, replace it with phonegapor ionic, for your particular scenario.

这些步骤适用于 Cordova、PhoneGap 或 Ionic。唯一的区别是,无论在何处调用,cordova都将其替换为phonegapionic,适用于您的特定场景。

Once you are done with the development and are ready to deploy, follow these steps:

完成开发并准备好部署后,请执行以下步骤:

  1. Open a command line window (Terminal on macOS and Linux OR Command Prompt on Windows).

  2. Head over to the /path/to/your/project/, which we would refer to as the Project Root.

  3. While at the project root, remove the "Console" plugin from your set of plugins.

    The command is: cordova plugin rm cordova-plugin-console

  4. While still at the project root, use the cordova build command to create an APK for release distribution.

    The command is: cordova build --release android

  5. The above process creates a file called android-release-unsigned.apkin the folder ProjectRoot/platforms/android/build/outputs/apk/

  6. Sign and align the APK using the instructions at https://developer.android.com/studio/publish/app-signing.html#signing-manually

    At the end of this step the APK which you get can be uploaded to the Play Store.

  1. 打开命令行窗口(macOS 和 Linux 上的终端或 Windows 上的命令提示符)。

  2. 转到/path/to/your/project/,我们将其称为项目根目录。

  3. 在项目根目录下,从您的插件集中删除“控制台”插件。

    命令是:cordova plugin rm cordova-plugin-console

  4. 仍在项目根目录下时,使用 cordova build 命令创建用于发布分发的 APK。

    命令是:cordova build --release android

  5. 上面的过程android-release-unsigned.apk在文件夹中创建了一个名为的文件ProjectRoot/platforms/android/build/outputs/apk/

  6. 使用https://developer.android.com/studio/publish/app-signing.html#signing-manually 上的说明签署并对齐 APK

    在此步骤结束时,您可以将获得的 APK 上传到 Play 商店.

Note:As a newbie or a beginner, the last step may be a bit confusing as it was to me. One may run into a few issues and may have some questions as to what these commands are and where to find them.

注意:作为新手或初学者,最后一步可能对我来说有点混乱。人们可能会遇到一些问题,并且可能会对这些命令是什么以及在哪里找到它们有一些疑问。

Q1. What are jarsignerand keytool?
Ans:The Android App Signing instructions do tell you specifically what jarsignerand keytoolare all about BUT it doesn't tell you where to find them if you run into a 'command not found error' on the command line window.

Thus, if you've got the Java Development Kit(JDK) added to your PATHvariable, simply running the commands as in the Guide would work. BUT, if you don't have it in your PATH, you can always access them from the binfolder of your JDK installation.

一季度。什么是jarsignerkeytool
Ans:Android 应用程序签名说明确实会具体告诉您jarsignerkeytool的全部内容,但是如果您在命令行窗口中遇到“找不到命令的错误”,它不会告诉您在哪里可以找到它们。

因此,如果您已将 Java 开发工具包 (JDK) 添加到您的PATH变量中,只需按照指南中的方式运行命令即可。但是,如果您的 PATH 中没有它,您始终可以从JDK 安装的bin文件夹中访问它们。

Q2. Where is zipalign?
Ans:There is a high probability to not find the zipaligncommand and receive the 'command not found error'. You'd probably be googling zipalignand where to find it?

The zipalignutility is present within the Android SDK installation folder. On macOS, the default location is at, user-name/Library/Android/sdk/. If you head over to the folder you would find a bunch of other folders like docs, platform-tools, build-tools, tools, add-ons...

Open the build-toolsfolder. cd build-tools. In here, there would be a number of folders which are versioned according to the build tool-chain you are using in the Android SDK Manager. ZipAlignis available in each of these folders. I personally go for the folder with the latest version on it. Open Any.

On macOS or Linux you may have to use ./zipalignrather than simply typing in zipalignas the documentation mentions. On Windows, zipalignis good enough.

Q2。zipalign在哪里?
Ans:很有可能找不到zipalign命令并收到'command not found 错误'。您可能会在谷上搜索zipalign以及在哪里可以找到它?

的zipalign工具是Android SDK安装文件夹内。在 macOS 上,默认位置是,user-name/Library/Android/sdk/。如果您转到该文件夹​​,您会发现一堆其他文件夹,例如docs, platform-tools, build-tools, tools, add-ons...

打开build-tools文件夹。cd build-tools. 在这里,将有许多文件夹根据您在 Android SDK 管理器中使用的构建工具链进行版本控制。ZipAlign在这些文件夹中的每一个中都可用。我个人去寻找带有最新版本的文件夹。打开任何。

在 macOS 或 Linux 上,您可能必须使用./zipalign而不是zipalign像文档中提到的那样简单地输入。在 Windows 上,zipalign已经足够好了。

回答by robert deveau

i got this to work by copy pasting the signed app in the same dir as zipalign. It seems that aapt.exe could not find the source file even when given the path. i.e. this did not work zipalign -f -v 4 C:...\CordovaApp-release-unsigned.apk C:...\destination.apk it reached aapt.exeCordovaApp-release-unsigned.apk , froze and upon hitting return 'aapt.exeCordovaApp-release-unsigned.apk' is not recognized as an internal or external command, operable program or batch file. And this did zipalign -f -v 4 CordovaApp-release-unsigned.apk myappname.apk

我通过将签名的应用程序复制粘贴到与 zipalign 相同的目录中来使其工作。即使给定了路径,似乎 aapt.exe 也找不到源文件。即这不起作用 zipalign -f -v 4 C:...\CordovaApp-release-unsigned.apk C:...\destination.apk 它到达 aapt.exeCordovaApp-release-unsigned.apk ,冻结并点击返回'aapt.exeCordovaApp-release-unsigned.apk' 不是内部或外部命令,也不是可运行的程序或批处理文件。这确实 zipalign -f -v 4 CordovaApp-release-unsigned.apk myappname.apk

回答by Julian

I know this question asks about Phonegap 3.X specifically, but just for reference any Phonegap version above 4.0.0 uses Gradle instead of Antto build by default. To use Ant instead of Gradle you can add this to your config.xml:

我知道这个问题专门询问 Phonegap 3.X,但仅供参考,任何高于 4.0.0 的 Phonegap 版本默认使用 Gradle 而不是 Ant来构建。要使用 Ant 而不是 Gradle,您可以将其添加到 config.xml 中:

<preference name="android-build-tool" value="ant" />

When using Gradle the keystore signing information now needs to go into a new location (as outlined in this post). Create new file called 'release-signing.properties' in the same folder as "build.gradle" file and put inside the following content:

使用 Gradle 时,密钥库签名信息现在需要进入一个新位置(如本文所述)。创建一个名为“新文件release-signing.properties在同一文件夹“的build.gradle”文件,并把下面的内容里”:

storeFile=..\..\some-keystore.keystore
storeType=jks
keyAlias=some-key
// if you don't want to enter the password at every build, you can store it with this
keyPassword=your-key-password
storePassword=your-store-password

回答by Laurie Clark

You could try this command, it should build and run the app (so .apk should be created) :

你可以试试这个命令,它应该构建并运行应用程序(所以 .apk 应该被创建):

phonegap local run android

phonegap local run android