最近有个需求,需要做两个功能相似的APP,大部分代码是一样的,只是界面不一样,以前要维护两套代码,比较麻烦,最近在网上找资料,发现可以用gradle使用同一套代码构建两个APP。下面介绍使用方法:

首先要构建两个APP需要有两个APP图标、APP名字和AndroidManifest.xml。AndroidManifest放置目录如下:

Android studio使用gradle动态构建APP(不同的包,不同的icon、label)

gradle构建需要用的配置文件build.gradle。 要使用两个AndroidManifest需要在build.gradle文件中配置sourceSets

sourceSets
             {
 
                 app1
                         {
                             manifest.srcFile 'src/main/manifest/AndroidManifest1.xml'
                         }
                 app2
                         {
                             manifest.srcFile "src/main/manifest/AndroidManifest2.xml"
                         }
             }

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     package="com.Example.app1">
 
 <application
         android:allowBackup="true"
         android:icon="@drawable/ic_launcher"
         android:label="@string/app_name"
         android:theme="@style/AppTheme"
         tools:replace="android:icon,android:label">

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

要使用两个不同的包名,需要在build.gradle文件中配置productFlavors

productFlavors{
 
 
 
 
         app1
                 {
                     applicationId "com.Example.app1"
                     versionCode 37
                     versionName "2.0.0"
                     manifestPlaceholders = [APPNAME: "app1"]
                 }
         app2
                 {
                     applicationId "com.Example.app2"
                     versionCode 5
                     versionName "1.0.4"
                     manifestPlaceholders = [APPNAME: "app2"]
                 }
 
     }

productFlavors中配置了不同的包名和版本信息以及变量APPNAME。APPNAME的值可以用在AndroidManifest中:

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

完整的build.gradle如下:

apply plugin: 'com.android.application'
 
 android {
 
     compileSdkVersion 22
     buildToolsVersion '23.0.2'
 
     defaultConfig {
         minSdkVersion 19
         targetSdkVersion 22
 
     }
     buildTypes {
         release {
             minifyEnabled true
             shrinkResources true
             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
         }
         debug
                 {
                     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
                 }
     }
 
 
     packagingOptions {
         exclude 'META-INF/DEPENDENCIES.txt'
         exclude 'META-INF/LICENSE.txt'
         exclude 'META-INF/NOTICE.txt'
         exclude 'META-INF/NOTICE'
         exclude 'META-INF/LICENSE'
         exclude 'META-INF/DEPENDENCIES'
         exclude 'META-INF/notice.txt'
         exclude 'META-INF/license.txt'
         exclude 'META-INF/dependencies.txt'
         exclude 'META-INF/LGPL2.1'
     }
     sourceSets
             {
 
                 app1
                         {
                             manifest.srcFile 'src/main/manifest/AndroidManifest1.xml'
                         }
                 app2
                         {
                             manifest.srcFile "src/main/manifest/AndroidManifest2.xml"
                         }
             }
 
 
     productFlavors{
 
 
 
 
         app1
                 {
                     applicationId "com.Example.app1"
                     versionCode 37
                     versionName "2.0.0"
                     manifestPlaceholders = [APPNAME: "app1"]
                 }
         app2
                 {
                     applicationId "com.Example.app2"
                     versionCode 5
                     versionName "1.0.4"
                     manifestPlaceholders = [APPNAME: "app2"]
                 }
 
     }
 
 }
 
 allprojects {
     repositories {
         maven { url "https://jitpack.io" }
     }
 }
 
 dependencies {
     compile fileTree(dir: 'libs', include: ['*.jar'])
 }