lwb
2020-11-19 7d62c45726746fd3a4e9360642e81d7da6d76757
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
plugins {
    id 'com.android.library'
}
def rootPath = rootProject.getRootDir().getAbsolutePath()
def channelPath = "${rootPath}/channel/${CHANNEL_NAME}"
 
def applyGradle(String path) {
    path = "${path}.gradle"
    File file = file(path)
    if (file.exists())
        apply from: path
}
 
applyGradle("${rootPath}/utils")//工具脚本
applyGradle("${channelPath}/build")//渠道脚本
 
Properties localProp = loadProperties('local')//本地配置
Properties channelProp = loadProperties("${channelPath}/config")//渠道所属的配置
 
def outPutPath = localProp.getProperty("OUTPUT_PATH")//工程sdk输出路径
 
//当前渠道所应用的插件名
def plugins = channelProp.getProperty("PLUGINS").split(';')
//插件脚本
plugins.each {
    applyGradle("${rootPath}/plugins/${it}/build")
}
 
//渠道需要覆盖主目录的java文件
def channelJavaFiles = getAllFiles(new ArrayList<File>(), "${channelPath}/java")
 
android {
    compileSdkVersion 29
    buildToolsVersion "30.0.2"
 
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
 
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
    }
 
    flavorDimensions "version"
    productFlavors {
        merger {
            dimension "version"
        }
    }
 
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    lintOptions {
        checkReleaseBuilds false
        abortOnError false
    }
 
    //合并资源
    sourceSets {
        main {
            java {
                //剔除主目录下,渠道重复的类
                channelJavaFiles.each {
                    def tag = "java\\"
                    def str = it.getAbsolutePath()
                    def str1 = str.substring(0, str.indexOf(tag))
                    def str2 = str.substring(str1.length() + tag.length(), str.length())
                    exclude str2
                }
            }
        }
        merger {
            java {
                srcDirs "${channelPath}/java"
            }
            jniLibs.srcDirs "${channelPath}/libs"
            res.srcDirs "${channelPath}/res"
            manifest {
                File file = file("${channelPath}/AndroidManifest.xml")
                if (file.exists())
                    srcFile "${channelPath}/AndroidManifest.xml"
            }
            //合并插件
            plugins.each {
                def pluginPath = "${rootPath}/plugins/${it}"
                java.srcDirs "${pluginPath}/java"
                jniLibs.srcDirs "${pluginPath}libs"
                res.srcDirs "${pluginPath}/res"
                manifest {
                    File file = file("${pluginPath}/AndroidManifest.xml")
                    if (file.exists())
                        srcFile "${pluginPath}/AndroidManifest.xml"
                }
            }
        }
    }
}
 
android.libraryVariants.all { variant ->
    variant.outputs.all {
        outputFileName = "library-${buildType.name}-${CHANNEL_NAME}.aar"
    }
}
 
//拷贝清单文件
task copyDebugManifest(type: Copy) {
    from zipTree("build/outputs/aar/library-debug-${CHANNEL_NAME}.aar")
    include "AndroidManifest.xml"
    into "${outPutPath}\\${CHANNEL_NAME}\\debug"
}
 
task copyManifest(type: Copy) {
    dependsOn 'copyDebugManifest'
    from zipTree("build/outputs/aar/library-release-${CHANNEL_NAME}.aar")
    include "AndroidManifest.xml"
    into "${outPutPath}\\${CHANNEL_NAME}\\release"
}
 
task copyDebugSdk(type: Copy) {
    from "build/outputs/aar/"
    include "library-debug-${CHANNEL_NAME}.aar"
    into "${outPutPath}\\${CHANNEL_NAME}\\debug\\libs\\"
}
 
//执行打包任务以及拷贝到外部
task assembleAndCopySdk(type: Copy) {
    delete "${outPutPath}\\${CHANNEL_NAME}"
 
    dependsOn 'assembleMerger'
//    dependsOn copyManifest
    dependsOn copyDebugSdk
 
    from "build/outputs/aar/"
    include "library-release-${CHANNEL_NAME}.aar"
    into "${outPutPath}\\${CHANNEL_NAME}\\release\\libs\\"
}
 
dependencies {
    testImplementation 'junit:junit:4.13.1'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
 
    implementation 'androidx.appcompat:appcompat:1.2.0'
    implementation 'com.google.android.material:material:1.2.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
    implementation 'androidx.navigation:navigation-fragment:2.3.1'
    implementation 'androidx.navigation:navigation-ui:2.3.1'
    implementation 'com.github.pqpo:Log4a:1.4.2@aar'
 
    implementation fileTree(dir: 'libs', include: ['*.jar'], exclude: ['unity-classes.jar'])
    compileOnly files('libs/unity-classes.jar')
}