LoginSignup
2
3

More than 5 years have passed since last update.

ライブラリプロジェクト(モジュール)のBuildConfigをDebugにする方法

Last updated at Posted at 2017-05-01

この記事の概要

開発をしている上で複数のプロジェクトで使いまわしたいコードをライブラリプロジェクトとしてモジュール化して使うことがあると思います。

本体のプロジェクトのビルド設定をデバッグにすればライブラリプロジェクトもデバッグになるかと思いきや、
ライブラリプロジェクトのビルド設定はデフォルトでは常にリリースビルドになります。

これではライブラリプロジェクト内でBuildConfigを使った処理の使い分けができません。
今回はライブラリプロジェクトを本体のプロジェクト設定に合わせる方法を紹介します。

プロジェクト構成例

以下のようなプロジェクト構成だとします。

・app //本体のプロジェクト
・module //ライブラリプロジェクト

settings.gradle
include ':app', ':module'
build.gradle(app)
//com.android.applicationを指定することで本体プロジェクトとして認識される
apply plugin: 'com.android.application' 
--  --

dependencies {
    
        ・
        ・
    compile project(':module')
    
        ・
        ・
}
build.gradle(module)
//com.android.libraryを指定することでライブラリプロジェクトとして認識される
apply plugin: 'com.android.library'

以上が特に何もしていない時の設定。

変更点

まずはmoduleのbuild.gradle。
publishNonDefault(true)を記載します。

build.gradle(module)
android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    publishNonDefault(true) ←これを追記
    
        ・
        ・
}

続いて、appのbuild.gradle
既存のcomplie project(':module')を消して以下のように変更。

build.gradle(app)
dependencies {
    
        ・
        ・
    debugCompile project(path: ':module', configuration: ':debug')
    releaseCompile project(path: ':module', configuration: ':release')
    
        ・
        ・
}

このようにしてやると、本体のプロジェクトに合わせてモジュールのビルド設定が切り替わります。

2
3
1

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
2
3