본문 바로가기
User Guide

커맨드라인에서 Gradle 실행시키기 Run Gradle from the Command Line (32)

by 각종 잡상식 지식 모음 2016. 6. 10.
반응형

커맨드라인에서 Gradle 실행시키기
Run Gradle from the Command Line

Gradle 빌드 셋팅을 사용하여 애플리케이션을 빌드하는 방법으로는 기본적으로 2가지가 있습니다:
애플리케이션을 디버깅하는 방법 - 디버그 - 배포를 위한 최종 패키지 빌딩 방법 - 배포 모드.
어느 빌드 방법을 사용하든 에뮬레이터 또는 장치에 설치 하기 전에, 디버그 모드로 빌딩할 때는 디버그 키로, 배포 모드로 빌딩할 때는 사설 키로 서명해야 합니다.
By default, there are two build types to build your application using the Gradle build settings: one for debugging your application — debug — and one for building your final package for release — release mode. Regardless of which build type your modules use, the app must be signed 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.

디버그 또는 릴리스 빌드 유형으로 빌드하는 경우 모듈을 실행하고 빌드해야 합니다. 

이렇게 하면 에뮬레이터나 장치에 설치할 수 있는 .apk 파일이 생성됩니다. 

디버그 빌드 유형을 사용하여 빌드할 때 .apk 파일은 모듈의 build.gradle 파일에서 디버깅 가능한 true 설정을 기반으로 하는 디버그 키를 사용하여 SDK 도구에 의해 자동으로 서명되므로 에뮬레이터 또는 연결된 개발장치에 즉시 설치할 수 있습니다. 

디버그 키로 서명된 애플리케이션은 배포할 수 없습니다. 

릴리스 빌드 유형을 사용하여 빌드하는 경우 .apk 파일은 서명되지 않으므로, 모듈의 build.gradle 파일에서 Keytool 및 Jarsigner 설정을 사용하여 자체 개인 키로 수동으로 서명해야 합니다.
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 debug build type, the .apk file is automatically signed by the SDK tools with a debug key based on the debuggable true setting in the module's build.gradle file, so it's instantly ready for installation onto an emulator or attached development device. You cannot distribute an application that is signed with a debug key. When you build using the release build type, the .apk file is unsigned, so you must manually sign it with your own private key, using Keytool and Jarsigner settings in the module's build.gradle file.


특히 애플리케이션을 릴리스하고 최종 사용자와 공유할 준비가 된 경우 애플리케이션 서명을 읽고 이해하는 것이 중요합니다. 

이 문서는 개인 키를 생성한 다음 이를 사용하여 APK 파일에 서명하는 절차를 설명합니다. 

그러나 이제 막 시작했다면 디버그 모드에서 빌드하여 에뮬레이터 또는 자체 개발 장치에서 애플리케이션을 빠르게 실행할 수 있습니다.

It's important that you read and understand Signing Your Applications, particularly once you're ready 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.

Gradle이 없는 경우 Gradle 홈페이지에서 다운로드할 수 있습니다. 

그것을 설치하고 실행 가능한 PATH에 있는지 확인하십시오. 

Gradle을 호출하기 전에 JAVA_HOME 환경 변수를 선언하여 JDK가 설치된 경로를 지정해야 합니다.

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 Gradle, you need to declare the JAVA_HOME environment variable to specify the path to where the JDK is installed.

Note: Windows에서 ant를 사용하고 JDK를 설치할 때 기본값은 "Program Files" 디렉토리에 설치하는 것입니다. 이 위치는 공간 때문에 개미가 실패하게 합니다. 문제를 해결하려면 다음과 같이 JAVA_HOME 변수를 지정할 수 있습니다. When using ant and 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 build.gradle file (found in the root of the module directory) and add entries for storeFile,storePasswordkeyAlias 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 build.gradle file to build your project in release mode:

  3. ...
    android
    {
       
    ...
        defaultConfig
    { ... }
        signingConfigs
    {
            release
    {
                storeFile file
    ("myreleasekey.keystore")
                storePassword
    "password"
                keyAlias
    "MyReleaseKey"
                keyPassword
    "password"
           
    }
       
    }
        buildTypes
    {
            release
    {
               
    ...
                signingConfig signingConfigs
    .release
           
    }
       
    }
    }
    ...
  4. 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 build.gradle file and aligned with zipalign. 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 deviceIn the Virtual Devices view, select an AVD and click Start.
  2. Install your applicationFrom your SDK's tools/ directory, install the .apk on the emulator:Your .apk file (signed with either a release or debug key) is in your module build/ directory after you build your application.
    adb -s emulator-5554 install path/to/your/app.apk
    To see a list of available device serial numbers, execute adb devices.
  3. 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 -soption. For example:
  4. adb install <path_to_your_bin>.apk

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 modeant 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. You can find the option under Settings > Developer options.Note: On Android 4.2 and newer, Developer options is hidden by default. To make it available, go to Settings > About phone and tap Build number seven times. Return to the previous screen to find Developer 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.

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.

 

 

반응형

댓글