热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

android命令行编译生成apk(翻译官方文档)

英文文档来源于官方文档,鉴于英文学习目的和很多同学访问不了该站点,在这里http:developer.android.comtoolsbuildingbuilding-cmdlin

英文文档来源于官方文档,鉴于英文学习目的和很多同学访问不了该站点,在这里http://developer.android.com/tools/building/building-cmdline.html

?

By default, there are two build types to build your application using the gradle.build settings: one for

通常,使用gradle.build settings编译你的应用程序有以下两种编译类型:

debugging your application —?debug?— and one for building your final package for release

一个debug模式:调试你的应用,另外一个是发布模式:编译成最终的发布应用。

—?release mode. Regardless of which way you build type your modules use, the app must be signed

不论你使用哪种方式编译你的模块,app运行在设备或者模拟器之前都需要被签名。

before it can install on an emulator or device—with a debug key when building in debug mode and with your own private key when building in release mode.

在debug模式使用一个debug key,在发布模式使用一个你的私有key进行编译。

Whether you‘re building with the debug or release build type, you need to run and build your module.

不论你将要使用调试或者发布模式,你都需要运行并且编译你的模块。

This will create the .apk file that you can install on an emulator or device. When you build using the

这时将会生成一个以.apk结尾的文件,然后你可以在模拟器或者真机上安装它。

debug build type, the .apk file is automatically signed by the SDK tools with a debug key based on

在使用调试模式时,如果gradle.build文件中debuggable=true时,apk文件是自动使用sdk 工具包里面自带的debug key签名。

the?debuggable true?setting in the module‘s gradle.build file, so it‘s instantly ready for installation onto

所以apk文件可以成功安装在模拟器或者开发设备商。

an emulator or attached development device. You cannot distribute an application that is signed?

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 你不要分发你的使用debug key签名的应用程序.

with a debug key. When you build using the release build type, the .apk file is?unsigned, so you must

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?当你用发布模拟编译时,apk文件是没有被签名的,你必须使用你的私有key用keytool和jarsigner签名。这些都在gradle.build文件。

manually sign it with your own private key, using Keytool and Jarsigner settings in the module‘s gradle.build file.

It‘s important that you read and understand?Signing Your Applications, particularly once you‘re

其他不明白的可以看signing Your Applications.

readygengx

to release your application and share it with end-users. That document describes the procedure for generating a private key and then using it to sign your .apk file. If you‘re just getting started, however, you can quickly run your applications on an emulator or your own development device by building in debug mode.

If you don‘t have?Gradle, you can obtain it from the?Gradle home page. Install it and make sure it is in your executable PATH. Before calling Ant, you need to declare the JAVA_HOME environment variable to specify the path to where the JDK is installed.

Note:?When installing JDK on Windows, the default is to install in the "Program Files" directory. This location will cause?ant?to fail, because of the space. To fix the problem, you can specify the JAVA_HOME variable like this:

set JAVA_HOME=c:\Progra~1\Java\

The easiest solution, however, is to install JDK in a non-space directory, for example:

c:\java\jdk1.7

Building in Debug Mode



For immediate application testing and debugging, you can build your application in debug mode and immediately install it on an emulator. In debug mode, the build tools automatically sign your application with a debug key and optimize the package with?zipalign.

To build in debug mode, open a command-line and navigate to the root of your project directory. Use Gradle to build your project in debug mode, invoke the?assembleDebug?build task using the Gradle wrapper script (gradlew assembleRelease).

This creates your debug?.apk?file inside the module?build/?directory, named?-debug.apk. The file is already signed with the debug key and has been aligned with?zipalign.

On Windows platforms, type this command:

> gradlew.bat assembleDebug

On Mac OS and Linux platforms, type these commands:

$ chmod +x gradlew
$ ./gradlew assembleDebug

The first command (chmod) adds the execution permission to the Gradle wrapper script and is only necessary the first time you build this project from the command line.

After you build the project, the output APK for the app module is located in?app/build/outputs/apk/, and the output AAR for any lib modules is located in?lib/build/outputs/libs/.

To see a list of all available build tasks for your project, type this command:

$ ./gradlew tasks

Each time you change a source file or resource, you must run Gradle again in order to package up the latest version of the application.

To install and run your application on an emulator, see the section about?Running on the Emulator.

Building in Release Mode



When you‘re ready to release and distribute your application to end-users, you must build your application in release mode. Once you have built in release mode, it‘s a good idea to perform additional testing and debugging with the final .apk.

Before you start building your application in release mode, be aware that you must sign the resulting application package with your private key, and should then align it using the?zipalign?tool. There are two approaches to building in release mode: build an unsigned package in release mode and then manually sign and align the package, or allow the build script to sign and align the package for you.

Build unsigned

If you build your application?unsigned, then you will need to manually sign and align the package.

To build an?unsigned?.apk in release mode, open a command-line and navigate to the root of your module directory. Invoke the?assembleRelease?build task.

On Windows platforms, type this command:

> gradlew.bat assembleRelease

On Mac OS and Linux platforms, type this command:

$ ./gradlew assembleRelease

This creates your Android application .apk file inside the project?bin/?directory, named?-unsigned.apk.

Note:?The .apk file is?unsigned?at this point and can‘t be installed until signed with your private key.

Once you have created the unsigned .apk, your next step is to sign the .apk with your private key and then align it with?zipalign. To complete this procedure, read?Signing Your Applications.

When your?.apk?has been signed and aligned, it‘s ready to be distributed to end-users. You should test the final build on different devices or AVDs to ensure that it runs properly on different platforms.

Build signed and aligned

If you would like, you can configure the Android build script to automatically sign and align your application package. To do so, you must provide the path to your keystore and the name of your key alias in your modules‘s build.gradle file. With this information provided, the build will prompt you for your keystore and alias password when you build using the release build type and produce your final application package, which will be ready for distribution.

To specify your keystore and alias, open the module gradle.build file (found in the root of the module directory) and add entries for?storeFile,?storePassword,?keyAlias?and?keyPassword. For example:

storeFile file("myreleasekey.keystore")
keyAlias "MyReleaseKey"

Save your changes. Now you can build a?signed?.apk in release mode:


  1. Open a command-line and navigate to the root of your module directory.

  2. Edit the gradle.build 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
    ? ? ? ? }
    ? ? }
    }
    ...

    ?


  3. When prompted, enter you keystore and alias passwords.

    Caution:?As described above, your password will be visible on the screen.


This creates your Android application .apk file inside the module?build/?directory, named?-release.apk. This .apk file has been signed with the private key specified in gradle.build file and aligned withzipalign. It‘s ready for installation and distribution.

Once built and signed in release mode

Once you have signed your application with a private key, you can install and run it on an?emulator?or?device. You can also try installing it onto a device from a web server. Simply upload the signed .apk to a web site, then load the .apk URL in your Android web browser to download the application and begin installation. (On your device, be sure you have enabled?Settings > Applications > Unknown sources.)

Running on the Emulator



Before you can run your application on the Android Emulator, you must?create an AVD.

To run your application:



  1. Open the AVD Manager and launch a virtual device

    From your SDK‘s?platform-tools/?directory, execute the?android?tool with the?avd?options:

    android avd

    In the?Virtual Devices?view, select an AVD and click?Start.



  2. Install your application

    From your SDK‘s?tools/?directory, install the?.apk?on the emulator:

    adb install .apk

    Your .apk file (signed with either a release or debug key) is in your module?build/?directory after you build your application.

    If there is more than one emulator running, you must specify the emulator upon which to install the application, by its serial number, with the?-s?option. For example:

    adb -s emulator-5554 install path/to/your/app.apk

    To see a list of available device serial numbers, execute?adb devices.


If you don‘t see your application on the emulator, try closing the emulator and launching the virtual device again from the AVD Manager. Sometimes when you install an application for the first time, it won‘t show up in the application launcher or be accessible by other applications. This is because the package manager usually examines manifests completely only on emulator startup.

Be certain to create multiple AVDs upon which to test your application. You should have one AVD for each platform and screen type with which your application is compatible. For instance, if your application compiles against the Android 4.0 (API Level 14) platform, you should create an AVD for each platform equal to and greater than 4.0 and an AVD for each?screen type?you support, then test your application on each one.

Tip:?If you have?only one?emulator running, you can build your application and install it on the emulator in one simple step. Navigate to the root of your project directory and use Ant to compile the project with?install mode:ant install. This will build your application, sign it with the debug key, and install it on the currently running emulator.

Running on a Device



Before you can run your application on a device, you must perform some basic setup for your device:


  • Enable?USB debugging?on your device.

    • On most devices running Android 3.2 or older, you can find the option under?Settings > Applications > Development.

    • On Android 4.0 and newer, it‘s in?Settings > Developer options.

      Note:?On Android 4.2 and newer,?Developer options?is hidden by default. To make it available, go toSettings > About phone?and tap?Build number?seven times. Return to the previous screen to findDeveloper options.




  • Ensure that your development computer can detect your device when connected via USB

Read?Setting up a Device for Development?for more information.

Once your device is set up and connected via USB, navigate to your SDK‘s?platform-tools/?directory and install the?.apk?on the device:

adb -d install path/to/your/app.apk

The?-d?flag specifies that you want to use the attached device (in case you also have an emulator running).

For more information on the tools used above, please see the following documents:


  • android Tool

  • Android Emulator


  • Android Debug Bridge?(ADB)


Application Signing



As you begin developing Android applications, understand that all Android applications must be digitally signed before the system will install them on an emulator or device. There are two ways to do this: with a?debug key?(for immediate testing on an emulator or development device) or with a?private key?(for application distribution).

The Android build tools help you get started by automatically signing your .apk files with a debug key at build time. This means that you can build your application and install it on the emulator without having to generate your own private key. However, please note that if you intend to publish your application, you?must?sign the application with your own private key, rather than the debug key generated by the SDK tools.

Android Studio helps you get started quickly by signing your .apk files with a debug key, prior to installing them on an emulator or development device. This means that you can quickly run your application from Android Studio without having to generate your own private key. No specific action on your part is needed, provided ADT has access to Keytool. However, please note that if you intend to publish your application, you?must?sign the application with your own private key, rather than the debug key generated by the SDK tools.

Please read?Signing Your Applications, which provides a thorough guide to application signing on Android and what it means to you as an Android application developer. The document also includes a guide to publishing and signing your application.

Android Plugin for Gradle



The Android build system uses the Android plugin for Gradle to support the Gradle Domain Specific Language (DSL) and declarative language elements. See the?Android Plug-in for Gradle?section for a description of the plugin and a link to the complete list of the supported Gradle DSL elements.


推荐阅读
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • CentOS 6.5安装VMware Tools及共享文件夹显示问题解决方法
    本文介绍了在CentOS 6.5上安装VMware Tools及解决共享文件夹显示问题的方法。包括清空CD/DVD使用的ISO镜像文件、创建挂载目录、改变光驱设备的读写权限等步骤。最后给出了拷贝解压VMware Tools的操作。 ... [详细]
  • 本文介绍了lua语言中闭包的特性及其在模式匹配、日期处理、编译和模块化等方面的应用。lua中的闭包是严格遵循词法定界的第一类值,函数可以作为变量自由传递,也可以作为参数传递给其他函数。这些特性使得lua语言具有极大的灵活性,为程序开发带来了便利。 ... [详细]
  • 本文介绍了在SpringBoot中集成thymeleaf前端模版的配置步骤,包括在application.properties配置文件中添加thymeleaf的配置信息,引入thymeleaf的jar包,以及创建PageController并添加index方法。 ... [详细]
  • 动态规划算法的基本步骤及最长递增子序列问题详解
    本文详细介绍了动态规划算法的基本步骤,包括划分阶段、选择状态、决策和状态转移方程,并以最长递增子序列问题为例进行了详细解析。动态规划算法的有效性依赖于问题本身所具有的最优子结构性质和子问题重叠性质。通过将子问题的解保存在一个表中,在以后尽可能多地利用这些子问题的解,从而提高算法的效率。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • 本文介绍了指针的概念以及在函数调用时使用指针作为参数的情况。指针存放的是变量的地址,通过指针可以修改指针所指的变量的值。然而,如果想要修改指针的指向,就需要使用指针的引用。文章还通过一个简单的示例代码解释了指针的引用的使用方法,并思考了在修改指针的指向后,取指针的输出结果。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • CentOS 7部署KVM虚拟化环境之一架构介绍
    本文介绍了CentOS 7部署KVM虚拟化环境的架构,详细解释了虚拟化技术的概念和原理,包括全虚拟化和半虚拟化。同时介绍了虚拟机的概念和虚拟化软件的作用。 ... [详细]
  • 本文介绍了一种解析GRE报文长度的方法,通过分析GRE报文头中的标志位来计算报文长度。具体实现步骤包括获取GRE报文头指针、提取标志位、计算报文长度等。该方法可以帮助用户准确地获取GRE报文的长度信息。 ... [详细]
  • PDF内容编辑的两种小方法,你知道怎么操作吗?
    本文介绍了两种PDF内容编辑的方法:迅捷PDF编辑器和Adobe Acrobat DC。使用迅捷PDF编辑器,用户可以通过选择需要更改的文字内容并设置字体形式、大小和颜色来编辑PDF文件。而使用Adobe Acrobat DC,则可以通过在软件中点击编辑来编辑PDF文件。PDF文件的编辑可以帮助办公人员进行文件内容的修改和定制。 ... [详细]
  • 深入理解CSS中的margin属性及其应用场景
    本文主要介绍了CSS中的margin属性及其应用场景,包括垂直外边距合并、padding的使用时机、行内替换元素与费替换元素的区别、margin的基线、盒子的物理大小、显示大小、逻辑大小等知识点。通过深入理解这些概念,读者可以更好地掌握margin的用法和原理。同时,文中提供了一些相关的文档和规范供读者参考。 ... [详细]
  • Redis底层数据结构之压缩列表的介绍及实现原理
    本文介绍了Redis底层数据结构之压缩列表的概念、实现原理以及使用场景。压缩列表是Redis为了节约内存而开发的一种顺序数据结构,由特殊编码的连续内存块组成。文章详细解释了压缩列表的构成和各个属性的含义,以及如何通过指针来计算表尾节点的地址。压缩列表适用于列表键和哈希键中只包含少量小整数值和短字符串的情况。通过使用压缩列表,可以有效减少内存占用,提升Redis的性能。 ... [详细]
  • 本文介绍了django中视图函数的使用方法,包括如何接收Web请求并返回Web响应,以及如何处理GET请求和POST请求。同时还介绍了urls.py和views.py文件的配置方式。 ... [详细]
author-avatar
1031372720_eba8d5
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有