Android studio uses gradle to dynamically build apps (different packages, different icons and labels) and androidgradle

Source: Internet
Author: User
Tags build gradle

Android studio uses gradle to dynamically build apps (different packages, different icons and labels) and androidgradle

 

Recently, I have a requirement to create two apps with similar functions. Most of the Code is the same, but the interface is different. In the past, I had to maintain two sets of code, which is troublesome. I recently asked for information on the Internet, it is found that two apps can be built using the same set of code in gradle. The following describes how to use it:

 

To build two apps, you must have two APP icons, the APP name, And AndroidManifest. xml. AndroidManifest directory:

 

 

Use the configuration file build. gradle to build gradle. To use two AndroidManifest files, configure sourceSets In the build. gradle file.

 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             }

 

You also need to modify AndroidManifest to add xmlns: tools and tools: replace as follows:

 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 indicates that different icons and labels need to be used.

 

To use two different package names, you must configure productFlavors In the build. gradle file.

 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     }

Different Package name and version information and variable APPNAME are configured in productFlavors. APPNAME values can be used in AndroidManifest:

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

The complete build. gradle is as follows:

 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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.