Skip to content

Commit

Permalink
feat(android): use common variables for config and dependencies (#2534)
Browse files Browse the repository at this point in the history
  • Loading branch information
jcesarmobile committed Mar 9, 2020
1 parent 2209113 commit d1009bb
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 43 deletions.
17 changes: 7 additions & 10 deletions android-template/app/build.gradle
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -19,21 +19,18 @@ android {
}

repositories {
maven {
url "https://dl.bintray.com/ionic-team/capacitor"
}
flatDir{
dirs '../capacitor-cordova-android-plugins/src/main/libs', 'libs'
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation "androidx.appcompat:appcompat:$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:$junitVersion"
androidTestImplementation "androidx.test.ext:junit:$androidxJunitVersion"
androidTestImplementation "androidx.test.espresso:espresso-core:$androidxEspressoCoreVersion"
implementation project(':capacitor-cordova-android-plugins')
}

Expand Down
2 changes: 2 additions & 0 deletions android-template/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ buildscript {
}
}

apply from: "variables.gradle"

allprojects {
repositories {
google()
Expand Down
16 changes: 16 additions & 0 deletions android-template/variables.gradle
Original file line number Diff line number Diff line change
@@ -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'
}
45 changes: 30 additions & 15 deletions android/capacitor/build.gradle
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -73,4 +88,4 @@ publish {
}
desc = 'Capacitor Android Runtime'
website = 'https://github.com/ionic-team/capacitor'
}
}
8 changes: 4 additions & 4 deletions capacitor-cordova-android-plugins/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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:$cordovaAndroidVersion"
// SUB-PROJECT DEPENDENCIES START

// SUB-PROJECT DEPENDENCIES END
Expand Down
26 changes: 18 additions & 8 deletions cli/src/android/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -134,17 +132,15 @@ 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');
buildGradle = buildGradle.replace(/(PLUGIN GRADLE EXTENSIONS START)[\s\S]*(\/\/ PLUGIN GRADLE EXTENSIONS END)/, '$1\n' + applyString.concat('\n') + '$2');
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 = []
}`;
Expand Down Expand Up @@ -201,3 +197,17 @@ async function getPluginsTask(config: Config) {
return androidPlugins;
});
}

async function replaceFrameworkVariables(config: Config, prefsArray: Array<any>, 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'), preference.$.default);
}
});
return frameworkString;
}
18 changes: 12 additions & 6 deletions plugin-template/android/build.gradle
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -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"
Expand All @@ -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"
}

0 comments on commit d1009bb

Please sign in to comment.