【Android】多版本控制

2020-12-24  本文已影响0人  urkay

开启多版本方式:在app下的build.gradle添加如下设置

android {
sourceSets {
        main {
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java.srcDirs = ['src/main/java']
            resources.srcDirs = ['src/main/resources']
            aidl.srcDirs = ['src/main/aidl']
            renderscript.srcDirs = ['src/maom']
            res.srcDirs = ['src/main/res']
            assets.srcDirs = ['src/main/assets']
            jniLibs.srcDir 'src/main/jniLibs'
        }

        //用各自对应的资源文件路径
        newapp.res.srcDirs = ['src/main/res-newapp']
        xqapp.res.srcDirs = ['src/main/res-xqapp']
        // Move the tests to tests/java, tests/res, etc...
//        instrumentTest.setRoot('tests')
        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
//        debug.setRoot('build-types/debug')
//        release.setRoot('build-types/release')
    }

    flavorDimensions "default"
    productFlavors {
        defaultapp {
            manifestPlaceholders = [channel: "defaultapp", app_name: "默认app"]
            resValue("string", "strKey", "defaultapp")
        }
        newapp {
            manifestPlaceholders = [channel: "newapp", app_name: "新版app1"]
            resValue("string", "strKey", "newapp")
        }
        xqapp {
            manifestPlaceholders = [channel: "xqapp", app_name: "新版app2"]
            resValue("string", "strKey", "xqapp")
        }
    }
}

例:main下的资源是公用的
各自对应的资源文件路径如src/main/res-newapp 只需添加newapp需要的资源。
若存在同名资源,各版本优先使用对应设置路径下的资源,类似覆盖操作
src/main/res-newapp/drawablesrc/main/res/drawable都存在logo.png
则在newapp版本优先使用src/mainres-newapp/drawable下的logo.png
切换defaultapp版本则使用的是src/main/res/drawable下的logo.png

调试切换在Android Stuido 左下角 image.png
上一篇下一篇

猜你喜欢

热点阅读