Android studio使用gradle動態構建APP(不同的包,不同的icon、label),androidgradle

來源:互聯網
上載者:User

Android studio使用gradle動態構建APP(不同的包,不同的icon、label),androidgradle

 

  最近有個需求,需要做兩個功能相似的APP,大部分代碼是一樣的,只是介面不一樣,以前要維護兩套代碼,比較麻煩,最近在網上找資料,發現可以用gradle使用同一套代碼構建兩個APP。下面介紹使用方法:

 

  首先要構建兩個APP需要有兩個APP表徵圖、APP名字和AndroidManifest.xml。AndroidManifest放置目錄如下:

 

 

gradle構建需要用的設定檔build.gradle。 要使用兩個AndroidManifest需要在build.gradle檔案中配置sourceSets

 1 sourceSets 2             { 3  4                 app1 5                         { 6                             manifest.srcFile 'src/main/manifest/AndroidManifest1.xml' 7                         } 8                 app2 9                         {10                             manifest.srcFile "src/main/manifest/AndroidManifest2.xml"11                         }12             }

 

同時需要修改AndroidManifest添加xmlns:tools和tools:replace如下:

 1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     package="com.Example.app1"> 4  5 <application 6         android:allowBackup="true" 7         android:icon="@drawable/ic_launcher" 8         android:label="@string/app_name" 9         android:theme="@style/AppTheme"10         tools:replace="android:icon,android:label">

android:icon,android:label表示需要使用不同的icon和label。

 

要使用兩個不同的包名,需要在build.gradle檔案中配置productFlavors

 1  productFlavors{ 2  3  4  5  6         app1 7                 { 8                     applicationId "com.Example.app1" 9                     versionCode 3710                     versionName "2.0.0"11                     manifestPlaceholders = [APPNAME: "app1"]12                 }13         app214                 {15                     applicationId "com.Example.app2"16                     versionCode 517                     versionName "1.0.4"18                     manifestPlaceholders = [APPNAME: "app2"]19                 }20 21     }

productFlavors中配置了不同的包名和版本資訊以及變數APPNAME。APPNAME的值可以用在AndroidManifest中:

<meta-data        android:name="APPNAME"        android:value="${APPNAME}" />

完整的build.gradle如下:

 1 apply plugin: 'com.android.application' 2  3 android { 4  5     compileSdkVersion 22 6     buildToolsVersion '23.0.2' 7  8     defaultConfig { 9         minSdkVersion 1910         targetSdkVersion 2211 12     }13     buildTypes {14         release {15             minifyEnabled true16             shrinkResources true17             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'18         }19         debug20                 {21                     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'22                 }23     }24 25 26     packagingOptions {27         exclude 'META-INF/DEPENDENCIES.txt'28         exclude 'META-INF/LICENSE.txt'29         exclude 'META-INF/NOTICE.txt'30         exclude 'META-INF/NOTICE'31         exclude 'META-INF/LICENSE'32         exclude 'META-INF/DEPENDENCIES'33         exclude 'META-INF/notice.txt'34         exclude 'META-INF/license.txt'35         exclude 'META-INF/dependencies.txt'36         exclude 'META-INF/LGPL2.1'37     }38     sourceSets39             {40 41                 app142                         {43                             manifest.srcFile 'src/main/manifest/AndroidManifest1.xml'44                         }45                 app246                         {47                             manifest.srcFile "src/main/manifest/AndroidManifest2.xml"48                         }49             }50 51 52     productFlavors{53 54 55 56 57         app158                 {59                     applicationId "com.Example.app1"60                     versionCode 3761                     versionName "2.0.0"62                     manifestPlaceholders = [APPNAME: "app1"]63                 }64         app265                 {66                     applicationId "com.Example.app2"67                     versionCode 568                     versionName "1.0.4"69                     manifestPlaceholders = [APPNAME: "app2"]70                 }71 72     }73 74 }75 76 allprojects {77     repositories {78         maven { url "https://jitpack.io" }79     }80 }81 82 dependencies {83     compile fileTree(dir: 'libs', include: ['*.jar'])84 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.