1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

モジュールのbuild.gradleをざっくりと読み解く

Last updated at Posted at 2018-10-28

備忘録も兼ねてモジュールのbuild.gradleの見方をざっくりと解説

今回使うサンプルのgradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.sho.gof"
        minSdkVersion 27
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            packageNameSuffix "release"
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    productFlavors {
        dev {
            dimension "stage"
            versionName "1.0-development"
        }
        testing {
            dimension "stage"
            versionName "1.0-testing"
        }
        staging {
            dimension "stage"
            versionName "1.0-staging"
        }
        production {
            dimension "stage"
            versionName "1.0-production"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

apply

ビルドスクリプトで使うプラグインの宣言

androidブロック

Androidアプリケーション固有の設定

  • defaultConfigブロック

    • アプリのバージョン管理
    • File -> Project Structure -> Flavors タブに対応
  • buildTypesブロック

    • ビルドタイプの管理と個別の設定を記述(標準ではdebugとrelease、明示的にreleaseが書いてあるがdebugも有効になっている)
      • 例えば上記例ではreleaseだけpackageNameの末尾にreleaseと命名することで使い分けが容易
    • File -> Project Structure -> Build Types タブに対応
  • productFlavors

    • アプリケーションごと管理するのに使う
      • 上記例では、開発用(dev), テスト環境用(testing), ステージング環境用(staging), 本番用(production)とすることで、開発での動作確認ではdevelopmentを、本番リリース用にはproductionと使い分けができる

そして、これらのbuildTypesとproductFlavorsの組み合わせにより、様々な環境のAPKファイルをビルドすることができる この概念がBuild Variantである。

Build Variants = productFlavors × buildTypes スクリーンショット 2018-10-28 23.35.33.png
Android Studioではこの部分から操作してビルドしたい環境を指定する

dependenciesブロック

依存関係(導入しているライブラリ、SDKのバージョン)を記述する
ライブラリやSDKではAndroidの対応バージョンや他ライブラリと依存している場合があり、それらを管理するのに用いる

依存関係を見たい場合は、

  • IDEの一番右にあるGradleをクリック
  • Tasks -> android -> androidDependenciesをクリック
  • 右下にあるGradle Consoleを確認

スクリーンショット 2018-10-28 23.47.17.png
こんな感じで出てればオーケー

最後に

モジュールのbuild.gradleをざっくりと解説してみました
gradleの設定はかなり奥が深いのですが、使いこなせればAndroidの開発をかなりハックすることができると思います
私自身実力がまだまだなので精進していく所存

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?