From 1e96dafb4d26a9e66e8b5fa544172e31ee2fcc47 Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 6 Mar 2020 19:00:42 +0100 Subject: [PATCH 1/2] feat(android): use common variables for ocnfig and dependencies --- android-template/app/build.gradle | 17 +++---- android-template/build.gradle | 2 + android-template/variables.gradle | 16 +++++++ android/capacitor/build.gradle | 45 ++++++++++++------- .../build.gradle | 8 ++-- cli/src/android/update.ts | 28 ++++++++---- plugin-template/android/build.gradle | 18 +++++--- 7 files changed, 91 insertions(+), 43 deletions(-) create mode 100644 android-template/variables.gradle diff --git a/android-template/app/build.gradle b/android-template/app/build.gradle index ccee48b82b..aa6f22a474 100644 --- a/android-template/app/build.gradle +++ b/android-template/app/build.gradle @@ -1,11 +1,11 @@ apply plugin: 'com.android.application' android { - compileSdkVersion 29 + compileSdkVersion rootProject.ext.compileSdkVersion defaultConfig { applicationId "com.getcapacitor.app" - minSdkVersion 21 - targetSdkVersion 29 + minSdkVersion rootProject.ext.minSdkVersion + targetSdkVersion rootProject.ext.targetSdkVersion versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -19,9 +19,6 @@ android { } repositories { - maven { - url "https://dl.bintray.com/ionic-team/capacitor" - } flatDir{ dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs' } @@ -29,11 +26,11 @@ repositories { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'androidx.appcompat:appcompat:1.1.0' + implementation "androidx.appcompat:appcompat:$rootProject.ext.androidxAppCompatVersion" implementation project(':capacitor-android') - testImplementation 'junit:junit:4.12' - androidTestImplementation 'androidx.test.ext:junit:1.1.1' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + testImplementation "junit:junit:$rootProject.ext.junitVersion" + androidTestImplementation "androidx.test.ext:junit:$rootProject.ext.androidxJunitVersion" + androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.ext.androidxEspressoCoreVersion" implementation project(':capacitor-cordova-android-plugins') } diff --git a/android-template/build.gradle b/android-template/build.gradle index 300eb889fa..160481cb88 100644 --- a/android-template/build.gradle +++ b/android-template/build.gradle @@ -15,6 +15,8 @@ buildscript { } } +apply from: "variables.gradle" + allprojects { repositories { google() diff --git a/android-template/variables.gradle b/android-template/variables.gradle new file mode 100644 index 0000000000..acfd26ce5b --- /dev/null +++ b/android-template/variables.gradle @@ -0,0 +1,16 @@ +ext { + minSdkVersion = 21 + compileSdkVersion = 29 + targetSdkVersion = 29 + androidxAppCompatVersion = '1.1.0' + androidxCoreVersion = '1.2.0' + androidxMaterialVersion = '1.1.0-rc02' + androidxBrowserVersion = '1.2.0' + androidxLocalbroadcastmanagerVersion = '1.0.0' + firebaseMessagingVersion = '20.1.2' + playServicesLocationVersion = '17.0.0' + junitVersion = '4.12' + androidxJunitVersion = '1.1.1' + androidxEspressoCoreVersion = '3.2.0' + cordovaAndroidVersion = '7.0.0' +} \ No newline at end of file diff --git a/android/capacitor/build.gradle b/android/capacitor/build.gradle index be62e7cb9d..73f02fbd4d 100644 --- a/android/capacitor/build.gradle +++ b/android/capacitor/build.gradle @@ -1,3 +1,18 @@ +ext { + androidxAppCompatVersion = project.hasProperty('androidxAppCompatVersion') ? rootProject.ext.androidxAppCompatVersion : '1.1.0' + androidxCoreVersion = project.hasProperty('androidxCoreVersion') ? rootProject.ext.androidxCoreVersion : '1.2.0' + androidxMaterialVersion = project.hasProperty('androidxMaterialVersion') ? rootProject.ext.androidxMaterialVersion : '1.1.0-rc02' + androidxBrowserVersion = project.hasProperty('androidxBrowserVersion') ? rootProject.ext.androidxBrowserVersion : '1.2.0' + androidxLocalbroadcastmanagerVersion = project.hasProperty('androidxLocalbroadcastmanagerVersion') ? rootProject.ext.androidxLocalbroadcastmanagerVersion : '1.0.0' + firebaseMessagingVersion = project.hasProperty('firebaseMessagingVersion') ? rootProject.ext.firebaseMessagingVersion : '20.1.2' + playServicesLocationVersion = project.hasProperty('playServicesLocationVersion') ? rootProject.ext.playServicesLocationVersion : '17.0.0' + junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.12' + androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.1' + androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.2.0' + cordovaAndroidVersion = project.hasProperty('cordovaAndroidVersion') ? rootProject.ext.cordovaAndroidVersion : '7.0.0' +} + + buildscript { repositories { google() @@ -15,10 +30,10 @@ apply plugin: 'com.android.library' apply plugin: 'com.novoda.bintray-release' android { - compileSdkVersion 29 + compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 29 defaultConfig { - minSdkVersion 21 - targetSdkVersion 29 + minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21 + targetSdkVersion targetSdkVersion = project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -46,17 +61,17 @@ repositories { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.core:core:1.2.0' - implementation 'com.google.android.material:material:1.1.0-rc02' - implementation 'androidx.browser:browser:1.2.0' - implementation 'androidx.localbroadcastmanager:localbroadcastmanager:1.0.0' - implementation 'com.google.firebase:firebase-messaging:18.0.0' - implementation 'com.google.android.gms:play-services-location:17.0.0' - testImplementation 'junit:junit:4.12' - androidTestImplementation 'androidx.test.ext:junit:1.1.1' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' - implementation 'org.apache.cordova:framework:7.0.0' + implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion" + implementation "androidx.core:core:$androidxCoreVersion" + implementation "com.google.android.material:material:$androidxMaterialVersion" + implementation "androidx.browser:browser:$androidxBrowserVersion" + implementation "androidx.localbroadcastmanager:localbroadcastmanager:$androidxLocalbroadcastmanagerVersion" + implementation "com.google.firebase:firebase-messaging:$firebaseMessagingVersion" + implementation "com.google.android.gms:play-services-location:$playServicesLocationVersion" + testImplementation "junit:junit:$junitVersion" + androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion" + androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion" + implementation "org.apache.cordova:framework:$cordovaAndroidVersion" } def version = System.getenv("BINTRAY_PKG_VERSION") @@ -73,4 +88,4 @@ publish { } desc = 'Capacitor Android Runtime' website = 'https://github.com/ionic-team/capacitor' -} +} \ No newline at end of file diff --git a/capacitor-cordova-android-plugins/build.gradle b/capacitor-cordova-android-plugins/build.gradle index b249603b47..f238a03aac 100644 --- a/capacitor-cordova-android-plugins/build.gradle +++ b/capacitor-cordova-android-plugins/build.gradle @@ -11,10 +11,10 @@ buildscript { apply plugin: 'com.android.library' android { - compileSdkVersion 29 + compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 29 defaultConfig { - minSdkVersion 21 - targetSdkVersion 29 + minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21 + targetSdkVersion targetSdkVersion = project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29 versionCode 1 versionName "1.0" } @@ -38,7 +38,7 @@ repositories { dependencies { implementation fileTree(dir: 'src/main/libs', include: ['*.jar']) - implementation 'org.apache.cordova:framework:7.0.0' + implementation "org.apache.cordova:framework:$rootProject.ext.cordovaAndroidVersion" // SUB-PROJECT DEPENDENCIES START // SUB-PROJECT DEPENDENCIES END diff --git a/cli/src/android/update.ts b/cli/src/android/update.ts index c7cec990e0..81e5df1843 100644 --- a/cli/src/android/update.ts +++ b/cli/src/android/update.ts @@ -2,7 +2,7 @@ import { Config } from '../config'; import { checkPlatformVersions, logFatal, resolveNode, runTask } from '../common'; import { getAndroidPlugins } from './common'; import { checkAndInstallDependencies, handleCordovaPluginsJS, writeCordovaAndroidManifest } from '../cordova'; -import { convertToUnixPath, copySync, readFileAsync, removeSync, writeFileAsync} from '../util/fs'; +import { convertToUnixPath, copySync, existsSync, readFileAsync, removeSync, writeFileAsync} from '../util/fs'; import { join, relative, resolve } from 'path'; import { Plugin, PluginType, getAllElements, getFilePath, getPlatformElement, getPluginPlatform, getPluginType, getPlugins, printPlugins } from '../plugin'; @@ -82,9 +82,7 @@ project(':${getGradlePackageName(p.id)}').projectDir = new File('${relativePlugi prefsArray = prefsArray.concat(getAllElements(p, platform, 'preference')); }); let frameworkString = frameworksArray.join('\n'); - prefsArray.map((preference: any) => { - frameworkString = frameworkString.replace(new RegExp(('$' + preference.$.name).replace('$', '\\$&'), 'g'), preference.$.default); - }); + frameworkString = await replaceFrameworkVariables(config, prefsArray, frameworkString); const dependencyLines = `// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN android { @@ -134,9 +132,7 @@ export async function handleCordovaPluginsGradle(config: Config, cordovaPlugins let frameworkString = frameworksArray.map(f => { return ` implementation "${f}"`; }).join('\n'); - prefsArray.map((preference: any) => { - frameworkString = frameworkString.replace(new RegExp(('$' + preference.$.name).replace('$', '\\$&'), 'g'), preference.$.default); - }); + frameworkString = await replaceFrameworkVariables(config, prefsArray, frameworkString); let applyString = applyArray.join('\n'); let buildGradle = await readFileAsync(pluginsGradlePath, 'utf8'); buildGradle = buildGradle.replace(/(SUB-PROJECT DEPENDENCIES START)[\s\S]*(\/\/ SUB-PROJECT DEPENDENCIES END)/, '$1\n' + frameworkString.concat('\n') + ' $2'); @@ -144,7 +140,7 @@ export async function handleCordovaPluginsGradle(config: Config, cordovaPlugins await writeFileAsync(pluginsGradlePath, buildGradle); const cordovaVariables = `// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN ext { - cdvMinSdkVersion = ${config.android.minVersion} + cdvMinSdkVersion = rootProject.ext.minSdkVersion // Plugin gradle extensions can append to this to have code run at the end. cdvPluginPostBuildExtras = [] }`; @@ -201,3 +197,19 @@ async function getPluginsTask(config: Config) { return androidPlugins; }); } + +async function replaceFrameworkVariables(config: Config, prefsArray: Array, frameworkString: string) { + const variablesFile = resolve(config.app.rootDir, 'android', 'variables.gradle'); + let variablesGradle = ''; + if (existsSync(variablesFile)) { + variablesGradle = await readFileAsync(variablesFile, 'utf8'); + } + prefsArray.map((preference: any) => { + if (variablesGradle.includes(preference.$.name)) { + frameworkString = frameworkString.replace(new RegExp(('$' + preference.$.name).replace('$', '\\$&'), 'g'), `$rootProject.ext.${preference.$.name}`); + } else { + frameworkString = frameworkString.replace(new RegExp(('$' + preference.$.name).replace('$', '\\$&'), 'g'), preference.$.default); + } + }); + return frameworkString; +} diff --git a/plugin-template/android/build.gradle b/plugin-template/android/build.gradle index 5a9ea9923b..68f49e0dc0 100644 --- a/plugin-template/android/build.gradle +++ b/plugin-template/android/build.gradle @@ -1,3 +1,9 @@ +ext { + junitVersion = project.hasProperty('junitVersion') ? rootProject.ext.junitVersion : '4.12' + androidxJunitVersion = project.hasProperty('androidxJunitVersion') ? rootProject.ext.androidxJunitVersion : '1.1.1' + androidxEspressoCoreVersion = project.hasProperty('androidxEspressoCoreVersion') ? rootProject.ext.androidxEspressoCoreVersion : '3.2.0' +} + buildscript { repositories { google() @@ -11,10 +17,10 @@ buildscript { apply plugin: 'com.android.library' android { - compileSdkVersion 29 + compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 29 defaultConfig { - minSdkVersion 21 - targetSdkVersion 29 + minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21 + targetSdkVersion = project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" @@ -40,7 +46,7 @@ repositories { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation project(':capacitor-android') - testImplementation 'junit:junit:4.12' - androidTestImplementation 'androidx.test.ext:junit:1.1.1' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' + testImplementation "junit:junit:$junitVersion" + androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion" + androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion" } From 1d76a20e9f748843ec2ffea5a2941133bec0371d Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Fri, 6 Mar 2020 19:46:35 +0100 Subject: [PATCH 2/2] remove some unneeded rootProject.ext. --- android-template/app/build.gradle | 8 ++++---- capacitor-cordova-android-plugins/build.gradle | 2 +- cli/src/android/update.ts | 4 +--- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/android-template/app/build.gradle b/android-template/app/build.gradle index aa6f22a474..641d2a8091 100644 --- a/android-template/app/build.gradle +++ b/android-template/app/build.gradle @@ -26,11 +26,11 @@ repositories { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation "androidx.appcompat:appcompat:$rootProject.ext.androidxAppCompatVersion" + implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion" implementation project(':capacitor-android') - testImplementation "junit:junit:$rootProject.ext.junitVersion" - androidTestImplementation "androidx.test.ext:junit:$rootProject.ext.androidxJunitVersion" - androidTestImplementation "androidx.test.espresso:espresso-core:$rootProject.ext.androidxEspressoCoreVersion" + testImplementation "junit:junit:$junitVersion" + androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion" + androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion" implementation project(':capacitor-cordova-android-plugins') } diff --git a/capacitor-cordova-android-plugins/build.gradle b/capacitor-cordova-android-plugins/build.gradle index f238a03aac..86dbdb5df0 100644 --- a/capacitor-cordova-android-plugins/build.gradle +++ b/capacitor-cordova-android-plugins/build.gradle @@ -38,7 +38,7 @@ repositories { dependencies { implementation fileTree(dir: 'src/main/libs', include: ['*.jar']) - implementation "org.apache.cordova:framework:$rootProject.ext.cordovaAndroidVersion" + implementation "org.apache.cordova:framework:$cordovaAndroidVersion" // SUB-PROJECT DEPENDENCIES START // SUB-PROJECT DEPENDENCIES END diff --git a/cli/src/android/update.ts b/cli/src/android/update.ts index 81e5df1843..0a7d24b3d1 100644 --- a/cli/src/android/update.ts +++ b/cli/src/android/update.ts @@ -205,9 +205,7 @@ async function replaceFrameworkVariables(config: Config, prefsArray: Array, variablesGradle = await readFileAsync(variablesFile, 'utf8'); } prefsArray.map((preference: any) => { - if (variablesGradle.includes(preference.$.name)) { - frameworkString = frameworkString.replace(new RegExp(('$' + preference.$.name).replace('$', '\\$&'), 'g'), `$rootProject.ext.${preference.$.name}`); - } else { + if (!variablesGradle.includes(preference.$.name)) { frameworkString = frameworkString.replace(new RegExp(('$' + preference.$.name).replace('$', '\\$&'), 'g'), preference.$.default); } });