使用不同的类为android构建变体

时间:2018-05-01 08:55:45

标签: android

我有6个应用程序都使用一个主要版本来运行。它们都有4个标签按钮,但我想将一个应用程序切换为5个标签栏按钮。

所以我已经配置了一个有4个标签栏按钮的主类,但是对于一个应用程序,我希望它覆盖它并使用5个标签栏按钮。我只是不确定如何改变课程。

任何帮助都将非常感谢

编辑:同样,如果你是downvote,请说明原因。如果我不清楚我在问什么,或者它是否是一个简单的问题等。

这是我尝试交换的课程

package com.android.stanby.app.domain;

import com.android.stanby.app.R;

/**
 * Created by trevor.wood on 2018/05/01.
 */

public class BottomMenuButtons {

    public static final int TAB_INDEX_JOB_LIST = 0;
    public static final int TAB_INDEX_JOB_MAP = 1;
    public static final int TAB_INDEX_WEB = 2;
    public static final int TAB_INDEX_RECOMMEND = 3;
    public static final int TAB_INDEX_CHAT = 4;

    public static final int[] NAVI_ITEMS = {
            R.id.bottom_navigation_job_list,
            R.id.bottom_navigation_job_map,
            R.id.bottom_navigation_keep,
            R.id.bottom_navigation_recommend,
            R.id.bottom_navigation_chat,
    };
    public static final int[] NAVI_ICONS = {
            R.id.bottom_navigation_job_list_icon,
            R.id.bottom_navigation_job_map_icon,
            R.id.bottom_navigation_keep_icon,
            R.id.bottom_navigation_recommend_icon,
            R.id.bottom_navigation_chat_icon,
    };
    public static final int[] NAVI_OFF_ICONS = {
            R.drawable.stanby_ic_bottom_tab_job_off,
            R.drawable.stanby_ic_bottom_tab_job_map_off,
            R.drawable.stanby_ic_bottom_tab_keep_off,
            R.drawable.stanby_ic_bottom_tab_recommend_off,
            R.drawable.stanby_ic_bottom_tab_chat_off,
    };
    public static final int[] NAVI_ON_ICONS = {
            R.drawable.stanby_ic_bottom_tab_job_on,
            R.drawable.stanby_ic_bottom_tab_job_map_on,
            R.drawable.stanby_ic_bottom_tab_keep_on,
            R.drawable.stanby_ic_bottom_tab_recommend_on,
            R.drawable.stanby_ic_bottom_tab_chat_on,
    };
}

这是我的构建设置

def PACKAGE_NAME = "com.stanby.jp"
// VERSION_CODE及びVERSION_NAMEはgradle.propertiesに定義されている
def VERSION_CODE = APP_VERSION_CODE.toInteger()
def VERSION_NAME = APP_VERSION_NAME

apply plugin: 'com.android.application'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.3"

    defaultConfig {
        minSdkVersion 16
        targetSdkVersion 25
        versionCode VERSION_CODE
        versionName VERSION_NAME
        multiDexEnabled true

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    signingConfigs {
        release {
            keyAlias STANBY_KEY_ALIAS
            keyPassword STANBY_KEY_PASSWORD
            storeFile file(System.getenv("HOME") + "/.android/" + STANBY_STORE_FILE)
            storePassword STANBY_STORE_PASSWORD
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    // フレーバー
    flavorDimensions "type", "env"
    productFlavors {
        // 全部入り
        stanby {
            dimension "type"
            applicationId "${PACKAGE_NAME}"
        }

        // アルバイト・パート
        part {
            dimension "type"
            applicationId "${PACKAGE_NAME}.part"
        }

        // 転職(正社員)
        full {
            dimension "type"
            applicationId "${PACKAGE_NAME}.full"
        }

        // ハローワーク
        hellowork {
            dimension "type"
            applicationId "${PACKAGE_NAME}.hellowork"
        }

        // 富山県
        toyama {
            dimension "type"
            applicationId "${PACKAGE_NAME}.toyama"
        }

        // 福島県
        fukushima {
            dimension "type"
            applicationId "${PACKAGE_NAME}.fukushima"
        }

        // 福岡県
        fukuoka {
            dimension "type"
            applicationId "${PACKAGE_NAME}.fukuoka"
        }

        develop {
            dimension "env"
        }
        product {
            dimension "env"
        }
    }

    // ビルドタイプ
    buildTypes {
        release {
            signingConfig signingConfigs.release
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.txt'
            buildConfigField "boolean", "USE_CRASHLYTICS", "true"
            ext.enableCrashlytics = true
        }
        debug {
            //signingConfig signingConfigs.release
            buildConfigField "boolean", "USE_CRASHLYTICS", "false"
            ext.enableCrashlytics = false
        }
    }

    // ソース構成
    sourceSets {
        // 全部入り
        stanby {
            java.srcDirs = ['src/stanby/java', 'src/common/java']
            res.srcDirs = ['src/stanby/res', 'src/common/res']
        }

        // アルバイト・パート
        part {
            java.srcDirs = ['src/part/java', 'src/common/java']
            res.srcDirs = ['src/part/res', 'src/common/res']
        }

        // 転職(正社員)
        full {
            java.srcDirs = ['src/full/java', 'src/common/java']
            res.srcDirs = ['src/full/res', 'src/common/res']
        }

        // ハローワーク
        hellowork {
            java.srcDirs = ['src/hellowork/java', 'src/common/java']
            res.srcDirs = ['src/hellowork/res', 'src/common/res']
        }

        // 富山県
        toyama {
            java.srcDirs = ['src/toyama/java']
            res.srcDirs = ['src/toyama/res']
        }

        // 福島県
        fukushima {
            java.srcDirs = ['src/fukushima/java']
            res.srcDirs = ['src/fukushima/res']
        }

        // 福岡県
        fukuoka {
            java.srcDirs = ['src/fukuoka/java']
            res.srcDirs = ['src/fukuoka/res']
        }

        // 検証環境
        develop {
            java.srcDirs = ['src/develop/java']
            res.srcDirs = ['src/develop/res']
        }

        // 本番環境
        product {
            java.srcDirs = ['src/product/java']
            res.srcDirs = ['src/product/res']
        }
    }

    // google-services.json を develop/product からコピーする
    gradle.taskGraph.beforeTask { Task task ->
        if (task.name ==~ /process.*GoogleServices/) {
            applicationVariants.all { variant ->
                if (task.name ==~ /(?i)process${variant.name}GoogleServices/) {
                    String fromDir = "${variant.flavorName}";
                    if (fromDir.endsWith("Develop")) {
                        fromDir = "develop";

                    } else if (fromDir.endsWith("Product")) {
                        fromDir = "product";
                    }
                    print "\n#####################################################\n";
                    print "google-services.json fromDir=${fromDir}\n"
                    print "#####################################################\n\n";

                    copy {
                        from "src/" + fromDir
                        into "."
                        include "google-services.json"
                    }
                }
            }
        }
    }
}

apply plugin: 'com.neenbedankt.android-apt'

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    // https://developer.android.com/topic/libraries/support-library/revisions.html
    def supportLibraryVersion = '25.3.1'
    // https://developers.google.com/android/guides/releases
    def playServiceVersion = '11.8.0'
    // https://github.com/firebase/FirebaseUI-Android
    def firebaseUiDatabaseVersion = '1.2.0'

    def retrofitVersion = '2.1.0'
    def okHttpVersion = '3.4.1'
    def daggerVersion = '2.6'
    def butterknifeVersion = '8.3.0'
    def rxLifecycleVersion = '0.7.0'

    compile fileTree(dir: 'libs', include: ['*.jar'])

    // 計測
    compile('com.crashlytics.sdk.android:crashlytics:2.8.0@aar') {
        transitive = true;
    }
    compile 'com.adjust.sdk:adjust-android:4.2.1'

    // support library
    compile 'com.android.support:multidex:1.0.1'
    compile "com.android.support:support-v4:${supportLibraryVersion}"
    compile "com.android.support:appcompat-v7:${supportLibraryVersion}"
    compile "com.android.support:design:${supportLibraryVersion}"
    compile "com.android.support:recyclerview-v7:${supportLibraryVersion}"
    compile "com.android.support:cardview-v7:${supportLibraryVersion}"

    // Play service
    compile "com.google.android.gms:play-services-analytics:${playServiceVersion}"
    compile "com.google.android.gms:play-services-location:${playServiceVersion}"
    compile "com.google.android.gms:play-services-maps:${playServiceVersion}"
    compile 'com.google.maps.android:android-maps-utils:0.4.3'

    // Firebase
    compile "com.google.firebase:firebase-core:${playServiceVersion}"
    compile "com.google.firebase:firebase-messaging:${playServiceVersion}"
    compile "com.google.firebase:firebase-config:${playServiceVersion}"
    compile "com.google.firebase:firebase-auth:${playServiceVersion}"
    compile "com.google.firebase:firebase-database:${playServiceVersion}"
    compile "com.google.firebase:firebase-invites:${playServiceVersion}"
    compile "com.firebaseui:firebase-ui-database:${firebaseUiDatabaseVersion}"

    // retrofit
    compile "com.squareup.retrofit2:retrofit:${retrofitVersion}"
    compile "com.squareup.retrofit2:adapter-rxjava:${retrofitVersion}"
    compile "com.squareup.retrofit2:converter-gson:${retrofitVersion}"

    // okHttp
    compile "com.squareup.okhttp3:logging-interceptor:${okHttpVersion}"

    // Glide
    compile 'com.github.bumptech.glide:glide:3.7.0'

    // Twillio
    compile 'com.koushikdutta.ion:ion:2.1.7'
    compile 'com.twilio:conversations-android:0.12.2'

    // 求人詳細の WebView レイアウト
    compile 'com.samskivert:jmustache:1.12'

    // base framework
    apt "com.google.dagger:dagger-compiler:$daggerVersion"
    compile "com.google.dagger:dagger:$daggerVersion"
    apt "com.jakewharton:butterknife-compiler:$butterknifeVersion"
    compile "com.jakewharton:butterknife:$butterknifeVersion"

    compile "com.trello:rxlifecycle:$rxLifecycleVersion"
    compile "com.trello:rxlifecycle-android:$rxLifecycleVersion"
    compile "com.trello:rxlifecycle-components:$rxLifecycleVersion"
    compile 'io.reactivex:rxandroid:1.1.0'

    // library
    compile 'com.facebook.android:facebook-android-sdk:4.25.0'
    compile 'com.viewpagerindicator:library:2.4.1@aar'
    compile 'com.wefika:flowlayout:0.4.1'
    compile 'com.ncapdevi:frag-nav:1.0.3'
    compile 'com.github.ksoichiro:android-observablescrollview:1.6.0'
    compile 'com.github.2359media:EasyAndroidAnimations:0.8'
    compile 'commons-codec:commons-codec:1.10'
    compile 'net.danlew:android.joda:2.9.2'

    // test
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:1.10.19'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    androidTestCompile "com.android.support:support-annotations:$supportLibraryVersion"
    androidTestCompile 'com.android.support.test:runner:0.5'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2') {
        exclude group: 'com.google.code.findbugs', module: 'jsr305'
    }
    androidTestCompile("com.squareup.retrofit2:retrofit-mock:$retrofitVersion") {
        exclude group: 'com.squareup.okio', module: 'okio'
        exclude group: 'com.squareup.okhttp3', module: 'okhttp'
    }
    androidTestCompile('com.squareup.assertj:assertj-android:1.1.1') {
        exclude group: 'com.squareup.okio', module: 'okio'
        exclude group: 'com.squareup.okhttp3', module: 'okhttp'
    }
}

apply plugin: 'com.google.gms.google-services'
apply plugin: 'me.tatarka.retrolambda'
apply plugin: 'io.fabric'
apply plugin: 'realm-android'

2 个答案:

答案 0 :(得分:1)

您可以通过在项目中创建具有相同名称的相应文件夹来覆盖每个变体的分类。查看解释所需目录结构的SO link。然后在编译该类时会自动覆盖该类。

答案 1 :(得分:1)

您需要为它创建flavor目录。例如,如果要为fukusima flavor创建特定类,则需要在fukusima目录中创建src目录。像这样:

src/fukusima/java/your/package/name/bottommenu

然后在那里创建BottomMenuButtons。将your/package/name/bottommenu更改为BottomMenuButtons路径。

另一种方法是使用标志为您的味道添加特定标志。首先,为您的味道添加一个特定的标志。例如,我们使用USE_FIVE_TAB。通过添加标志来改变味道:

productFlavors {
    stanby {
        buildConfigField "boolean", "USE_FIVE_TAB", "false"
        dimension "type"
        applicationId "${PACKAGE_NAME}"
    }
    part {
        buildConfigField "boolean", "USE_FIVE_TAB", "false"
        dimension "type"
        applicationId "${PACKAGE_NAME}.part"
    }

    full {
        buildConfigField "boolean", "USE_FIVE_TAB", "true"
        dimension "type"
        applicationId "${PACKAGE_NAME}.full"
    }

    // add the same flag to all of your flavors
    ...

然后,您可以使用以下内容:

if(BuildConfig.USE_FIVE_TAB) {
   // set to use five tabs
} else {
   // use four tabs
}