LoginSignup
14
12

More than 5 years have passed since last update.

Androidアプリ開発における build.gradle のビルドタイプごと設定やディレクトリ構成についての備忘録

Last updated at Posted at 2016-10-09

Androidアプリ開発における build.gradle のビルドタイプごと設定やディレクトリ構成についての備忘録.png

本記事の前段についてはこちら
iOSエンジニアのAndroidアプリ開発備忘録 - Qiita

はじめに

本記事は、私がAndroidアプリ開発時に行ったビルドタイプごとの build.gradle や、AndroidManifest.xml の記述内容、ディレクトリ構成 等を備忘録的にまとめたものになります。
誤りやもっと良い方法などあったらご指摘いただけるととてもありがたいです。

app/build.gradle

  • ビルドタイプ、Product Flavor、依存ライブラリ、署名の設定などは本ファイルに記述
  • ここで指定したビルドタイプ(× Product Flavor)の数が Build Variants となりビルドごとの設定を切り替えられる
  • keystore はdebug用、release用の2つ作成(developやstagingではdebug用のものを使用)
app/build.gradle
// プラグイン
apply plugin: "com.android.application"
    :

android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId "com.example.myapplication" // アプリケーションID
        minSdkVersion 19
        targetSdkVersion 24
        versionCode 1000000 // メジャーバージョンは ×1000000、ミドルバージョンは ×10000、マイナーバージョンは ×100、リビジョンは ×1。何かを参考にした
        versionName "1.0.0"
        multiDexEnabled true // multidex を有効/無効
    }
    // 署名設定
    signingConfigs {
        debug {
            storeFile file("debug.keystore")
            storePassword "password"
            keyAlias "alias"
            keyPassword "password"
        }
        release {
            storeFile file("production.keystore")
            storePassword "password"
            keyAlias "alias"
            keyPassword "password"
        }
    }
    // ビルドタイプ
    buildTypes {
        debug {
            debuggable true // デバッグモード有効。adb コマンドが叩けるようになる
            applicationIdSuffix ".debug" // application_id のサフィックス。これにより同端末にビルドタイプごとにアプリの同梱できる
            versionNameSuffix "-DEBUG" // バージョン名のサフィックス
            resValue "string", "app_name", "Debug My Application" // アプリケーション名を指定。デバッグモードとわかるような名前に変更
            signingConfig signingConfigs.debug // signingConfig で定義したものを指定
        }
        // develop { 必要に応じて }
        // staging { 必要に応じて }
        release {
            debuggable false // デバッグモード無効
            minifyEnabled true // プロガード 有効/無効
            proguardFiles getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro"
            shrinkResources true // リソースの圧縮
            zipAlignEnabled true // app の最適化 有効/無効 https://developer.android.com/studio/command-line/zipalign.html
            resValue "string", "app_name", "My Application"
            signingConfig signingConfigs.release
        }
    }
    // Product Flavors
    productFlavors {
        /*
        使ったことがないが、ビルドタイプとは別にアプリ内のモードの切り替えに使用するらしい
        (例えばアプリの 無料版/有料版 など)
        free { }
        pro { }
        これを追加することでビルドモードは buildTypes × productFlavors 個になる。
        (pro-debug, pro-release, free-debug, free-release)
        */
    }
    // ビルドタイプに応じたソースの参照先を指定(後述)
    sourceSets {
        main.java.srcDirs += "src/main/kotlin"
        debug.java.srcDirs += "src/debug/kotlin"
        // staging.java.srcDirs += "src/debug/kotlin"
        // develop.java.srcDirs += "src/develop/kotlin"
        release.java.srcDirs += "src/release/kotlin"
    }
}

dependencies {
    compile fileTree(dir: "libs", include: ["*.jar"])

    // test compile scope(テストビルドの時のみ有効)
    testCompile "junit:junit:X.X.X"

    // debug compile scope(デバッグビルドタイプの時のみ有効)
    debugCompile "com.example.xxx:X.X.X"

    // develop compile scope(開発ビルドタイプの時のみ有効)
    // staging compile scope(Stagingビルドタイプの時のみ有効)

    // release compile scope(デバッグビルドタイプの時のみ有効)
    releaseCompile "com.example.xxx:X.X.X"

    // compile scope(全ビルドタイプで有効)
    compile "com.example.xxx:X.X.X"
        :

    // 内製ライブラリで使用しているライブラリの記述
    compile "com.example.xxx:X.X.X"
        :
}

apply plugin: "com.google.gms.google-services"

repositories {
    mavenCentral()
    // dependencies のリポジトリURLを記述。ライブラリによって記述する必要がある場合がある
}

ディレクトリ

  • ビルドタイプごとに Application クラス、google-service.json、AndroidManifest.xml を作成(後述)
src
 ├ debug/kotlin
 |  |       └ com/example/myapplication/
 |  |                          └ DebugMyApplication.kt
 |  ├ AndroidManifest.xml
 |  └ google-services.json
 |
 ├ release/kotlin
 |   |       └ com/example/myapplication/
 |   |                          └ ReleaseMyApplication.kt
 |   ├ AndroidManifest.xml
 |   └ google-services.json
 |
 ├ develop/kotlin // 割愛
 |
 ├ staging/kotlin // 割愛
 |
 └ main/kotlin/
    |       └ com/example/myapplication/
    |                          ├ MyApplication.kt
    |                          |
    |                          ├ activities/
    |                          |     ├ XxxActivity.kt
    |                          |           :
    |                          ├ models/
    |                             :  ├ Xxxx.kt
    |                             :        :
    |                             :
    |
    ├ AndroidManifest.xml
    └ google-services.json

ビルドタイプごとの AndroidManifest.xml

Main

いつものように

AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.myapplication">

    <!-- Permissions -->

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <!-- Activities -->
        <activity android:name=".MainActivity">

        <!-- Receivers -->
               :
        <!-- Services -->
               :
        <!-- Metadata -->
               :
    </application>


</manifest>

Debug

  • android:name に個別に作成した Application クラスを指定
  • アプリ名の属性には tools:replace と指定
DebugAndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.myapplication">
    <application
        tools:replace="android:name"
        android:name=".DebugMyApplication">
    </application>
</manifest>

Develop, Staging, Release

割愛

Application クラス

Main

MyApplication.kt
class MyApplication: Application() {
    override fun onCreate() {
        super.onCreate()
            :
    }

        :

Debug

  • MyApplication 継承
DebugMyApplication.kt
class DebugMyApplication: MyApplication() {
    override fun onCreate() {
        super.onCreate()
            :
    }

        :

Develop, Staging, Release

割愛

まとめ

いろんな記事を参考に思い思いにやってみた結果です。
ご指摘やアドバイスいただけるとありがたいです。

疑問

ビルドタイプごとにルートレベルから applicationId を変更する方法がわからない。
com.example.myapplicationnet.fuga.hogeapplication に変更するみたいな

参考

14
12
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
14
12