Android动态配置ApplicationId,App名字,AppLogo


需求:同一套代码,打一个京东,腾讯,小米的包,logo都是对应公司的logo,App名字为:京东,腾讯,小米
一、打开项目,在src目录下,与main同级的地方,将要打的包新建三个文件夹,分别以jingdong,tengxun,xiaomi命名,在这三个文件夹下面就放着和其他两个App不同的东西,比如logo图片,App名字等,结构如图:

strings.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">小米</string>
</resources>

styles.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="LaunchTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <!-- Show a splash screen on the activity. Automatically removed when
             Flutter draws its first frame -->
        <item name="android:windowBackground">@drawable/launch_background</item>
    </style>
</resources>

 

二、文件路径app ==> src ==> main ==> AndroidManifest.xml 文件配置

<application
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher"
android:theme="@style/LaunchTheme"
tools:replace="android:allowBackup,icon,theme,label">
...
</application>

三、对App 的build.gradle(android ==> app ==> build.gradle)进行配置,其中主要是productFlavors的配置,

注意点:

flavorDimensions "default"

//不同APP的配置
productFlavors {
//TODO:....
}

配置如下

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

//打包时间
def releaseTime() {
    return new Date().format("yyyyMMddHHmm")
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    flavorDimensions "default"
    signingConfigs {
        debug {
            storeFile file('AndroidKeyStore')
            storePassword '1234567'
            keyAlias = 'keyforproject'
            keyPassword '123456'
        }
        release {
            storeFile file('AndroidKeyStore')
            storePassword '1234567'
            keyAlias = 'keyforproject'
            keyPassword '123456'
        }
    }
    compileSdkVersion 28

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    lintOptions {
        disable 'InvalidPackage'
    }

    //APP默认配置
    defaultConfig {

        applicationId = 'com.xiaoxiao.tengxun'
        signingConfig signingConfigs.debug

        minSdkVersion 16
        targetSdkVersion 28
        versionName "1.0.1"
        versionCode 10
//        versionCode flutterVersionCode.toInteger()
//        versionName flutterVersionName

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

        ndk {
            //选择要添加的对应 cpu 类型的 .so 库。
            abiFilters 'armeabi', 'armeabi-v7a', 'x86', 'x86_64', 'mips', 'mips64', 'arm64-v8a'
        }

        manifestPlaceholders = [
                JPUSH_PKGNAME : applicationId,
                JPUSH_APPKEY : "appkey", // NOTE: JPush 上注册的包名对应的 Appkey.
                JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
        ]

    }

    //不同APP的配置
    productFlavors {
        jingdong {
            applicationId "com.xiaoxiao.jingdong"
            resValue "string", "app_name", "jingdong"
            manifestPlaceholders = [CHANNEL_VALUE: "jingdong"
                                    ,app_icon   : "@mipmap/ic_launcher"]
        }
        tengxun {
            applicationId "com.xiaoxiao.tengxun"
            resValue "string", "app_name", "tengxun"
            manifestPlaceholders = [CHANNEL_VALUE: "tengxun"
                                    ,app_icon   : "@mipmap/ic_launcher"]
        }
        xiaomi {
            applicationId "com.xiaoxiao.xiaomi"
            resValue "string", "app_name", "xiaomi"
            manifestPlaceholders = [CHANNEL_VALUE: "xiaomi",
                                    app_icon   : "@mipmap/ic_launcher",
                                    JPUSH_PKGNAME : applicationId,
                                    JPUSH_APPKEY : "appkey", // NOTE: JPush 上注册的包名对应的 Appkey.
                                    JPUSH_CHANNEL : "developer-default", //暂时填写默认值即可.
            ]
        }
    }

    buildTypes {
        //测试版本
        debug {
            /* 签名类型 */
            signingConfig signingConfigs.debug
            /* 是否开启代码混淆,默认false */
            minifyEnabled false
            /* 是否应该生成可调试的apk */
            debuggable true
            /* 混淆规则配置文件 */
//            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            /* 自定义buildType */
//            buildConfigField 'String', 'BASE_URL', '"http://api-debug.**/"'
        }

        //生产版本
        release {
            /* 签名类型 */
            signingConfig signingConfigs.release
            /* 是否开启代码混淆,默认false */
            minifyEnabled false
            /* 是否应该生成可调试的apk */
            debuggable false
            /* 移除无用的resource文件 */
//            shrinkResources true
            /* 混淆规则配置文件 */
//            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            /* 自定义buildType */
//            buildConfigField 'String', 'BASE_URL', '"http://api-release.**/"'
        }

        //预生产版本
        /* 从给定的构建类型复制所有属性 */
        pre.initWith(release)
        pre {
            buildConfigField "String", "BASE_URL", "http://api-pre.**/"
            matchingFallbacks = ['pre', 'debug', 'release']
        }


    }

//      当执行"Generate Signed Bundle or APK"时候,取消注释,生成apk文件名
//    android.applicationVariants.all { variant ->
//        variant.outputs.all { output ->
//            def outputFile = output.outputFile
//            if (outputFile != null && outputFile.name.endsWith('.apk')) {
//                //这里修改apk文件名
//                def fileName = "hu_${variant.productFlavors[0].name}-${defaultConfig.versionCode}-${defaultConfig.versionName}-${releaseTime()}.apk"
//                //def fileName = outputFile.name.replace("app", "${rootProject.ext.appName}-${releaseTime()}-${defaultConfig.versionCode}-${defaultConfig.versionName}")
//                //                output.outputFile = new File(outputFile.parent, fileName)
//                outputFileName = fileName
//            }
//        }
//    }


}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

 

参考链接

Android动态配置ApplicationId,App名字,AppLogo

一次搞懂Android签名文件的生成与配置


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM