hch
2025-06-05 9aaa448473c6e4ec95546817958b72cbdc2b5167
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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")//渠道脚本
applyGradle("${channelPath}/deps")//渠道的远程依赖脚本
 
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")
    applyGradle("${rootPath}/plugins/${it}/deps")
}
 
//渠道需要覆盖主目录的java文件
def channelJavaFiles = getAllFiles(new ArrayList<File>(), "${channelPath}/java")
 
android {
    compileSdkVersion 34
    buildToolsVersion "34.0.0"
 
    defaultConfig {
        minSdkVersion 24
        targetSdkVersion 34
        versionCode 1
        versionName "1.0"
 
//        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        consumerProguardFiles "consumer-rules.pro"
        multiDexEnabled true
    }
 
 
    flavorDimensions "version"
    productFlavors {
        merger {
            dimension "version"
        }
    }
 
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    packagingOptions {exclude 'META-INF/rxjava.properties'}
    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"
                //清单只能合并一个,所有插件的清单需要手动复制到对应的渠道里
            }
        }
    }
}
 
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"
}
 
//拷贝debug aar
task copyDebugSdk(type: Copy) {
    from "build/outputs/aar/"
    include "library-debug-${CHANNEL_NAME}.aar"
    into "${outPutPath}\\${CHANNEL_NAME}\\debug\\libs\\"
}
 
task copyReleaseSdk(type: Copy) {
    from "build/outputs/aar/"
    include "library-release-${CHANNEL_NAME}.aar"
    into "${outPutPath}\\${CHANNEL_NAME}\\release\\libs\\"
}
 
task deleteSdk() {
    delete "${outPutPath}\\${CHANNEL_NAME}\\debug"
    delete "${outPutPath}\\${CHANNEL_NAME}\\release"
}
 
//执行打包任务以及拷贝到外部
task assembleAndCopySdk() {
    dependsOn 'assemble'
    dependsOn deleteSdk
    dependsOn copyDebugSdk
    dependsOn copyReleaseSdk
 
    tasks.findByName('deleteSdk').mustRunAfter 'assembleMerger'
    tasks.findByName('copyDebugSdk').mustRunAfter 'deleteSdk'
    tasks.findByName('copyReleaseSdk').mustRunAfter 'deleteSdk'
 
    //导出所有外部引用
    String content = ""
    def deps = file("${channelPath}\\deps.gradle")
    deps.eachLine { line ->
        if (line != null)
            content += line + "\n"
    }
    plugins.each {
        deps = file("${rootPath}/plugins/${it}/deps.gradle")
        if(deps.exists()){
            deps.eachLine { line ->
                if (line != null)
                    content += line + "\n"
            }
        }
    }
    def dir = new File("${outPutPath}\\${CHANNEL_NAME}")
    def libraries = new File("${outPutPath}\\${CHANNEL_NAME}\\deps.gradle")
    dir.mkdir()
    if (libraries.exists())
        libraries.delete()
    if (!libraries.exists())
        libraries.createNewFile()
    libraries.text = content
}
 
dependencies {
 
    implementation fileTree(dir: "../channel/${CHANNEL_NAME}/libs/", include: ['*.jar'])
    plugins.each {
        implementation fileTree(dir: "${rootPath}/plugins/${it}/libs/", include: ['*.jar'])
    }
    implementation fileTree(dir: 'libs', include: ['*.jar'], exclude: ['unity-classes.jar'])
    compileOnly files('libs/unity-classes.jar')
 
//以下为必需依赖的库
    implementation fileTree(include: ['*.jar'], dir: 'libs/aldsdk_libs150')
    implementation(name: 'common_feature-release', ext: 'aar')
    implementation(name: 'sdk_common_code-release', ext: 'aar')
    implementation(name: 'sdk_common_res_base-release', ext: 'aar')
    implementation(name: 'sdk_common_res-release', ext: 'aar')
 
    //base  theme
    implementation 'androidx.appcompat:appcompat:1.2.0'
    //ald ui用
    implementation 'androidx.cardview:cardview:1.0.0'
    //webview控件
    implementation('androidx.browser:browser:1.5.0')
 
    //谷歌能力
    implementation 'com.google.android.gms:play-services-auth:20.1.0'  //google服务
    implementation("com.android.billingclient:billing:7.0.0")  // google内购
    implementation 'androidx.percentlayout:percentlayout:1.0.0'  //google提供的支持库
    implementation 'com.google.android.play:review:2.0.1'  //google评价
 
    //facebook能力
    implementation 'com.facebook.android:facebook-login:latest.release'
    implementation 'com.facebook.android:facebook-share:latest.release'
 
}