日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android gradle 多渠道打包小结

發(fā)布時(shí)間:2024/3/12 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android gradle 多渠道打包小结 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

概述

多渠道對(duì)于android來說是一個(gè)比較常見的概念,舉幾個(gè)常見的用法:

  • 根據(jù)不同的渠道使用不同的資源
  • 根據(jù)不同的渠道使用不同的依賴
  • 根據(jù)不同的渠道作不同的數(shù)據(jù)統(tǒng)計(jì)
  • 根據(jù)不同的渠道,游戲app中對(duì)應(yīng)不同的服務(wù)區(qū)
  • github地址

    本文項(xiàng)目基于筆者自己寫的demo,對(duì)其有興趣的讀者可以自行下載:
    https://github.com/Double2hao/MultiChannelTest

    android studio的多渠道

    如果要使用多渠道,僅需要在該項(xiàng)目的build.gradle文件中增加以下代碼:

    android {flavorDimensions "version"productFlavors {oneTest {}twoTest {}threeTest {}} }

    然后可以在android studio左側(cè)欄中的 Build Variants 中選擇module的渠道,如下圖:

    buildConfig區(qū)分不同的渠道

    通過buildConfigField可以在BuildConfig中設(shè)置不同的參數(shù),然后在代碼中可以通過BuildConfig的參數(shù)來區(qū)分不同的渠道。

    productFlavors {oneTest {buildConfigField("String", "TEST_CHANNEL", "\"one\"")}twoTest {buildConfigField("String", "TEST_CHANNEL", "\"two\"")}threeTest {buildConfigField("String", "TEST_CHANNEL", "\"three\"")}}

    Demo中BuildConfig的代碼如下:

    public final class BuildConfig {public static final boolean DEBUG = Boolean.parseBoolean("true");public static final String APPLICATION_ID = "com.example.multichanneltest";public static final String BUILD_TYPE = "debug";public static final String FLAVOR = "threeTest";public static final int VERSION_CODE = 1;public static final String VERSION_NAME = "1.0";// Field from product flavor: threeTestpublic static final String TEST_CHANNEL = "three"; }

    Demo中BuildConfig的使用代碼如下:

    public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//根據(jù)不同的渠道參數(shù)來作不同的邏輯if (TextUtils.equals(BuildConfig.TEST_CHANNEL, "one")) {} else if (TextUtils.equals(BuildConfig.TEST_CHANNEL, "two")) {} else if (TextUtils.equals(BuildConfig.TEST_CHANNEL, "three")) {}} }

    manifest區(qū)分不同的渠道

    通過使用manifestPlaceholders,為不同的渠道設(shè)置不同的值。

    productFlavors {oneTest {manifestPlaceholders = [test_app_name: "TestOneApp"]}twoTest {manifestPlaceholders = [test_app_name: "TestTwoApp"]}threeTest {manifestPlaceholders = [test_app_name: "TestThreeApp"]}}

    Demo中為不同的渠道設(shè)置了不同的appName,代碼如下:

    <applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="${test_app_name}"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/Theme.MultiChannelTest"></application>

    設(shè)置不同渠道的資源

    通過設(shè)置sourceSets,可以為不同的渠道設(shè)置不同的資源。
    如下,Demo中的代碼,在不同的渠道下,使用不同的java資源。
    如果在oneTest的渠道下,"src/main/twoTest"與"src/main/threeTest"目錄下的文件不會(huì)參與編譯。

    android {sourceSets {oneTest {java {srcDirs = ["src/main/java", "src/main/oneTest"]}}twoTest {java {srcDirs = ["src/main/java", "src/main/twoTest"]}}threeTest {java {srcDirs = ["src/main/java", "src/main/threeTest"]}}} }

    總結(jié)

    以上是生活随笔為你收集整理的android gradle 多渠道打包小结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。