Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Update Android dependencies and add AndroidX support #137

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
cordova plugin add https://github.com/aerogear/aerogear-cordova-push.git#:/tests
```

1. Add this plugin:
3. Add this plugin:
```bash
cordova plugin add http://git-wip-us.apache.org/repos/asf/cordova-plugin-test-framework.git
```
1. Change the start page in `config.xml` with `<content src="cdvtests/index.html" />` or navigate to cdvtests/index.html from within your app.
4. Change the start page in `config.xml` with `<content src="cdvtests/index.html" />` or navigate to cdvtests/index.html from within your app.

## Documentation

Expand All @@ -50,7 +50,22 @@ Many other plugins require Google Play Services and/or Firebase libraries. This
For example:

```
cordova plugin add aerogear-cordova-push --variable FIREBASE_VERSION=11.8.0
cordova plugin add aerogear-cordova-push --variable FIREBASE_VERSION=18.0.0
```

## AndroidX Support

This plugin has [AndroidX](https://developer.android.com/jetpack/androidx) support. This means that you should migrate your project to AndroidX. To prevent to do it manually everytime, there are 2 great plugins to migrate it:

1. First, enable AndroidX adding the [cordova-plugin-androidx](https://github.com/dpa99c/cordova-plugin-androidx) plugin:

```
cordova plugin add cordova-plugin-androidx
```

2. If you encounter build failures after installing (or after manually enabling AndroidX), try to install [cordova-plugin-androidx-adapter](https://github.com/dpa99c/cordova-plugin-androidx-adapter) into your project. It will migrate any references from the legacy Android Support library to use the new AndroidX which should resolve build failures.
```
cordova plugin add cordova-plugin-androidx-adapter
```

## Development
Expand Down
22 changes: 6 additions & 16 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,19 @@
<config-file target="AndroidManifest.xml" parent="/manifest">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.INTERNET" />
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<activity android:name="org.jboss.aerogear.cordova.push.PushHandlerActivity"/>
<meta-data android:name="DEFAULT_MESSAGE_HANDLER_KEY" android:value="org.jboss.aerogear.cordova.push.NotificationMessageHandler" />
<service android:name="org.jboss.aerogear.android.unifiedpush.fcm.UnifiedPushInstanceIDListenerService" android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<service android:name="org.jboss.aerogear.android.unifiedpush.fcm.AeroGearFCMMessageReceiver">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</config-file>
<framework src="com.google.firebase:firebase-messaging:$FIREBASE_VERSION" />
<preference name="FIREBASE_VERSION" default="10.2.1"/>
<framework src="com.android.support:appcompat-v7:27.+" />
<framework src="com.android.support:support-v4:27.+" />
<framework src="com.android.support:support-compat:27.+" />
<framework src="com.android.support:support-annotations:27.+" />
<preference name="FIREBASE_VERSION" default="19.0.0"/>
<framework src="androidx.appcompat:appcompat:1.1.0" />
<framework src="androidx.core:core:1.1.0" />
<framework src="androidx.annotation:annotation:1.1.0" />
<framework src="org.jboss.aerogear:aerogear-android-store:3.0.2" />
<framework src="org.jboss.aerogear:aerogear-android-push:4.0.2" />
<framework src="org.jboss.aerogear:aerogear-android-push:5.1.0" />
<framework src="me.leolin:ShortcutBadger:1.1.12@aar"/>
<framework src="src/android/dependencies.gradle" type="gradleReference" custom="true"/>
<source-file src="src/android/org/jboss/aerogear/cordova/push/NotificationMessageHandler.java" target-dir="src/org/jboss/aerogear/cordova/push/"/>
Expand Down
40 changes: 13 additions & 27 deletions scripts/androidBeforeInstall.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
module.exports = function(ctx) {
var fs = require('fs'),
path = require('path'),
os = require("os"),
readline = require("readline");

var platformRoot = path.join(ctx.opts.projectRoot, 'www');
var settingsFile = path.join(platformRoot, 'google-services.json');

var platformAndroid = 'platforms/android';

if (fs.existsSync('platforms/android/app/src/main')) {
Expand All @@ -15,28 +11,18 @@ module.exports = function(ctx) {
}

return new Promise(function(resolve, reject) {
fs.stat(settingsFile, function(err,stats) {
if (err) {
reject("To use this plugin on android you'll need to add a google-services.json file with the FCM project_info and place that into your www folder");
} else {

fs.createReadStream(settingsFile).pipe(fs.createWriteStream(platformAndroid + '/google-services.json'));

var lineReader = readline.createInterface({
terminal: false,
input : fs.createReadStream(platformAndroid + '/build.gradle')
});
lineReader.on("line", function(line) {
fs.appendFileSync('./build.gradle', line.toString() + os.EOL);
if (/.*\ dependencies \{.*/.test(line)) {
fs.appendFileSync('./build.gradle', '\t\tclasspath "com.google.gms:google-services:3.0.0"' + os.EOL);
fs.appendFileSync('./build.gradle', '\t\tclasspath "com.android.tools.build:gradle:1.2.3+"' + os.EOL);
}
}).on("close", function () {
fs.rename('./build.gradle', platformAndroid + '/build.gradle', resolve);
});

}
});
var lineReader = readline.createInterface({
terminal: false,
input : fs.createReadStream(platformAndroid + '/build.gradle')
});
lineReader.on("line", function(line) {
fs.appendFileSync('./build.gradle', line.toString() + os.EOL);
if (/.*\ dependencies \{.*/.test(line)) {
fs.appendFileSync('./build.gradle', '\t\tclasspath "com.google.gms:google-services:4.2.0"' + os.EOL);
fs.appendFileSync('./build.gradle', '\t\tclasspath "com.android.tools.build:gradle:3.3.0"' + os.EOL);
}
}).on("close", function () {
fs.rename('./build.gradle', platformAndroid + '/build.gradle', resolve);
});
})
};
13 changes: 2 additions & 11 deletions src/android/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ repositories {
mavenLocal()
}

cdvMinSdkVersion = Math.max(16, cdvHelpers.getConfigPreference('android-minSdkVersion', 0) as Integer)
//cdvMinSdkVersion = Math.max(16, cdvHelpers.getConfigPreference('android-minSdkVersion', 0) as Integer)
cdvPluginPostBuildExtras << {
apply plugin: 'com.google.gms.google-services'
}
Expand All @@ -17,15 +17,6 @@ if (file('src/main/AndroidManifest.xml').exists()) {
manifestFile = 'AndroidManifest.xml'
}

import java.util.regex.Pattern

def doExtractStringFromManifest(name) {
def manifestFile = file(android.sourceSets.main.manifest.srcFile)
def pattern = Pattern.compile(name + "=\"(.*?)\"")
def matcher = pattern.matcher(manifestFile.getText())
matcher.find()
return matcher.group(1)
}

android {
sourceSets {
Expand All @@ -42,6 +33,6 @@ android {
}

defaultConfig {
applicationId = doExtractStringFromManifest("package")
applicationId cordovaConfig.PACKAGE_NAMESPACE
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;

import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;

import org.jboss.aerogear.android.store.DataManager;
import org.jboss.aerogear.android.store.sql.SQLStore;
Expand Down