Android項目配置多環境多包名

1.配置keystore

在gradle.properties中添加keystore配置信息

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official

#添加下面keystore配置信息
MYAPP_RELEASE_STORE_FILE=keystore.keystore
MYAPP_RELEASE_KEY_ALIAS=bieming
MYAPP_RELEASE_KEY_PASSWORD=123456
MYAPP_RELEASE_STORE_PASSWORD=123456

2.build.gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.zian.test1"
        minSdkVersion 26
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        flavorDimensions "default"
    }

    signingConfigs {
        release {
            storeFile file(MYAPP_RELEASE_STORE_FILE)
            storePassword MYAPP_RELEASE_STORE_PASSWORD
            keyAlias MYAPP_RELEASE_KEY_ALIAS
            keyPassword MYAPP_RELEASE_KEY_PASSWORD
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'

            signingConfig signingConfigs.release
        }
    }

    productFlavors {
        //開發環境
        version_develop {
            buildConfigField "int", "ENV_TYPE", "1"
            applicationId 'com.zian.test1'
            manifestPlaceholders = [
                    app_name: "Test1",
                    app_icon: "@mipmap/ic_laucher",
                    app_round_icon: "@mipmap/ic_laucher",
            ]
        }
        //測試環境
        version_test {
            buildConfigField "int", "ENV_TYPE", "2"
            applicationId 'com.zian.test2'
            manifestPlaceholders = [
                    app_name: "Test2",
                    app_icon: "@mipmap/ic_laucher",
                    app_round_icon: "@mipmap/ic_laucher",
            ]
        }
        //生產環境
        version_product {
            buildConfigField "int", "ENV_TYPE", "3"
            applicationId 'com.zian.test3'
            manifestPlaceholders = [
                    app_name: "Test3",
                    app_icon: "@mipmap/ic_laucher",
                    app_round_icon: "@mipmap/ic_laucher",
            ]
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.core:core-ktx:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}

3.AndroidManifest.xml

    <application
            android:allowBackup="true"
            android:label="${app_name}"
            android:icon="${app_icon}"
            android:roundIcon="${app_round_icon}"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

    </application>

4.Terminal編譯安裝命令

Debug包: gradlew app:installVersion_developDebug
Release包: gradlew app:installVersion_productRelease

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章