diy886
V2EX  ›  Android

有办法设置打包 apk 时,不打包部分资源吗

  •  
  •   diy886 · Dec 27, 2019 · 15876 views
    This topic created in 2353 days ago, the information mentioned may be changed or developed.

    公司的项目是一套多用,基本就是换个 appicon 和启动页就给另一个客户使用,于是就用 productFlavors 来打包,大概是这样配置的

     productFlavors {
            a {
                manifestPlaceholders = [app_icon: "@drawable/logo_a"]
                buildConfigField "int", "loading_bg", "R.drawable.bg_a"
               }
            b {
                manifestPlaceholders = [app_icon: "@drawable/logo_b"]
                buildConfigField "int", "loading_bg", "R.drawable.bg_b"
               }
              .
              .
              .
    

    原来还好,就一两个,但是现在有大概 30 个客户了,就有很多启动页都被一起打包进 apk,apk 变得很大。

    试过混淆 shrinkResources true 没有用

    也试了 sourceSets exclude 的配置也没生效

        sourceSets {
            b {
                resources {
                    exclude "**/bg_a.png"
                }
            }
        }
    

    不知道是不是写错了,有经验的大佬帮忙看下,谢谢了

    14 replies    2019-12-28 12:55:26 +08:00
    stephonhu
        1
    stephonhu  
       Dec 27, 2019   ❤️ 1
    可以在 src 下和 main 同级创建对应 productFlavors 名字的文件夹,里面目录和 main 一样,然后把启动页放到对应 productFlavors 的 drawable 下就好了呀
    yaming116
        2
    yaming116  
       Dec 27, 2019 via iPhone
    做一个脚本替换资源打包好了呀,简单的就是一个客户一个文件夹,客户再多就自己写一个 web 界面管理
    diy886
        3
    diy886  
    OP
       Dec 27, 2019
    @stephonhu 然后用 exclude 排除其他文件夹吗?
    iseejun
        4
    iseejun  
       Dec 27, 2019
    脚本跑起来呀,第三方打包都是替换公共资源
    目前我这边就是这样做
    diy886
        5
    diy886  
    OP
       Dec 27, 2019
    @iseejun 老哥怎么搞,有没有教程文章啥的,没弄过啊
    0DBBFF
        6
    0DBBFF  
       Dec 27, 2019
    https:
    //developer.android.com/
    studio/
    build
    /optimize-your-build#reduce_resources

    上面去掉换行的连接
    mrleft
        7
    mrleft  
       Dec 27, 2019
    一楼说得对 gradle flavour 本身就能实现
    Tomey
        8
    Tomey  
       Dec 27, 2019
    一楼的做法是可以实现的
    sunrain
        9
    sunrain  
       Dec 27, 2019
    百度 tools:discard
    royliu
        10
    royliu  
       Dec 27, 2019
    我们项目也有这个需求,就是通过一楼说的在对应 productFlavors 中建同名目录和资源就行。
    PhxNirvana
        11
    PhxNirvana  
       Dec 27, 2019
    自动化打包
    Minson
        12
    Minson  
       Dec 27, 2019
    一楼正解
    allenframe
        13
    allenframe  
       Dec 27, 2019
    3、4 年前就有的 Android 打包方案,属于基础;还不涉及到复杂的需求,都不需要用到脚本
    diy886
        14
    diy886  
    OP
       Dec 28, 2019 via Android
    @stephonhu 老哥你这方法正是我想要的,谢谢了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   890 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 48ms · UTC 22:07 · PVG 06:07 · LAX 15:07 · JFK 18:07
    ♥ Do have faith in what you're doing.