Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sahara-ooga/83600c3c931f5fd87a802bf67b7c33bd to your computer and use it in GitHub Desktop.
Save sahara-ooga/83600c3c931f5fd87a802bf67b7c33bd to your computer and use it in GitHub Desktop.
Androidで、ビルド設定に応じて切り替える

ReleaseビルドとDebugビルド

コード上でのビルド設定の参照

まず、BuildConfigファイルを参照します。

/**
 * Automatically generated file. DO NOT MODIFY
 */
package com.example.deviceangledetector;

public final class BuildConfig {
  public static final boolean DEBUG = Boolean.parseBoolean("true");
  public static final String APPLICATION_ID = "com.example.deviceangledetector";
  public static final String BUILD_TYPE = "debug";
  public static final String FLAVOR = "";
  public static final int VERSION_CODE = 1;
  public static final String VERSION_NAME = "1.0";
}

したがって、debug ビルド時のみに処理したい場合は次のように書けます。

if (BuildConfig.DEBUG) {
    // ...
}

ビルドを切り替える方法

debug / release ビルドは Android Studio の Build Variants タブで切り替えることが出来ます。

build-variants

Product Flavorについて

Android StudioでProduct Flavorを使う - Qiita参照のこと。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment