Skip to content

Commit d94eb34

Browse files
authored
chore: Android build (#545)
* refactors * new build style for android * global melos * simplify * correct path
1 parent cfc4838 commit d94eb34

19 files changed

+103
-212
lines changed

.github/workflows/analysis.yml

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ jobs:
99
- uses: axel-op/dart-package-analyzer@v3
1010
with:
1111
# Required:
12-
githubToken: ${{ secrets.GITHUB_TOKEN }}
12+
githubToken: ${{ secrets.GITHUB_TOKEN }}
13+
relativePath: workmanager/

.github/workflows/examples.yml

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ jobs:
2020

2121
- name: build
2222
run: |
23+
dart pub global activate melos
24+
melos bootstrap
2325
cd example && flutter build apk --debug
2426
2527
example_ios:
@@ -32,4 +34,6 @@ jobs:
3234

3335
- name: build
3436
run: |
37+
dart pub global activate melos
38+
melos bootstrap
3539
cd example && flutter build ios --debug --no-codesign

.github/workflows/format.yml

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ jobs:
4747

4848
- name: publish checks
4949
run: |
50+
dart pub global activate melos
51+
melos bootstrap
5052
cd workmanager
5153
flutter pub get
5254
flutter pub publish -n

ANDROID_SETUP.md

-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,3 @@
1-
# Update build.gradle
2-
3-
Make sure that your kotlin_version to `1.8.0` or greater:
4-
5-
```
6-
buildscript {
7-
ext.kotlin_version = '1.8.0+'
8-
repositories {
9-
google()
10-
mavenCentral()
11-
}
12-
// ...
13-
```
14-
151
# Check your AndroidManifest.xml
162

173
Check if you have the following in your `AndroidManifest.xml` file.

README.md

+8-8
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,16 @@ Workmanager().registerProcessingTask(
176176
```
177177

178178
### Background App Refresh permission
179+
179180
On iOS user can disable `Background App Refresh` permission anytime, hence background tasks can only run if user has granted the permission.
180-
With `Workmanager.checkBackgroundRefreshPermission` you can check whether background app refresh is enabled. If it is not enabled you might ask
181-
the user to enable it in app settings.
182181

183-
```dart
184-
if (Platform.isIOS) {
185-
final hasPermission = await Workmanager().checkBackgroundRefreshPermission();
186-
if (hasPermission != BackgroundRefreshPermissionState.available){
187-
// Inform the user that background app refresh is disabled
188-
}
182+
Use `permision_handler` to check for the permission:
183+
184+
``` dart
185+
final status = await Permission.backgroundRefresh.status;
186+
if (status != PermissionStatus.granted) {
187+
_showNoPermission(context, status);
188+
return;
189189
}
190190
```
191191

example/android/app/build.gradle

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
plugins {
2+
id "com.android.application"
3+
id "kotlin-android"
4+
id "dev.flutter.flutter-gradle-plugin"
5+
}
6+
17
def localProperties = new Properties()
28
def localPropertiesFile = rootProject.file('local.properties')
39
if (localPropertiesFile.exists()) {
@@ -6,10 +12,6 @@ if (localPropertiesFile.exists()) {
612
}
713
}
814

9-
def flutterRoot = localProperties.getProperty('flutter.sdk')
10-
if (flutterRoot == null) {
11-
throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
12-
}
1315

1416
def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
1517
if (flutterVersionCode == null) {
@@ -21,10 +23,6 @@ if (flutterVersionName == null) {
2123
flutterVersionName = '1.0'
2224
}
2325

24-
apply plugin: 'com.android.application'
25-
apply plugin: 'kotlin-android'
26-
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
27-
2826
android {
2927
sourceSets {
3028
main.java.srcDirs += 'src/main/kotlin'
@@ -74,7 +72,6 @@ flutter {
7472
}
7573

7674
dependencies {
77-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
7875
testImplementation 'junit:junit:4.13.2'
7976
androidTestImplementation 'androidx.test:runner:1.2.0'
8077
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

example/android/build.gradle

-13
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
1-
buildscript {
2-
ext.kotlin_version = '1.8.10'
3-
repositories {
4-
google()
5-
mavenCentral()
6-
}
7-
8-
dependencies {
9-
classpath 'com.android.tools.build:gradle:8.1.1'
10-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
11-
}
12-
}
13-
141
plugins {
152
id "com.github.ben-manes.versions" version "0.41.0"
163
}

example/android/settings.gradle

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
include ':app'
1+
pluginManagement {
2+
def flutterSdkPath = {
3+
def properties = new Properties()
4+
file("local.properties").withInputStream { properties.load(it) }
5+
def flutterSdkPath = properties.getProperty("flutter.sdk")
6+
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
7+
return flutterSdkPath
8+
}()
29

3-
def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
10+
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")
411

5-
def plugins = new Properties()
6-
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
7-
if (pluginsFile.exists()) {
8-
pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
12+
repositories {
13+
google()
14+
mavenCentral()
15+
gradlePluginPortal()
16+
}
917
}
1018

11-
plugins.each { name, path ->
12-
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
13-
include ":$name"
14-
project(":$name").projectDir = pluginDirectory
19+
plugins {
20+
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
21+
id "com.android.application" version "8.1.4" apply false
22+
id "org.jetbrains.kotlin.android" version "1.9.23" apply false
1523
}
24+
25+
include ":app"

example/ios/Podfile.lock

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ PODS:
55
- path_provider_foundation (0.0.1):
66
- Flutter
77
- FlutterMacOS
8+
- permission_handler_apple (9.3.0):
9+
- Flutter
810
- shared_preferences_foundation (0.0.1):
911
- Flutter
1012
- FlutterMacOS
@@ -15,6 +17,7 @@ DEPENDENCIES:
1517
- Flutter (from `Flutter`)
1618
- integration_test (from `.symlinks/plugins/integration_test/ios`)
1719
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
20+
- permission_handler_apple (from `.symlinks/plugins/permission_handler_apple/ios`)
1821
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
1922
- workmanager (from `.symlinks/plugins/workmanager/ios`)
2023

@@ -25,6 +28,8 @@ EXTERNAL SOURCES:
2528
:path: ".symlinks/plugins/integration_test/ios"
2629
path_provider_foundation:
2730
:path: ".symlinks/plugins/path_provider_foundation/darwin"
31+
permission_handler_apple:
32+
:path: ".symlinks/plugins/permission_handler_apple/ios"
2833
shared_preferences_foundation:
2934
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"
3035
workmanager:
@@ -34,6 +39,7 @@ SPEC CHECKSUMS:
3439
Flutter: e0871f40cf51350855a761d2e70bf5af5b9b5de7
3540
integration_test: 13825b8a9334a850581300559b8839134b124670
3641
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943
42+
permission_handler_apple: 9878588469a2b0d0fc1e048d9f43605f92e6cec2
3743
shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126
3844
workmanager: 0afdcf5628bbde6924c21af7836fed07b42e30e6
3945

example/ios/Runner.xcodeproj/project.pbxproj

+19
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@
190190
9705A1C41CF9048500538489 /* Embed Frameworks */,
191191
3B06AD1E1E4923F5004D2608 /* Thin Binary */,
192192
F4A106E3067F0564F4FF488C /* [CP] Embed Pods Frameworks */,
193+
E672F4E929E3D3E957CCC34B /* [CP] Copy Pods Resources */,
193194
);
194195
buildRules = (
195196
);
@@ -353,6 +354,24 @@
353354
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
354355
showEnvVarsInLog = 0;
355356
};
357+
E672F4E929E3D3E957CCC34B /* [CP] Copy Pods Resources */ = {
358+
isa = PBXShellScriptBuildPhase;
359+
buildActionMask = 2147483647;
360+
files = (
361+
);
362+
inputPaths = (
363+
"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources.sh",
364+
"${PODS_CONFIGURATION_BUILD_DIR}/permission_handler_apple/permission_handler_apple_privacy.bundle",
365+
);
366+
name = "[CP] Copy Pods Resources";
367+
outputPaths = (
368+
"${TARGET_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/permission_handler_apple_privacy.bundle",
369+
);
370+
runOnlyForDeploymentPostprocessing = 0;
371+
shellPath = /bin/sh;
372+
shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-Runner/Pods-Runner-resources.sh\"\n";
373+
showEnvVarsInLog = 0;
374+
};
356375
F4A106E3067F0564F4FF488C /* [CP] Embed Pods Frameworks */ = {
357376
isa = PBXShellScriptBuildPhase;
358377
buildActionMask = 2147483647;

example/lib/main.dart

+5-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:math';
44

55
import 'package:flutter/material.dart';
66
import 'package:path_provider/path_provider.dart';
7+
import 'package:permission_handler/permission_handler.dart';
78
import 'package:shared_preferences/shared_preferences.dart';
89
import 'package:workmanager/workmanager.dart';
910

@@ -124,11 +125,9 @@ class _MyAppState extends State<MyApp> {
124125
child: Text("Start the Flutter background service"),
125126
onPressed: () async {
126127
if (Platform.isIOS) {
127-
final hasPermission = await Workmanager()
128-
.checkBackgroundRefreshPermission();
129-
if (hasPermission !=
130-
BackgroundRefreshPermissionState.available) {
131-
_showNoPermission(context, hasPermission);
128+
final status = await Permission.backgroundRefresh.status;
129+
if (status != PermissionStatus.granted) {
130+
_showNoPermission(context, status);
132131
return;
133132
}
134133
}
@@ -352,8 +351,7 @@ class _MyAppState extends State<MyApp> {
352351
);
353352
}
354353

355-
void _showNoPermission(
356-
BuildContext context, BackgroundRefreshPermissionState hasPermission) {
354+
void _showNoPermission(BuildContext context, PermissionStatus hasPermission) {
357355
showDialog(
358356
context: context,
359357
builder: (BuildContext context) {

example/pubspec.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ environment:
66
sdk: ">=2.18.0 <4.0.0"
77

88
dependencies:
9-
path_provider: ^2.0.11
10-
shared_preferences: ^2.2.1
9+
path_provider:
10+
shared_preferences:
11+
permission_handler:
1112
flutter:
1213
sdk: flutter
1314
workmanager:

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ name: workmanager_workspace
33
environment:
44
sdk: '>=2.17.0 <3.0.0'
55
dev_dependencies:
6-
melos: ^3.1.0
6+
melos: ^5.3.0

workmanager/android/build.gradle

+2-17
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
group 'dev.fluttercommunity.workmanager'
22
version '1.0-SNAPSHOT'
33

4-
buildscript {
5-
ext.kotlin_version = '1.8.10'
6-
repositories {
7-
google()
8-
mavenCentral()
9-
}
10-
11-
dependencies {
12-
classpath 'com.android.tools.build:gradle:8.0.2'
13-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
14-
}
15-
}
16-
174
rootProject.allprojects {
185
repositories {
196
mavenCentral()
@@ -34,7 +21,7 @@ android {
3421
main.java.srcDirs += 'src/main/kotlin'
3522
}
3623
defaultConfig {
37-
compileSdk 33
24+
compileSdk 34
3825
minSdkVersion 19
3926
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
4027
}
@@ -55,9 +42,7 @@ android {
5542
}
5643

5744
dependencies {
58-
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
59-
60-
def work_version = "2.8.1"
45+
def work_version = "2.9.0"
6146
implementation "androidx.work:work-runtime:$work_version"
6247
implementation "androidx.concurrent:concurrent-futures:1.1.0"
6348

workmanager/android/settings.gradle

+25-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,25 @@
1-
rootProject.name = 'workmanager'
1+
pluginManagement {
2+
def flutterSdkPath = {
3+
def properties = new Properties()
4+
file("local.properties").withInputStream { properties.load(it) }
5+
def flutterSdkPath = properties.getProperty("flutter.sdk")
6+
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
7+
return flutterSdkPath
8+
}()
9+
10+
includeBuild("$flutterSdkPath/packages/flutter_tools/gradle")
11+
12+
repositories {
13+
google()
14+
mavenCentral()
15+
gradlePluginPortal()
16+
}
17+
}
18+
19+
plugins {
20+
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
21+
id "com.android.application" version "8.1.4" apply false
22+
id "org.jetbrains.kotlin.android" version "1.9.23" apply false
23+
}
24+
25+
include ":app"

0 commit comments

Comments
 (0)