`

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

 
阅读更多

英文文档来源于官方文档,鉴于英文学习目的和很多同学访问不了该站点,在这里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\<jdkdir>

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 <your_module_name>-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 <your_module_name>-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 storeFilestorePasswordkeyAlias 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 <your_module_name>-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 <path_to_your_bin>.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:

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.

1
0
分享到:
评论

相关推荐

    android apk反编译工具

    2. **反编译APK**:将`apktool`添加到系统路径后,你可以通过命令行输入`apktool d &lt;apk_file&gt;`来反编译APK。这里的`&lt;apk_file&gt;`是你要反编译的APK的路径。执行此命令后,`apktool`会生成一个与APK同名的目录,里面...

    Android apk素材和源码反编译工具及操作文档

    总的来说,这些工具和文档提供了一个完整的Android APK反编译解决方案,对于开发者而言,它们是理解和逆向工程APK的重要资源。在实际工作中,我们可以利用这些工具来调试、学习或优化现有的应用程序。

    apk安装包制作方法

    本文详细讲解了在 Android Eclipse 开发环境下制作 APK 安装包的方法,包括 Android 项目的编译、Keystore 的设置、生成 APK 文件等步骤。通过本文,读者可以学习到制作 APK 安装包的详细过程,并应用于实际的 ...

    Android反编译apk到java源码的方法.pdf

    标题和描述中提到的知识点是Android应用的反编译技术,主要是如何将APK文件反编译为Java源代码。以下是对这些知识点的详细说明: Android应用通常由多个部分组成,包括资源文件(如图片、布局XML)、Java字节码...

    apk反编译,apk2java+dex2jar-2.0+jdgui

    然而,由于Android应用的APK文件本质上是可执行的Java代码经过编译和打包后的产物,它并非完全不可逆。本主题聚焦于“apk反编译”,这是一个用于理解或修改APK内部代码的过程。这里介绍的工具集合包括`apk2java`, `...

    反编译安卓(android)软件(apk)的工具及方法说明

    2. **反编译APK**:打开命令行,定位到Apktool所在的目录,然后输入以下命令: ``` apktool d &lt;apk_file_path&gt; -o ``` 这里的`&lt;apk_file_path&gt;`是你想要反编译的APK文件路径,`&lt;output_directory&gt;`是你希望反...

    图文apk反编译白痴文档(全)

    3. **使用apktool进行反编译**:在命令行中切换至包含apktool.bat的目录,执行命令`apktool d C:\*.apk C:\*`。这里,`d`表示反编译,`C:\*.apk`是待反编译的APK文件路径,`C:\*`是输出文件夹路径。 4. **查看反编译...

    安卓 APK 反编译工具文档

    本文档将详细讲解如何使用 Android APK 反编译工具,包括获取 Java 源代码和提取资源文件的步骤。 首先,我们关注如何通过 dex2jar 和 JD-GUI 获取 APK 的 Java 源代码。dex2jar 是一个工具,它能够将 APK 中的 ...

    Android 反编译全套工具(dex2jar,jdgui)以及详细文档

    在提供的"Android_反编译apk_到java源码的方法.doc"文档中,会详细介绍如何操作这两个工具,包括下载、安装、使用命令行工具等步骤,对于初学者来说非常有帮助。 需要注意的是,反编译行为可能涉及法律问题,应在...

    android 反编译工具及例子

    在Android开发领域,有时我们需要对APK文件进行反编译以了解其内部结构、查看源代码或进行二次开发。本文将详细介绍两种常见的Android反编译工具及其应用,并提供一个具体的例子来帮助理解。 首先,我们要了解的是...

    android反编译

    apk2java能够解析.dex文件,生成对应的Java源代码,这对于想要批量处理APK或者在自动化测试环境中使用反编译结果的开发者来说非常有用。 然后,我们讨论一下“dex2jar”。dex2jar是一个用于将.dex文件转换为.jar...

    SDL Android下编译源码

    9. **测试应用**:将生成的APK安装到Android设备或模拟器上。如果一切顺利,你应该能看到SDL的欢迎画面,表示编译和运行成功。 在这个过程中,你可能需要解决各种编译错误或链接问题,比如版本不兼容、库找不到等。...

    Android APK反编译

    下面我们将详细讨论如何使用`dex2jar`和`jd-gui`这两个工具来完成Android APK的反编译。 首先,`dex2jar`是一个开源工具,它的主要功能是将Dalvik字节码转换为Java字节码,也就是将.dex文件转换为.jar文件。这个...

    Android 反编译工具

    本文将深入探讨Android反编译的相关知识,包括为什么要进行反编译、常用工具有哪些以及如何使用这些工具来解析APK中的图片和XML文档。 首先,我们来理解一下什么是Android反编译。Android反编译是指将已编译好的APK...

    androidapk问题大全解析参照.pdf

    3. 构建项目,IDE会编译源码并生成APK。 4. 通过签名和优化过程,APK准备就绪,可以发布或测试。 ### 在模拟器中安装APK 1. 配置Android SDK环境变量,确保`adb`可执行文件在PATH环境变量中。 2. 将APK文件放置在...

    android工程反编译工具集-附有用法说明文档

    在Android开发领域,有时我们需要对APK文件进行反编译...配合提供的用法说明文档,你将能够高效地进行APK的反编译工作,深入理解Android应用的内部构造。在进行此类操作时,请确保遵守版权法规,仅用于学习和研究目的。

    android 反编译工具包,很好使

    在Android开发领域,有时我们需要对APK文件进行反编译以查看其源代码、资源文件或进行二次修改。"android 反编译工具包"是一个专门为开发者提供的实用工具集,它可以帮助我们深入理解APK的工作原理,进行逆向工程...

    Android版编译发布1

    本文档主要介绍如何在Windows平台上搭建Android编译环境以及发布应用程序的基本流程。整个过程涉及到了关键的开发工具配置,包括但不限于JDK、SDK、NDK、Ant等环境的设置与验证,同时也包括了Qt编译器的具体配置细节...

    APK编译与反编译

    1. **环境配置**:按照官方文档或使用提供的自动配置工具来设置Java环境。 2. **安装Apktool**:下载Apktool并将其解压到指定位置。 3. **准备文件**:将待处理的APK文件(例如systemui.apk和framework-res.apk)...

    两种方法反编译Android的apk文件.doc

    ### 两种方法反编译Android的apk文件 在学习Android编程的过程中,经常需要对已发布的应用程序进行逆向分析或学习其编程技巧。然而,大多数Android应用都是以`.apk`文件形式发布,这意味着开发者无法直接访问源代码...

Global site tag (gtag.js) - Google Analytics