Android Studio --> Gradle Build設定自動

來源:互聯網
上載者:User

標籤:

ps:http://www.cnblogs.com/kangyi/p/4448398.html應用情境

通常情況下我們的apps發布後也就是release模式下log是不顯示的,debug模式下是顯示log的,但是在特殊情況下我們測試release包的時候需要log的時候,就無法使用BuildConfig.DEBUG來達到要求,因為在release模式下自動化佈建為false,debug模式下是true,這個時候我們需要自訂可控制的log開關。

Android Studio 對應的BuildConfig.java位置

在Studio中產生的目錄: /app/build/generated/source/buildConfig/ 檔案下的產品目錄裡面,找到想要的包名下會自動產生BuildConfig.java檔案。我們可以看看下release模式下該檔案的內容

/* * Automatically generated file. DO NOT MODIFY */package com.leo.kang.cetfour; public final class BuildConfig {  public static final boolean DEBUG = false;  public static final String APPLICATION_ID = "com.leo.demo";  public static final String BUILD_TYPE = "release";  public static final String FLAVOR = "baidu";  public static final int VERSION_CODE = 47;  public static final String VERSION_NAME = "3.6.1";  public static final boolean LEO_LOG = false;}

  

怎樣自訂BuildConfig欄位

在我們的build.gradle裡面加入如下代碼:

buildTypes {        release {            // 不顯示Log, 在java代碼中的調用方式為:BuildConfig.LOG_DEBUG            buildConfigField "boolean", "LEO_DEBUG", "false"             minifyEnabled true            zipAlignEnabled true            shrinkResources true            proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘             signingConfig signingConfigs.release        }         debug {            // 顯示Log            buildConfigField "boolean", "LEO_DEBUG", "true"             versionNameSuffix "-debug"            minifyEnabled false            zipAlignEnabled false            shrinkResources false            signingConfig signingConfigs.debug        }    }

 

文法為:

buildConfigField "boolean", "LEO_DEBUG", "true"

上述文法就定義了一個boolean類型的LEO_DEBUG欄位,值為true,之後我們就可以在程式中使用BuildConfig.LEO_DEBUG欄位來判斷我們所處的api環境。例如:

    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         CommonUtils.getVersionName(this);         initViews();         if(BuildConfig.LEO_DEBUG) {            Log.i("leo", "MainActivity.onCreate()");        }    }

  

 

Android Studio --> Gradle Build設定自動

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.