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


      最近有个需求,需要做两个功能相似的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 37
    10                     versionName "2.0.0"
    11                     manifestPlaceholders = [APPNAME: "app1"]
    12                 }
    13         app2
    14                 {
    15                     applicationId "com.Example.app2"
    16                     versionCode 5
    17                     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 19
    10         targetSdkVersion 22
    11 
    12     }
    13     buildTypes {
    14         release {
    15             minifyEnabled true
    16             shrinkResources true
    17             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    18         }
    19         debug
    20                 {
    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     sourceSets
    39             {
    40 
    41                 app1
    42                         {
    43                             manifest.srcFile 'src/main/manifest/AndroidManifest1.xml'
    44                         }
    45                 app2
    46                         {
    47                             manifest.srcFile "src/main/manifest/AndroidManifest2.xml"
    48                         }
    49             }
    50 
    51 
    52     productFlavors{
    53 
    54 
    55 
    56 
    57         app1
    58                 {
    59                     applicationId "com.Example.app1"
    60                     versionCode 37
    61                     versionName "2.0.0"
    62                     manifestPlaceholders = [APPNAME: "app1"]
    63                 }
    64         app2
    65                 {
    66                     applicationId "com.Example.app2"
    67                     versionCode 5
    68                     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 }
  • 相关阅读:
    Java正则表达式匹配例子
    python实现的json数据以HTTP GET,POST,PUT,DELETE方式页面请求
    pure-Python PDF library
    搭建nginx反向代理用做内网域名转发
    ASCII、Unicode、UTF-8 字符串和编码
    pdftk
    SQL中distinct 和 row_number() over() 的区别及用法
    使用Python进行AES加密和解密
    python中zip()函数的用法
    查找只出现一次的字符和位置
  • 原文地址:https://www.cnblogs.com/l2rf/p/6186833.html
Copyright © 2020-2023  润新知