简体   繁体   中英

Use different class for android build variant

I have 6 apps that all use one main build to run off of. They all have 4 tab buttons but I'd like to switch one app to have 5 tab bar buttons.

So I've configured a main class that has 4 tab bar buttons, however for one app I'd like it to override that and use 5 tab bar buttons. I'm just not sure how to change out the classes.

Any help at all would be greatly appreciated

EDIT: also, if you downvote, please say why. If it's unclear what I'm asking or if it's a simple question etc.

Here's the class I'm trying to exchange

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,
    };
}

And here are my build settings

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'

You can override classses per variant by creating the corresponding folder in the project with the same name for the flavor. Check this SO link that explains the directory structure needed. Then automatically when you compile that flavor the class will be overriden.

You need to create your flavor directory for it. For example, if you want to create specific class for fukusima flavor, you need to create the fukusima directory inside your src directory. Like this:

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

then you create the BottomMenuButtons there. Change your/package/name/bottommenu to your BottomMenuButtons path.

The other way is using a flag for adding specific flag for your flavor. First, add a specific flag to your flavor. For example, we use USE_FIVE_TAB . Change the flavor by adding the flag:

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
    ...

Then, you can use it something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM