一套源码编译多个APP

用Android Studio 开发软件,一定要熟悉Gradle的使用。一套源码编译多个APP,就是通过Gradle实现的。下面就先简单介绍一下怎么使用Gradle编译多个APP,以后会逐步深入解读。

要求: APP 名字   APP 包名   APP 图标   界面颜色  不同

1.新建两个目录

在app -> src -> 目录右键 -> New -> Directory 新建两个目录  把各个app
需要的资源文件,分开拷贝,需要什么界面效果,在各自的资源文件配置就好。

2.我们在 app的gradle下 添加如下代码   

APP唯一标识    版本号  APP名字(使用占位符,在清单文件替换的)
android {
    compileSdkVersion 26
    buildToolsVersion "25.0.3"
    defaultConfig {
        applicationId "com.example.wh.myapplication"
        minSdkVersion 21
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
//================================以下代码===========================================================
    productFlavors {
        WengCang {
            applicationId "com.wengcang.android"
            versionCode 1
            versionName "1.0"
            manifestPlaceholders = [ appName : "文藏" ]
        }

        TouTiao {
            applicationId "com.toutiao.android"
            versionCode 1
            versionName "1.0"
            manifestPlaceholders = [ appName : "头条" ]
        }
    }
//=================================以上代码====================================================================
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}

3.清单文件主要是APP名字的设置。如果需要集成 推送,支付, 第三方登录等,每个APP的配置都是唯一的,这时可以用占位符解决。

例如APP名字。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.wh.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="${appName}"//=================APP名字用占位符解决。从gradle引用对应的名字==================================
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

4.上面已经一步步配置好了之后,会有下图这些东西,选择完成之后再点击 Run 安装

自此简单的一套代码管理多个APP的使用就轻松搞定,以后会详细怎么高效的使用一套代码管理多个APP,

比如:所有APP都用一套用户平台的代码,资源API有各自的特点,各个客户端接口相互挂接。




  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值