Skip to content

Commit 7ad81a8

Browse files
[Android] Android 14 (SDK 34) support
1 parent 8eb0a1c commit 7ad81a8

File tree

9 files changed

+68
-58
lines changed

9 files changed

+68
-58
lines changed

android/app/build.gradle

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

33
android {
4-
compileSdkVersion 30
4+
compileSdkVersion 34
5+
compileOptions {
6+
sourceCompatibility JavaVersion.VERSION_1_8
7+
targetCompatibility JavaVersion.VERSION_1_8
8+
}
59
defaultConfig {
610
applicationId "com.transistorsoft.backgroundfetch"
711
minSdkVersion 16
8-
targetSdkVersion 30
12+
targetSdkVersion 34
913
versionCode 1
1014
versionName "1.0"
1115
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
@@ -19,9 +23,9 @@ android {
1923
}
2024

2125
dependencies {
22-
testImplementation 'junit:junit:4.12'
23-
androidTestImplementation 'androidx.test:runner:1.2.0'
24-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
26+
testImplementation 'junit:junit:4.13.2'
27+
androidTestImplementation 'androidx.test:runner:1.5.2'
28+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
2529

2630
implementation fileTree(dir: 'libs', include: ['*.jar'])
2731

android/build.gradle

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
// Top-level build file where you can add configuration options common to all sub-projects/modules.
22

33
buildscript {
4+
ext {
5+
compileSdkVersion = 34
6+
targetSdkVersion = 34
7+
appCompatVersion = "1.6.1"
8+
}
49
repositories {
510
google()
611
mavenCentral()
712
}
813

914
dependencies {
10-
classpath 'com.android.tools.build:gradle:4.1.3'
15+
classpath 'com.android.tools.build:gradle:7.4.2'
1116

1217

1318
// NOTE: Do not place your application dependencies here; they belong
@@ -26,9 +31,4 @@ task clean(type: Delete) {
2631
delete rootProject.buildDir
2732
}
2833

29-
ext {
30-
compileSdkVersion = 32
31-
targetSdkVersion = 31
32-
buildToolsVersion = "29.0.6"
33-
appCompatVersion = "1.4.1"
34-
}
34+

android/gradle.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ org.gradle.jvmargs=-Xmx1536m
1616
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
1717
# org.gradle.parallel=true
1818

19-
VERSION_NAME=0.5.7
20-
VERSION_CODE=22
19+
VERSION_NAME=1.0.1
20+
VERSION_CODE=24
2121

2222
android.useAndroidX=true
2323
android.enableJetifier=true

android/gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip

android/settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
include ':app', ':tsbackgroundfetch'
1+
include ':tsbackgroundfetch'

android/tsbackgroundfetch/build.gradle

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,48 @@
11
apply plugin: 'com.android.library'
2-
apply plugin: 'maven'
32
apply plugin: 'maven-publish'
43

4+
publishing {
5+
publications {
6+
tsbackgroundfetch(MavenPublication) {
7+
groupId 'com.transistorsoft'
8+
artifactId 'tsbackgroundfetch'
9+
version VERSION_NAME
10+
artifact("$buildDir/outputs/aar/tsbackgroundfetch-release.aar")
11+
12+
}
13+
}
14+
repositories {
15+
maven {
16+
name = "tsbackgroundfetch"
17+
url "$buildDir/repo"
18+
}
19+
}
20+
}
21+
522
android {
23+
namespace "com.transistorsoft.tsbackgroundfetch"
624
compileSdkVersion rootProject.compileSdkVersion
725
defaultConfig {
826
minSdkVersion 16
927
targetSdkVersion rootProject.targetSdkVersion
10-
versionCode 1
11-
versionName "1.0"
1228
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
1329
}
14-
1530
buildTypes {
1631
release {
1732
minifyEnabled false
1833
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
1934
}
2035
}
21-
publishing {
22-
publications {
23-
tslocationmanager(MavenPublication) {
24-
groupId 'com.transistorsoft'
25-
artifactId 'tsbackgroundfetch'
26-
version VERSION_NAME
27-
artifact("$buildDir/outputs/aar/tsbackgroundfetch-release.aar")
28-
29-
}
30-
}
31-
repositories {
32-
maven {
33-
url "$buildDir/repo"
34-
}
35-
}
36-
}
37-
compileOptions {
38-
sourceCompatibility JavaVersion.VERSION_1_8
39-
targetCompatibility JavaVersion.VERSION_1_8
40-
}
36+
4137
}
4238

4339
dependencies {
44-
testImplementation 'junit:junit:4.12'
45-
androidTestImplementation 'androidx.test:runner:1.2.0'
40+
testImplementation 'junit:junit:4.13.2'
41+
androidTestImplementation 'androidx.test:runner:1.5.2'
4642

4743
implementation fileTree(dir: 'libs', include: ['*.jar'])
4844

49-
implementation "androidx.lifecycle:lifecycle-runtime:2.5.1"
45+
implementation "androidx.lifecycle:lifecycle-runtime:2.6.1"
5046
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
5147
//implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
5248

@@ -56,7 +52,7 @@ dependencies {
5652
task buildRelease { task ->
5753
task.dependsOn 'cordovaRelease'
5854
task.dependsOn 'reactNativeRelease'
59-
task.dependsOn 'nativeScriptRelease'
55+
//task.dependsOn 'nativeScriptRelease'
6056
task.dependsOn 'flutterRelease'
6157
task.dependsOn 'capacitorRelease'
6258
}

android/tsbackgroundfetch/src/main/AndroidManifest.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.transistorsoft.tsbackgroundfetch">
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
32

43
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
54
<uses-permission android:name="android.permission.WAKE_LOCK" />
6-
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
5+
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" android:maxSdkVersion="33" />
76

87
<application>
9-
<receiver android:name="com.transistorsoft.tsbackgroundfetch.FetchAlarmReceiver" />
8+
<receiver android:name="com.transistorsoft.tsbackgroundfetch.FetchAlarmReceiver" android:exported="false" />
109
<service android:name="com.transistorsoft.tsbackgroundfetch.FetchJobService" android:permission="android.permission.BIND_JOB_SERVICE" android:exported="true" />
1110
<receiver android:name="com.transistorsoft.tsbackgroundfetch.BootReceiver" android:exported="false">
1211
<intent-filter>

android/tsbackgroundfetch/src/main/java/com/transistorsoft/tsbackgroundfetch/BGTask.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.transistorsoft.tsbackgroundfetch;
22

3+
import android.annotation.SuppressLint;
34
import android.app.AlarmManager;
45
import android.app.PendingIntent;
56
import android.app.job.JobInfo;
@@ -125,18 +126,19 @@ static void schedule(Context context, BackgroundFetchConfig config) {
125126

126127
long interval = (config.isFetchTask()) ? (TimeUnit.MINUTES.toMillis(config.getMinimumFetchInterval())) : config.getDelay();
127128

128-
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !config.getForceAlarmManager()) {
129+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !config.getForceAlarmManager()) {
129130
// API 21+ uses new JobScheduler API
130131

131132
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
133+
@SuppressLint("WrongConstant")
132134
JobInfo.Builder builder = new JobInfo.Builder(config.getJobId(), new ComponentName(context, FetchJobService.class))
133135
.setRequiredNetworkType(config.getRequiredNetworkType())
134136
.setRequiresDeviceIdle(config.getRequiresDeviceIdle())
135137
.setRequiresCharging(config.getRequiresCharging())
136138
.setPersisted(config.getStartOnBoot() && !config.getStopOnTerminate());
137139

138140
if (config.getPeriodic()) {
139-
if (android.os.Build.VERSION.SDK_INT >= 24) {
141+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
140142
builder.setPeriodic(interval, interval);
141143
} else {
142144
builder.setPeriodic(interval);
@@ -150,7 +152,7 @@ static void schedule(Context context, BackgroundFetchConfig config) {
150152

151153
builder.setExtras(extras);
152154

153-
if (android.os.Build.VERSION.SDK_INT >= 26) {
155+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
154156
builder.setRequiresStorageNotLow(config.getRequiresStorageNotLow());
155157
builder.setRequiresBatteryNotLow(config.getRequiresBatteryNotLow());
156158
}
@@ -166,9 +168,17 @@ static void schedule(Context context, BackgroundFetchConfig config) {
166168
if (config.getPeriodic()) {
167169
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, delay, interval, pi);
168170
} else {
169-
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
170-
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, delay, pi);
171-
} else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
171+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
172+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
173+
if (alarmManager.canScheduleExactAlarms()) {
174+
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, delay, pi);
175+
} else {
176+
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, delay, pi);
177+
}
178+
} else {
179+
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, delay, pi);
180+
}
181+
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
172182
alarmManager.setExact(AlarmManager.RTC_WAKEUP, delay, pi);
173183
} else {
174184
alarmManager.set(AlarmManager.RTC_WAKEUP, delay, pi);
@@ -193,7 +203,11 @@ void onTimeout(Context context) {
193203
BackgroundFetchConfig config = adapter.getConfig(mTaskId);
194204
if (config != null) {
195205
if (config.getJobService() != null) {
196-
fireHeadlessEvent(context, config);
206+
try {
207+
fireHeadlessEvent(context, config);
208+
} catch (BGTask.Error error) {
209+
Log.e(BackgroundFetch.TAG, "Headless task error: " + error.getMessage());
210+
}
197211
} else {
198212
adapter.finish(mTaskId);
199213
}

android/tsbackgroundfetch/src/main/java/com/transistorsoft/tsbackgroundfetch/BackgroundFetch.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package com.transistorsoft.tsbackgroundfetch;
22

33
import android.annotation.TargetApi;
4-
import android.app.ActivityManager;
54

65
import android.content.Context;
7-
import android.content.Intent;
8-
import android.content.pm.PackageManager;
96
import android.os.Build;
107
import android.os.Handler;
118
import android.os.Looper;

0 commit comments

Comments
 (0)