久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

不同安卓風格的通用代碼

Common code for different android flavors(不同安卓風格的通用代碼)
本文介紹了不同安卓風格的通用代碼的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在構建 4 種不同風格的 Android 應用.

I am building 4 different flavors of my Android app.

我有一個類 Customization.java,其中 3 個相同,1 個不同.

I have a class Customization.java that is the same for 3 of them and different for 1.

由于我不能將同一個類同時放在主文件夾和風味文件夾中,我現在必須為這 3 個風味維護完全相同的類的 3 個副本.

Since I cannot put the same class both in the main folder and in the flavor folder, I now have to maintain 3 copies of the exact same class for those 3 flavors.

我有什么辦法可以只保留這個類的兩個版本嗎?

Is there any way that I could do with keeping just two versions of this class?

到目前為止我考慮過的事情:

  1. 我查看了風味維度,但發現它們不適用于這種情況.
  2. 在其中一種風格中只保留一個文件并通過我的構建腳本復制它.

我想知道是否有開箱即用的清潔劑.

I am wondering if there is something cleaner out of the box.

推薦答案

我想將 CommonsWare 的評論轉換為答案.然后我將解釋最終的目錄設置應該是什么樣子.我希望這可以幫助人們通過搜索偶然發現這個問題.

I would like to convert CommonsWare's comment to an answer. I'll then explain how the final directory setup should look like. I hope this helps out the people stumbling upon this question through search.

好吧,您可以在風味中覆蓋資源.所以,有一個共同的在 main/res/layout/ 和風味特定的yourFlavorHere/res/layout/.

Well, you can override resources in flavors. So, have the common one in main/res/layout/ and the flavor-specific one in yourFlavorHere/res/layout/.

因此,如果 Customization 活動的布局文件名為 activity_customization.xml,您將在 src/main 下的三種風格之間共享其公共副本/res/layout 目錄并將修改后的布局 xml 放置在其相應的源集目錄 src/flavorFour/res/layout 下,例如 flavorFour.

So, if the Customization activity's layout file is called activity_customization.xml, you'll leave its common copy shared among the three flavors under src/main/res/layout directory and place the modified layout xml to be used by, say flavorFour, under its corresponding source set directory src/flavorFour/res/layout.

其工作方式是,由于風味一到三(與風味四不同)沒有提供自己的 activity_customization.xml 版本,它們將繼承來自 的版本main 源碼集.

The way this works is that since flavor one to three (unlike flavor four) haven't provided their own versions of activity_customization.xml, they'll inherit the one coming from the main source set.

是活動 Java 類變得棘手.另一種可能性也就是使用相同的活動實現來配置風味從兩個源目錄中提取:一個特定于風味的目錄和一個與通用類實現通用的一種.

It's the activity Java class that gets tricky. Another possibility for that is to configure the flavors with the same activity implementation to pull from two source directories: a flavor-specific one and a common one with the common class implementation.

與資源不同,Java 代碼文件不會合并或覆蓋.因此,您不能在 main 下以及任何風味源集中擁有具有相同完全限定類名的 Java 文件.如果這樣做,您將收到重復類錯誤.

Unlike resources, Java code files are not merged or overridden. So, you can't have Java files with the same fully qualified class name under main as well as in any of your flavor source sets. If you do, you'll receive a duplicate class error.

要解決此問題,最簡單的解決方案是將 Customization 活動移出 main 并移到每個風味源集中.這是可行的,因為風味目錄是互斥的(彼此互斥,而不是與 main),因此避免了沖突.

To resolve this issue, the simplest solution is to move Customization activity out of the main and into each flavor source set. This works because the flavor directories are mutually exclusive (with each other, not with main) hence avoiding the conflict.

但這意味著四種風格中的三種具有活動的重復副本 - 維護噩夢 - 只是因為其中一種風格需要對其進行一些更改.為了解決這個問題,我們可以引入另一個源目錄,只保留三種風格之間共享的公共代碼文件.

But this means three out of the four flavors have a duplicate copy of the activity - a maintenance nightmare - just because one of the flavors required some changes to it. To resolve this issue we can introduce another source directory that keeps just the common code files shared between the three flavors.

所以,build.gradle 腳本看起來像

android {
    ...
    productFlavors {
        flavorOne {
            ...
        }
        flavorTwo {
            ...
        }
        flavorThree {
            ...
        }
        flavorFour {
            ...
        }
    }
    sourceSets {
        flavorOne.java.srcDir 'src/common/java'
        flavorTwo.java.srcDir 'src/common/java'
        flavorThree.java.srcDir 'src/common/java'
    }
}

注意使用 java.srcDir(而不是 srcDirs)添加另一個 Java 源目錄到已經存在的默認 src/flavorX/java.

Notice the use of java.srcDir (and not srcDirs) which adds another Java source directory to the already existing default src/flavorX/java.

現在我們需要做的就是將通用的 Customization 活動文件放到 src/common/java 中,以使其可用于一到三種風格.flavorFour 所需的修改版本將位于其自己的源集 src/flavorFour/java 下.

Now all we need to do is to drop the common Customization activity file in src/common/java to make it available to the flavors one to three. The modified version required by flavorFour would go under its own source set at src/flavorFour/java.

所以,最終的項目結構應該是這樣的

So, the final project structure would look something like

+ App // module
|- src
   |- common // shared srcDir
      |- java
       |- path/to/pkg
         |- CustomizationActivity.java // inherited by flavors 1, 2, 3
   + flavorOne
   + flavorTwo
   + flavorThree
   + flavorFour
      |- java
       |- path/to/pkg
         |- CustomizationActivity.java // per-flavor activity class
      |- res
         |- layout
            |- activity_customization.xml // overrides src/main/res/layout
   |- main
      + java
      |- res
         |- layout
            |- activity_customization.xml // inherited by flavors 1, 2, 3

這篇關于不同安卓風格的通用代碼的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

IncompatibleClassChangeError after updating to Android Build Tools 25.1.6 GCM / FCM(更新到 Android Build Tools 25.1.6 GCM/FCM 后出現 IncompatibleClassChangeError)
How to get current flavor in gradle(如何在 gradle 中獲取當前風味)
How to fix quot;unexpected element lt;queriesgt; found in lt;manifestgt;quot; error?(如何修復“意外元素lt;查詢gt;在“清單中找到錯誤?)
Multi flavor app based on multi flavor library in Android Gradle(基于 Android Gradle 中多風味庫的多風味應用)
Android dependency has different version for the compile and runtime(Android 依賴在編譯和運行時有不同的版本)
Transitive dependencies for local aar library(本地 aar 庫的傳遞依賴)
主站蜘蛛池模板: 亚洲三区在线观看 | 日本成人午夜影院 | 999久久久久久久久6666 | 国产精品视频在线播放 | 二区不卡 | 亚洲视频一区在线播放 | 日韩在线小视频 | 国产一二三视频在线观看 | 欧美三区 | 91精品www | 毛片黄片免费看 | 欧美激情久久久久久 | 一区二区久久精品 | 国产精品视频一区二区三区 | 免费看国产一级特黄aaaa大片 | 国产一区二区三区色淫影院 | 国产一区二区三区在线视频 | 龙珠z在线观看 | 夜久久 | 成人精品视频 | 亚洲国产精品久久久久婷婷老年 | 国产一区二区三区www | 视频一区二区在线观看 | 久久精品这里精品 | 日日干综合 | 亚洲色图婷婷 | 国产精品美女在线观看 | 久久久久久成人 | 91传媒在线观看 | 久久久一区二区 | 成人三级av | 成人免费视频网站在线看 | 亚洲综合色网 | 中文字幕综合在线 | 欧美一区永久视频免费观看 | 国产视频一区二区 | 国产精品美女久久久 | 精品国产31久久久久久 | 日韩图区 | 中文字幕91 | 精品国产一区久久 |