2015-01-04 29 views
1

我有1個應用程序,但我想做2個應用程序(2個Android ID)並分發給手機和平板電腦。 爲此(以及其他方面)我正在使用Android Studio的flavors「功能」。Android工作室在清單中使用<compatible-screens>風味

同時我需要使用this來設置每個apk的屏幕大小。根據flavor方法,需要在兩個不同的文件夾中創建兩個相同的項目(這是一個非常簡單的方法,我知道),但是這種情況下,我應該只需在兩個地方複製AndroidManifest並設置<compatible-screens>標籤中的適當內容,或者如何處理這個問題? 我猜這會起作用,但我想知道最佳做法是什麼。如果有人知道或曾嘗試過,請告訴我。

回答

0

沒關係我自己想通了。我把這段代碼build.gradle的應用

//============================ 
// Custom manifest merging: 
//============================ 
android.applicationVariants.all { variant -> 
    variant.processManifest.doLast { 

     def propertyList = variant.productFlavors*.name 
     propertyList.add(variant.buildType.name) 

     def variantProperties = new Properties() 
     propertyList.reverse().each { property -> 
      def propFile = "src/${property}/manifest.properties" 
      try { 
       file(propFile).withReader { reader -> 
        def tmpProps = new Properties() 
        tmpProps.load(reader) 
        variantProperties.putAll(tmpProps) 
       } 
      } catch (e) { 
       println "[${variant.name}] Failed to load ${propFile}" 
      } 
     } 
     println "[${variant.name}] manifest properties: ${variantProperties}" 

     copy { 
      from("${buildDir}/intermediates/manifests/full") { 
       include "${variant.dirName}/AndroidManifest.xml" 
      } 
      into("${buildDir}/filtered_manifests") 
      filter { String line -> line.replaceAll("<compatible-screens(.*)/>", variantProperties.get("compatible-screens", "")) } 
      filter { String line -> line.replaceAll("<supports-screens(.*)/>", variantProperties.get("supports-screens", "")) } 
     } 
    } 
    variant.processResources.manifestFile = file("build/filtered_manifests/${variant.dirName}/AndroidManifest.xml") 
} 

然後,我創建了這些路徑下的兩個文件:src/phone/manifest.propertiessrc/tablet/manifest.properties下一個內容:

compatible-screens=<compatible-screens>\ 
     <screen android:screenSize="small" android:screenDensity="ldpi" />\ 
     <screen android:screenSize="small" android:screenDensity="mdpi" />\ 
     <screen android:screenSize="small" android:screenDensity="hdpi" />\ 
     <screen android:screenSize="small" android:screenDensity="xhdpi" />\ 
     <screen android:screenSize="normal" android:screenDensity="ldpi" />\ 
     <screen android:screenSize="normal" android:screenDensity="mdpi" />\ 
     <screen android:screenSize="normal" android:screenDensity="hdpi" />\ 
     <screen android:screenSize="normal" android:screenDensity="xhdpi" />\ 
     </compatible-screens> 

supports-screens=<supports-screens \ 
    android:smallScreens="false" \ 
    android:normalScreens="false" \ 
    android:largeScreens="true" \ 
    android:xlargeScreens="true" \ 
    android:requiresSmallestWidthDp="600" /> 

而且在AndroidManifest.xml這是unde src/main/我放置了兩個空標籤<supports-screens /><compatible-screens />

就是這樣!

一切都來自此鏈接:https://groups.google.com/forum/#!msg/adt-dev/8igxEyihIlc/gk4b8K6ZxNYJ與構建路徑文件夾的小改變。礦井在${buildDir}/intermediates/manifests/full之下。

3

我可以看到你自己找到了一個解決方案,但它似乎是一個非常奇怪的解決方案,可以追溯到2013年3月,所以讓我介紹一下當前的標準方法。此方法使用Google團隊實施的清單合併。

源 - >主 - > AndroidManifest.xml中

沒有做任何關於它的特殊,不添加相關的東西給它的任何畫面。

源 - >電話 - > AndroidManifest.xml中

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

    <supports-screens 
     android:smallScreens="true" 
     android:normalScreens="true" 
     android:largeScreens="false" 
     android:xlargeScreens="false"/> 

</manifest> 

然後源 - >平板電腦 - > AndroidManifest.xml中

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

    <supports-screens 
     android:smallScreens="false" 
     android:normalScreens="false" 
     android:largeScreens="true" 
     android:xlargeScreens="true"/> 

</manifest> 

上面是完整的文件,只support-screens等等。他們通過構建系統合併到您的main - > ANdroidManifest.xml中。

此外,如果你希望(你所需要的谷歌播放)把不同的版本號爲他們每個人,在build.gradleandroid {部分內包括:

productFlavors { 
     phone { 
      versionCode 200001 
     } 
     tablet { 
      versionCode 300001 
     } 
    } 
+0

我只有一個味道「電話」這對我不起作用。它仍然不會強制平板電腦上的屏幕兼容模式。有任何想法嗎? – shelll