android-gradle
Gradle을 사용하여 빌드 구성
수색…
비고
Android 빌드 시스템은 앱 리소스와 소스 코드를 컴파일하고이를 테스트, 배포, 서명 및 배포 할 수있는 APK에 패키지화합니다. Android Studio는 고급 빌드 도구 키트 인 Gradle을 사용하여 빌드 프로세스를 자동화하고 관리하며 유연한 사용자 정의 빌드 구성을 정의 할 수 있도록합니다.
공식 문서
Android Studio 프로젝트에 build.gradle 파일이 두 개있는 이유는 무엇입니까?
<PROJECT_ROOT>\app\build.gradle 은 앱 모듈에만 적용됩니다.
<PROJECT_ROOT>\build.gradle 은 모든 하위 프로젝트 / 모듈에 공통된 구성 옵션을 추가 할 수있는 "최상위 빌드 파일"입니다.
프로젝트에서 다른 모듈을 로컬 라이브러리로 사용하는 경우 다른 build.gradle 파일이 있습니다. <PROJECT_ROOT>\module\build.gradle
최상위 빌드 파일
루트 프로젝트 디렉토리에있는 최상위 레벨 build.gradle 파일은 프로젝트의 모든 모듈에 적용되는 빌드 구성을 정의합니다. 기본적으로 최상위 빌드 파일은 buildscript {} block 을 사용하여 프로젝트의 모든 모듈에 공통적 인 Gradle 저장소 및 종속성을 정의합니다. 다음 코드 샘플은 새 프로젝트를 만든 후 최상위 build.gradle에서 찾을 수있는 기본 설정과 DSL 요소를 설명합니다.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.google.gms:google-services:3.0.0'
}
}
ext {
compileSdkVersion = 23
buildToolsVersion = "23.0.1"
}
모듈 수준 빌드 파일
각 <project>/<module>/ 디렉토리에있는 모듈 수준 build.gradle 파일을 사용하면 해당 모듈이있는 특정 모듈에 대한 빌드 설정을 구성 할 수 있습니다. 이러한 빌드 설정을 구성하면 다음과 같은 사용자 정의 패키징 옵션을 제공 할 수 있습니다. 추가 빌드 유형 및 제품 취향으로 사용하고 main/ app 매니페스트 또는 최상위 build.gradle 파일의 설정을 무시합니다.
apply plugin: 'com.android.application'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}
dependencies {
//.....
}
최상위 파일 예제
/**
* The buildscript {} block is where you configure the repositories and
* dependencies for Gradle itself--meaning, you should not include dependencies
* for your modules here. For example, this block includes the Android plugin for
* Gradle as a dependency because it provides the additional instructions Gradle
* needs to build Android app modules.
*/
buildscript {
/**
* The repositories {} block configures the repositories Gradle uses to
* search or download the dependencies. Gradle pre-configures support for remote
* repositories such as JCenter, Maven Central, and Ivy. You can also use local
* repositories or define your own remote repositories. The code below defines
* JCenter as the repository Gradle should use to look for its dependencies.
*/
repositories {
jcenter()
}
/**
* The dependencies {} block configures the dependencies Gradle needs to use
* to build your project. The following line adds Android Plugin for Gradle
* version 2.0.0 as a classpath dependency.
*/
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
}
}
/**
* The allprojects {} block is where you configure the repositories and
* dependencies used by all modules in your project, such as third-party plugins
* or libraries. Dependencies that are not required by all the modules in the
* project should be configured in module-level build.gradle files. For new
* projects, Android Studio configures JCenter as the default repository, but it
* does not configure any dependencies.
*/
allprojects {
repositories {
jcenter()
}
}
모듈 파일 예제
/**
* The first line in the build configuration applies the Android plugin for
* Gradle to this build and makes the android {} block available to specify
* Android-specific build options.
*/
apply plugin: 'com.android.application'
/**
* The android {} block is where you configure all your Android-specific
* build options.
*/
android {
/**
* compileSdkVersion specifies the Android API level Gradle should use to
* compile your app. This means your app can use the API features included in
* this API level and lower.
*
* buildToolsVersion specifies the version of the SDK build tools, command-line
* utilities, and compiler that Gradle should use to build your app. You need to
* download the build tools using the SDK Manager.
*/
compileSdkVersion 23
buildToolsVersion "23.0.3"
/**
* The defaultConfig {} block encapsulates default settings and entries for all
* build variants, and can override some attributes in main/AndroidManifest.xml
* dynamically from the build system. You can configure product flavors to override
* these values for different versions of your app.
*/
defaultConfig {
/**
* applicationId uniquely identifies the package for publishing.
* However, your source code should still reference the package name
* defined by the package attribute in the main/AndroidManifest.xml file.
*/
applicationId 'com.example.myapp'
// Defines the minimum API level required to run the app.
minSdkVersion 14
// Specifies the API level used to test the app.
targetSdkVersion 23
// Defines the version number of your app.
versionCode 1
// Defines a user-friendly version name for your app.
versionName "1.0"
}
/**
* The buildTypes {} block is where you can configure multiple build types.
* By default, the build system defines two build types: debug and release. The
* debug build type is not explicitly shown in the default build configuration,
* but it includes debugging tools and is signed with the debug key. The release
* build type applies Proguard settings and is not signed by default.
*/
buildTypes {
/**
* By default, Android Studio configures the release build type to enable code
* shrinking, using minifyEnabled, and specifies the Proguard settings file.
*/
release {
minifyEnabled true // Enables code shrinking for the release build type.
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
/**
* The productFlavors {} block is where you can configure multiple product
* flavors. This allows you to create different versions of your app that can
* override defaultConfig {} with their own settings. Product flavors are
* optional, and the build system does not create them by default. This example
* creates a free and paid product flavor. Each product flavor then specifies
* its own application ID, so that they can exist on the Google Play Store, or
* an Android device, simultaneously.
*/
productFlavors {
free {
applicationId 'com.example.myapp.free'
}
paid {
applicationId 'com.example.myapp.paid'
}
}
}
/**
* The dependencies {} block in the module-level build configuration file
* only specifies dependencies required to build the module itself.
*/
dependencies {
compile project(":lib")
compile 'com.android.support:appcompat-v7:24.1.0'
compile fileTree(dir: 'libs', include: ['*.jar'])
}
archivesBaseName을 사용하여 apk 이름 변경
archivesBaseName 을 사용하여 apk의 이름을 설정할 수 있습니다.
예 :
defaultConfig {
....
project.ext.set("archivesBaseName", "MyName-" + defaultConfig.versionName);
}
이 결과를 얻을 수 있습니다.
MyName-X.X.X-release.apk