app自動命名,更換ICON圖標(biāo),打包apk配置

1.在gradle編譯的時候,通過一個變量切換成自己需要的項目(包名,應(yīng)用圖標(biāo),應(yīng)用名稱)
2.打包apk配置,按照需要的格式命名apk
關(guān)鍵點:getBuildType() ,設(shè)置這個返回值,處理相關(guān)的值(包名,應(yīng)用圖標(biāo),應(yīng)用名稱)
附注:這個只是切換不同的編譯,不是多渠道打包

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"

    defaultConfig {
        //動態(tài)修改包名
        applicationId "com.iim.myapplication.${getPageName()}"
        minSdkVersion 22
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    //簽名的配置
    //注意,signingConfigs要在buildTypes前面
    signingConfigs {
        //正式配置
        release {
            //放簽名文件的目錄
            //項目/app/keystore
            storeFile file("./keystore/demo_text.jks")
            keyAlias 'test'
            keyPassword '123456'
            storePassword '123456'
            //簽名的v1, v2模式
            v1SigningEnabled true
            v2SigningEnabled true
        }
//        debug{
//           需要 debug需要不同的配置
//        }
    }


    buildTypes {
        release {
            //自定義字段
            buildConfigField "int", "BUIL_VERSION_TYPE", "${getBuildType()}"

            //使用release的配置
            signingConfig signingConfigs.release
            //開啟混淆
            minifyEnabled true
            //混淆配置
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            flavorDimensions "default"
        }

        debug {
            buildConfigField "int", "BUIL_VERSION_TYPE", "${getBuildType()}"
            signingConfig signingConfigs.release
            minifyEnabled false
            debuggable true
            flavorDimensions "default"
            signingConfig signingConfigs.release
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

    android.applicationVariants.all {
        variant ->
            variant.outputs.all {
                //這里修改apk文件名
                //打包的apk:哈哈_測試-01_v1.0_1_20200512_release.apk
                outputFileName = "哈哈_${getTypeName()}_v${defaultConfig.versionName}_${defaultConfig.versionCode}_${getCurrentTime()}_${buildType.name}.apk"
            }
    }

    productFlavors {
        dev {
            //更改包名
//            applicationId "com.iim.myapplication.dev1"
            manifestPlaceholders = [ENVIRONMENT:"dev",app_icon: "${getTypeIcon()}"]

            //動態(tài)增加string.xml的字段
            resValue "string", "app_name_text", "${getTypeName()}"
        }

        //多渠道配置 --> http://m.itdecent.cn/p/00da6153f77e
//        dev02{
//            applicationId "com.iim.myapplication.dev2"
//            //app圖標(biāo)
//            manifestPlaceholders = [ENVIRONMENT:"dev02",app_icon: "@drawable/test_2"]
//            //app名稱
//            resValue "string", "app_name_text", "測試-2"
//        }
//
//        dev03{
//            applicationId "com.iim.myapplication.dev3"
//            //app圖標(biāo)
//            manifestPlaceholders = [ENVIRONMENT:"dev03",app_icon: "@drawable/test_3"]
//            //app名稱
//            resValue "string", "app_name_text", "測試-3"
//        }
    }
}

//獲取編譯的版本, 0:測試1版本 1:測試2版本  2:測試3版本
//同時動態(tài)修改包名,app名稱
static def getBuildType() {
    return 1
}


//獲取當(dāng)前時間
static def getCurrentTime() {
    return new Date().format("yyyyMMdd")
}

static def getPageName() {
    return "test${getBuildType()}"
}

static def getTypeName() {
    return "測試-${getBuildType()}"
}

static def getTypeIcon() {
    if (getBuildType() == 1) {
        return "@drawable/test_1"
    } else if (getBuildType() == 2) {
        return "@drawable/test_2"
    } else {
        return "@drawable/test_3"
    }

}


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

自動配置app的名稱,圖標(biāo)
關(guān)鍵點:icon, label

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iim.myapplication">

    <application
        android:allowBackup="true"
        android:icon="${app_icon}"
        android:label="@string/app_name_text"
        android:roundIcon="@mipmap/ic_launcher_round"
        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>

</manifest>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容