-
Notifications
You must be signed in to change notification settings - Fork 28
/
build.gradle
500 lines (444 loc) · 12.2 KB
/
build.gradle
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
/*
* libretroshare Android AAR library gradle builder
*
* Copyright (C) 2022 Gioacchino Mazzurco <[email protected]>
* Copyright (C) 2022 Asociación Civil Altermundi <[email protected]>
*
* SPDX-License-Identifier: CC0-1.0
*/
/*
* Set minumum Android API level supported by this build passing
* -PANDROID_MIN_API_LEVEL=16 (change 16 to what you need) on the gradle command
* line.
*
* LIBRETROSHARE_SOURCE_VERSION Set source version ex:
* -PLIBRETROSHARE_SOURCE_VERSION="$(git describe --always)"
*
* Optionally set JNI_NATIVE_LIBS_ARCHS to a space separated target CPU
* architectures list named as seen in AAR native libraries directories passing
* -PJNI_NATIVE_LIBS_ARCHS="arm64-v8a armeabi-v7a" defaults to all architectures
* supported by current script at current Android minimum API
*
* Optionally set NATIVE_TOOLCHAINS_DIR for easier reuse of toolchains which
* take a lot to build and doesn't change often
* -PNATIVE_TOOLCHAINS_DIR="/native/toolchains/path"
*
* Optionally set environement variables defined in prepare-toolchain-clang.sh
* to further customize the build
* export RS_EXTRA_CMAKE_OPTS="-DRS_DEVELOPMENT_BUILD=ON"
*/
buildscript
{
repositories
{
// The order in which you list these repositories matter.
google()
mavenCentral()
}
dependencies
{
classpath 'com.android.tools.build:gradle:7.1.+'
}
}
allprojects
{
repositories
{
// The order in which you list these repositories matter.
google()
mavenCentral()
}
}
apply plugin: 'com.android.library'
apply plugin: 'maven-publish'
ext.getAndroidMinApiLevel =
{ ->
if(project.hasProperty('ANDROID_MIN_API_LEVEL'))
{
return ANDROID_MIN_API_LEVEL.toInteger();
}
else
{
return 16
}
}
ext.getJniNativeLibsArchs =
{ ->
if(project.hasProperty('JNI_NATIVE_LIBS_ARCHS'))
{
return JNI_NATIVE_LIBS_ARCHS.split(' ') as List
}
else
{
if(getAndroidMinApiLevel() <= 16)
{
return ['armeabi-v7a']
}
else
{
return ['arm64-v8a', 'armeabi-v7a']
}
}
}
ext.getLibretroshareSourceVersion =
{ ->
if(project.hasProperty('LIBRETROSHARE_SOURCE_VERSION'))
{
return LIBRETROSHARE_SOURCE_VERSION
}
else
{
return 'unspecified'
}
}
ext.getNativeToolchainsDir =
{ ->
if(project.hasProperty('NATIVE_TOOLCHAINS_DIR'))
{
return NATIVE_TOOLCHAINS_DIR
}
else
{
return "${buildDir}/native_toolchains/"
}
}
ext.getRsExtraCmakeOpts =
{ ->
if(project.hasProperty('RS_EXTRA_CMAKE_OPTS'))
{
return RS_EXTRA_CMAKE_OPTS
}
else
{
return ""
}
}
ext.getjniLibsDir =
{
pBuildType ->
return "${buildDir}/native_libs/${pBuildType.toLowerCase()}"
}
ext.getAssetsDir =
{ ->
return "${buildDir}/android-assets/"
}
ext.getArtifactBaseName =
{ ->
return project.name + "-MinApiLevel" + getAndroidMinApiLevel()
}
ext.getRsId =
{ ->
return "org.retroshare.service"
}
ext.copyChecked =
{
sourceFile,
destDir ->
assert file(sourceFile).exists()
assert file(destDir).exists()
copy
{
from sourceFile
into destDir
}
assert file("$destDir/${file(sourceFile).getName()}").exists()
}
ext.stripLib =
{
toolchainPath,
libDir,
libFile,
stripOptions = "" ->
def stripCmd = "$toolchainPath/bin/llvm-strip"
assert file(stripCmd).exists()
assert file("${libDir}/${libFile}").exists()
exec
{
workingDir libDir
commandLine stripCmd, stripOptions, libFile
}
}
ext.buildLibretroshareNativeLib =
{
pApiLevel, /* Android API level */
pAbi, /* Arch name as seen in AAR native libraries directories */
pNdkPath, /* Android NDK path */
pBuildType, /* Debug, Release... */
pReuseToolchain = true /* If true reuse previously built toochain */ ->
/* Convert pAbi into corresponding prepare toolchain ANDROID_NDK_ARCH */
def toolchainArch = "unsupported"
def libcxxsharedTriple = "unsupported"
switch(pAbi)
{
case "armeabi-v7a":
toolchainArch = "arm";
libcxxsharedTriple = "arm-linux-androideabi";
break;
case "arm64-v8a":
toolchainArch = "arm64";
libcxxsharedTriple = "aarch64-linux-android";
break;
default:
throw new GradleException(
"buildLibretroshareNativeLib unsupported pAbi: $pAbi" );
break;
}
def toolchainsWorkdir = getNativeToolchainsDir()
mkdir toolchainsWorkdir
def currToolchainPath = "$toolchainsWorkdir/$pApiLevel-$toolchainArch-${pBuildType.toLowerCase()}/"
def toolChainScriptPath = "${projectDir}/misc/Android/prepare-toolchain-clang.sh"
if(!pReuseToolchain || !file(currToolchainPath).exists())
{
exec
{
workingDir toolchainsWorkdir
environment "ANDROID_NDK_PATH", pNdkPath
environment "NATIVE_LIBS_TOOLCHAIN_PATH", currToolchainPath
environment "ANDROID_PLATFORM_VER", pApiLevel
environment "ANDROID_NDK_ARCH", toolchainArch
environment "RS_EXTRA_CMAKE_OPTS", getRsExtraCmakeOpts()
environment "TOOLCHAIN_BUILD_TYPE", pBuildType
commandLine toolChainScriptPath
}
}
else
{
exec
{
workingDir toolchainsWorkdir
environment "ANDROID_NDK_PATH", pNdkPath
environment "NATIVE_LIBS_TOOLCHAIN_PATH", currToolchainPath
environment "ANDROID_PLATFORM_VER", pApiLevel
environment "ANDROID_NDK_ARCH", toolchainArch
environment "RS_EXTRA_CMAKE_OPTS", getRsExtraCmakeOpts()
environment "TOOLCHAIN_BUILD_TYPE", pBuildType
commandLine toolChainScriptPath, 'build_libretroshare'
}
}
def currJniLibsDir = getjniLibsDir(pBuildType)
mkdir currJniLibsDir
def currAbiLibDir = "$currJniLibsDir/$pAbi/"
mkdir currAbiLibDir
copyChecked(
"${currToolchainPath}/sysroot/usr/lib/libretroshare.so",
currAbiLibDir )
copyChecked(
"${currToolchainPath}/sysroot/usr/lib/${libcxxsharedTriple}/libc++_shared.so",
currAbiLibDir )
copyChecked(
"${currToolchainPath}/sysroot/usr/lib/${libcxxsharedTriple}/${pApiLevel}/liblog.so",
currAbiLibDir )
/* Work around Android gradle stripping bug read more information near
* android.buildTypes.debug packagingOptions.jniLibs.keepDebugSymbols
* section of this file.
* First save a release debuggable copy, then strip release */
if(pBuildType.equalsIgnoreCase("Release"))
{
copy
{
from currJniLibsDir
into getjniLibsDir("releaseWithDebuggingSymbols")
}
stripLib(currToolchainPath, currAbiLibDir, "libretroshare.so", "--strip-unneeded")
stripLib(currToolchainPath, currAbiLibDir, "libc++_shared.so", "--strip-debug")
stripLib(currToolchainPath, currAbiLibDir, "liblog.so", "--strip-debug")
}
def bdbootAssetDir = "${getAssetsDir()}/values"
mkdir bdbootAssetDir
copyChecked(
"${currToolchainPath}/sysroot/usr/share/retroshare/bdboot.txt",
bdbootAssetDir )
}
android
{
// see https://stackoverflow.com/questions/27301867/what-is-compilesdkversion
compileSdkVersion 21
ndkVersion "21.0.6113669"
defaultConfig
{
minSdkVersion getAndroidMinApiLevel()
targetSdkVersion 28
/* Prevent proguard run at applications build time, to alter
* libretroshare Java classes that are used by libretroshare C++ code */
consumerProguardFiles 'misc/Android/proguard-keep-libretroshare-java-classes.pro'
}
setProperty("archivesBaseName", getArtifactBaseName())
buildTypes
{
/* To avoid Android Gradle plugin buggy behaviours with native libraries
* debugging symbols, we disable stripping completely on it's side and
* deal with them completely on our own. */
debug
{
debuggable true
jniDebuggable true
/* The following line was added as an attempt to avoid stripping of
* debugging symbols, for debug build type
* https://developer.android.com/reference/tools/gradle-api/7.3/com/android/build/api/dsl/PackagingOptions#doNotStrip%28kotlin.String%29=
* https://developer.android.com/reference/tools/gradle-api/7.3/com/android/build/api/dsl/JniLibsPackagingOptions
* but it ends up affecting all other build types too.
* So enabling this without other countermeasures bloated release
* library with debugging symbols.
* Even more strange is that this is an old known bug at Google
* https://issuetracker.google.com/issues/155215248
* https://stackoverflow.com/questions/52972371/set-donotstrip-packagingoptions-to-a-specific-buildtype
* but nothing substantial has been done to fix this even on newer
* versions of Android Gradle plugin.
* To work around this bug the release .so libraries must be
* stripped in advance in buildLibretroshareNativeLib
*/
packagingOptions.jniLibs.keepDebugSymbols += "**/*.so"
}
release
{
/* The following was added as an attempt to ship separated debugging
* symbols in release mode, but had no effect last time I tested
* 2022/04/06
* https://developer.android.com/studio/build/shrink-code#android_gradle_plugin_version_41_or_later
*/
//ndk.debugSymbolLevel 'FULL'
}
releaseWithDebuggingSymbols
{
/* Distribute the library compiled with release optimization plus
* debugging symbols, this is not intended for direct usage
* (altought possible), but to be able to symbolicate reports
* from stripped releases build. To achieve this the release build
* save a copy of libraries before stripping, so they are product of
* same build ( => same code addressess ) but keeping the symbols
* tables sections */
}
}
buildTypes.all
{
buildTypeObj ->
if(buildTypeObj.name == "releaseWithDebuggingSymbols")
{
/* To be useful to symbolicate reports from stripped releases,
* release with debug symbols must be generated from the very same
* release binaries just before stripping.
* Make sure to not register extra build tasks for it. */
return;
}
def buildTypeName = buildTypeObj.name.capitalize()
def preTaskName = "pre${buildTypeName}Build"
def buildNativeTaskName = "build${buildTypeName}RetroshareNativeLibs"
tasks.register(buildNativeTaskName)
{
doLast
{
delete getjniLibsDir(buildTypeName)
if(buildTypeName == "Release")
{
/* To be useful to symbolicate reports from stripped
* releases, release with debug symbols must be generated
* from the very same release binaries just before
* stripping so make sure to delete those artifacts too */
delete getjniLibsDir("releaseWithDebuggingSymbols")
}
def pNdkPath = android.getNdkDirectory().getAbsolutePath()
getJniNativeLibsArchs().each
{
abi ->
buildLibretroshareNativeLib(
getAndroidMinApiLevel(), abi, pNdkPath, buildTypeName )
}
}
}
/* While `preBuild` seems always defined so one can simply use
* `preBuild.dependsOn prepareRetroshareNativeLibs` it is not the same with
* `preDebugBuild` or any `pre${buildType}Build` that gives this error
* if used directly
* "Could not get unknown property 'preDebugBuild' for root project 'libretroshare' of type org.gradle.api.Project"
* So make them depend on native build once they are added
*/
tasks.whenTaskAdded
{
task ->
if(task.name == preTaskName)
{
task.dependsOn buildNativeTaskName
}
}
}
sourceSets
{
main
{
java.srcDirs = [ 'src/rs_android/' ]
manifest.srcFile 'src/rs_android/AndroidManifest.xml'
assets.srcDirs = [ getAssetsDir() ]
}
debug
{
jniLibs.srcDirs = [ getjniLibsDir(name) ] // name == "debug"
}
release
{
jniLibs.srcDirs = [ getjniLibsDir(name) ]
}
releaseWithDebuggingSymbols
{
jniLibs.srcDirs = [ getjniLibsDir(name) ]
}
}
lintOptions
{
disable 'LongLogTag'
}
publishing
{
multipleVariants
{
allVariants()
}
}
}
afterEvaluate
{
publishing
{
// see https://developer.android.com/reference/tools/gradle-api/7.1/com/android/build/api/dsl/LibraryPublishing
publications
{
debug(MavenPublication)
{
groupId = getRsId()
artifactId "${getArtifactBaseName()}-$name" // name == "debug"
version getLibretroshareSourceVersion()
artifact bundleDebugAar
}
release(MavenPublication)
{
groupId = getRsId()
artifactId "${getArtifactBaseName()}-$name"
version getLibretroshareSourceVersion()
artifact bundleReleaseAar
}
releaseWithDebuggingSymbols(MavenPublication)
{
groupId = getRsId()
artifactId "${getArtifactBaseName()}-$name"
version getLibretroshareSourceVersion()
artifact bundleReleaseWithDebuggingSymbolsAar
}
}
repositories
{
maven
{
url = uri("https://gitlab.com/api/v4/projects/${System.getenv('CI_PROJECT_ID')}/packages/maven")
name = "Gitlab"
credentials(HttpHeaderCredentials)
{
name = "Job-Token"
value = System.getenv("CI_JOB_TOKEN")
}
authentication
{
header(HttpHeaderAuthentication)
}
}
}
}
}