Skip to content

Commit d1009bb

Browse files
authored
feat(android): use common variables for config and dependencies (#2534)
1 parent 2209113 commit d1009bb

File tree

7 files changed

+89
-43
lines changed

7 files changed

+89
-43
lines changed

android-template/app/build.gradle

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 29
4+
compileSdkVersion rootProject.ext.compileSdkVersion
55
defaultConfig {
66
applicationId "com.getcapacitor.app"
7-
minSdkVersion 21
8-
targetSdkVersion 29
7+
minSdkVersion rootProject.ext.minSdkVersion
8+
targetSdkVersion rootProject.ext.targetSdkVersion
99
versionCode 1
1010
versionName "1.0"
1111
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
@@ -19,21 +19,18 @@ android {
1919
}
2020

2121
repositories {
22-
maven {
23-
url "https://dl.bintray.com/ionic-team/capacitor"
24-
}
2522
flatDir{
2623
dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs'
2724
}
2825
}
2926

3027
dependencies {
3128
implementation fileTree(include: ['*.jar'], dir: 'libs')
32-
implementation 'androidx.appcompat:appcompat:1.1.0'
29+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
3330
implementation project(':capacitor-android')
34-
testImplementation 'junit:junit:4.12'
35-
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
36-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
31+
testImplementation "junit:junit:$junitVersion"
32+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
33+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
3734
implementation project(':capacitor-cordova-android-plugins')
3835
}
3936

android-template/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ buildscript {
1515
}
1616
}
1717

18+
apply from: "variables.gradle"
19+
1820
allprojects {
1921
repositories {
2022
google()

android-template/variables.gradle

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
ext {
2+
minSdkVersion = 21
3+
compileSdkVersion = 29
4+
targetSdkVersion = 29
5+
androidxAppCompatVersion = '1.1.0'
6+
androidxCoreVersion = '1.2.0'
7+
androidxMaterialVersion = '1.1.0-rc02'
8+
androidxBrowserVersion = '1.2.0'
9+
androidxLocalbroadcastmanagerVersion = '1.0.0'
10+
firebaseMessagingVersion = '20.1.2'
11+
playServicesLocationVersion = '17.0.0'
12+
junitVersion = '4.12'
13+
androidxJunitVersion = '1.1.1'
14+
androidxEspressoCoreVersion = '3.2.0'
15+
cordovaAndroidVersion = '7.0.0'
16+
}

android/capacitor/build.gradle

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
ext {
2+
androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.1.0'
3+
androidxCoreVersion = project.hasProperty('androidxCoreVersion') ? rootProject.ext.androidxCoreVersion : '1.2.0'
4+
androidxMaterialVersion = project.hasProperty('androidxMaterialVersion') ? rootProject.ext.androidxMaterialVersion : '1.1.0-rc02'
5+
androidxBrowserVersion = project.hasProperty('androidxBrowserVersion') ? rootProject.ext.androidxBrowserVersion : '1.2.0'
6+
androidxLocalbroadcastmanagerVersion = project.hasProperty('androidxLocalbroadcastmanagerVersion') ? rootProject.ext.androidxLocalbroadcastmanagerVersion : '1.0.0'
7+
firebaseMessagingVersion = project.hasProperty('firebaseMessagingVersion') ? rootProject.ext.firebaseMessagingVersion : '20.1.2'
8+
playServicesLocationVersion = project.hasProperty('playServicesLocationVersion') ? rootProject.ext.playServicesLocationVersion : '17.0.0'
9+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.12'
10+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.1'
11+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.2.0'
12+
cordovaAndroidVersion = project.hasProperty('cordovaAndroidVersion') ? rootProject.ext.cordovaAndroidVersion : '7.0.0'
13+
}
14+
15+
116
buildscript {
217
repositories {
318
google()
@@ -15,10 +30,10 @@ apply plugin: 'com.android.library'
1530
apply plugin: 'com.novoda.bintray-release'
1631

1732
android {
18-
compileSdkVersion 29
33+
compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 29
1934
defaultConfig {
20-
minSdkVersion 21
21-
targetSdkVersion 29
35+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21
36+
targetSdkVersion targetSdkVersion = project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29
2237
versionCode 1
2338
versionName "1.0"
2439
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
@@ -46,17 +61,17 @@ repositories {
4661

4762
dependencies {
4863
implementation fileTree(dir: 'libs', include: ['*.jar'])
49-
implementation 'androidx.appcompat:appcompat:1.1.0'
50-
implementation 'androidx.core:core:1.2.0'
51-
implementation 'com.google.android.material:material:1.1.0-rc02'
52-
implementation 'androidx.browser:browser:1.2.0'
53-
implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0'
54-
implementation 'com.google.firebase:firebase-messaging:18.0.0'
55-
implementation 'com.google.android.gms:play-services-location:17.0.0'
56-
testImplementation 'junit:junit:4.12'
57-
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
58-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
59-
implementation 'org.apache.cordova:framework:7.0.0'
64+
implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
65+
implementation "androidx.core:core:$androidxCoreVersion"
66+
implementation "com.google.android.material:material:$androidxMaterialVersion"
67+
implementation "androidx.browser:browser:$androidxBrowserVersion"
68+
implementation "androidx.localbroadcastmanager:localbroadcastmanager:$androidxLocalbroadcastmanagerVersion"
69+
implementation "com.google.firebase:firebase-messaging:$firebaseMessagingVersion"
70+
implementation "com.google.android.gms:play-services-location:$playServicesLocationVersion"
71+
testImplementation "junit:junit:$junitVersion"
72+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
73+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
74+
implementation "org.apache.cordova:framework:$cordovaAndroidVersion"
6075
}
6176

6277
def version = System.getenv("BINTRAY_PKG_VERSION")
@@ -73,4 +88,4 @@ publish {
7388
}
7489
desc = 'Capacitor Android Runtime'
7590
website = 'https://github.com/ionic-team/capacitor'
76-
}
91+
}

capacitor-cordova-android-plugins/build.gradle

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ buildscript {
1111
apply plugin: 'com.android.library'
1212

1313
android {
14-
compileSdkVersion 29
14+
compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 29
1515
defaultConfig {
16-
minSdkVersion 21
17-
targetSdkVersion 29
16+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21
17+
targetSdkVersion targetSdkVersion = project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29
1818
versionCode 1
1919
versionName "1.0"
2020
}
@@ -38,7 +38,7 @@ repositories {
3838

3939
dependencies {
4040
implementation fileTree(dir: 'src/main/libs', include: ['*.jar'])
41-
implementation 'org.apache.cordova:framework:7.0.0'
41+
implementation "org.apache.cordova:framework:$cordovaAndroidVersion"
4242
// SUB-PROJECT DEPENDENCIES START
4343

4444
// SUB-PROJECT DEPENDENCIES END

cli/src/android/update.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Config } from '../config';
22
import { checkPlatformVersions, logFatal, resolveNode, runTask } from '../common';
33
import { getAndroidPlugins } from './common';
44
import { checkAndInstallDependencies, handleCordovaPluginsJS, writeCordovaAndroidManifest } from '../cordova';
5-
import { convertToUnixPath, copySync, readFileAsync, removeSync, writeFileAsync} from '../util/fs';
5+
import { convertToUnixPath, copySync, existsSync, readFileAsync, removeSync, writeFileAsync} from '../util/fs';
66
import { join, relative, resolve } from 'path';
77
import { Plugin, PluginType, getAllElements, getFilePath, getPlatformElement, getPluginPlatform, getPluginType, getPlugins, printPlugins } from '../plugin';
88

@@ -82,9 +82,7 @@ project(':${getGradlePackageName(p.id)}').projectDir = new File('${relativePlugi
8282
prefsArray = prefsArray.concat(getAllElements(p, platform, 'preference'));
8383
});
8484
let frameworkString = frameworksArray.join('\n');
85-
prefsArray.map((preference: any) => {
86-
frameworkString = frameworkString.replace(new RegExp(('$' + preference.$.name).replace('$', '\\$&'), 'g'), preference.$.default);
87-
});
85+
frameworkString = await replaceFrameworkVariables(config, prefsArray, frameworkString);
8886
const dependencyLines = `// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN
8987
9088
android {
@@ -134,17 +132,15 @@ export async function handleCordovaPluginsGradle(config: Config, cordovaPlugins
134132
let frameworkString = frameworksArray.map(f => {
135133
return ` implementation "${f}"`;
136134
}).join('\n');
137-
prefsArray.map((preference: any) => {
138-
frameworkString = frameworkString.replace(new RegExp(('$' + preference.$.name).replace('$', '\\$&'), 'g'), preference.$.default);
139-
});
135+
frameworkString = await replaceFrameworkVariables(config, prefsArray, frameworkString);
140136
let applyString = applyArray.join('\n');
141137
let buildGradle = await readFileAsync(pluginsGradlePath, 'utf8');
142138
buildGradle = buildGradle.replace(/(SUB-PROJECT DEPENDENCIES START)[\s\S]*(\/\/ SUB-PROJECT DEPENDENCIES END)/, '$1\n' + frameworkString.concat('\n') + ' $2');
143139
buildGradle = buildGradle.replace(/(PLUGIN GRADLE EXTENSIONS START)[\s\S]*(\/\/ PLUGIN GRADLE EXTENSIONS END)/, '$1\n' + applyString.concat('\n') + '$2');
144140
await writeFileAsync(pluginsGradlePath, buildGradle);
145141
const cordovaVariables = `// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN
146142
ext {
147-
cdvMinSdkVersion = ${config.android.minVersion}
143+
cdvMinSdkVersion = rootProject.ext.minSdkVersion
148144
// Plugin gradle extensions can append to this to have code run at the end.
149145
cdvPluginPostBuildExtras = []
150146
}`;
@@ -201,3 +197,17 @@ async function getPluginsTask(config: Config) {
201197
return androidPlugins;
202198
});
203199
}
200+
201+
async function replaceFrameworkVariables(config: Config, prefsArray: Array<any>, frameworkString: string) {
202+
const variablesFile = resolve(config.app.rootDir, 'android', 'variables.gradle');
203+
let variablesGradle = '';
204+
if (existsSync(variablesFile)) {
205+
variablesGradle = await readFileAsync(variablesFile, 'utf8');
206+
}
207+
prefsArray.map((preference: any) => {
208+
if (!variablesGradle.includes(preference.$.name)) {
209+
frameworkString = frameworkString.replace(new RegExp(('$' + preference.$.name).replace('$', '\\$&'), 'g'), preference.$.default);
210+
}
211+
});
212+
return frameworkString;
213+
}

plugin-template/android/build.gradle

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
ext {
2+
junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.12'
3+
androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.1'
4+
androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.2.0'
5+
}
6+
17
buildscript {
28
repositories {
39
google()
@@ -11,10 +17,10 @@ buildscript {
1117
apply plugin: 'com.android.library'
1218

1319
android {
14-
compileSdkVersion 29
20+
compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 29
1521
defaultConfig {
16-
minSdkVersion 21
17-
targetSdkVersion 29
22+
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21
23+
targetSdkVersion = project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29
1824
versionCode 1
1925
versionName "1.0"
2026
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
@@ -40,7 +46,7 @@ repositories {
4046
dependencies {
4147
implementation fileTree(dir: 'libs', include: ['*.jar'])
4248
implementation project(':capacitor-android')
43-
testImplementation 'junit:junit:4.12'
44-
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
45-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
49+
testImplementation "junit:junit:$junitVersion"
50+
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
51+
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
4652
}

0 commit comments

Comments
 (0)