Unverified Commit ec9813a5 authored by shihchanghsiungsonos's avatar shihchanghsiungsonos Committed by GitHub

Fix the issue of Android add2app build fails on Android when assets are read-only (#50047)

parent e481fcae
...@@ -12,6 +12,7 @@ import 'package:path/path.dart' as path; ...@@ -12,6 +12,7 @@ import 'package:path/path.dart' as path;
final String gradlew = Platform.isWindows ? 'gradlew.bat' : 'gradlew'; final String gradlew = Platform.isWindows ? 'gradlew.bat' : 'gradlew';
final String gradlewExecutable = Platform.isWindows ? '.\\$gradlew' : './$gradlew'; final String gradlewExecutable = Platform.isWindows ? '.\\$gradlew' : './$gradlew';
final String fileReadWriteMode = Platform.isWindows ? 'rw-rw-rw-' : 'rw-r--r--';
final bool useAndroidEmbeddingV2 = Platform.environment['ENABLE_ANDROID_EMBEDDING_V2'] == 'true'; final bool useAndroidEmbeddingV2 = Platform.environment['ENABLE_ANDROID_EMBEDDING_V2'] == 'true';
...@@ -39,10 +40,36 @@ Future<void> main() async { ...@@ -39,10 +40,36 @@ Future<void> main() async {
); );
}); });
section('Add plugins'); section('Add read-only asset');
final File readonlyTxtAssetFile = await File(path.join(
projectDir.path,
'assets/read-only.txt'
))
.create(recursive: true);
if (!exists(readonlyTxtAssetFile)) {
return TaskResult.failure('Failed to create read-only asset');
}
if (!Platform.isWindows) {
await exec('chmod', <String>[
'444',
readonlyTxtAssetFile.path
]);
}
final File pubspec = File(path.join(projectDir.path, 'pubspec.yaml')); final File pubspec = File(path.join(projectDir.path, 'pubspec.yaml'));
String content = await pubspec.readAsString(); String content = await pubspec.readAsString();
content = content.replaceFirst(
'\n # assets:\n',
'\n assets:\n - assets/read-only.txt\n',
);
await pubspec.writeAsString(content, flush: true);
section('Add plugins');
content = await pubspec.readAsString();
content = content.replaceFirst( content = content.replaceFirst(
'\ndependencies:\n', '\ndependencies:\n',
'\ndependencies:\n device_info: 0.4.1\n package_info: 0.4.0+9\n', '\ndependencies:\n device_info: 0.4.1\n package_info: 0.4.0+9\n',
...@@ -226,6 +253,29 @@ Future<void> main() async { ...@@ -226,6 +253,29 @@ Future<void> main() async {
); );
} }
section('Check file access modes for read-only asset from Flutter module');
final String readonlyDebugAssetFilePath = path.join(
hostApp.path,
'app',
'build',
'intermediates',
'merged_assets',
'debug',
'out',
'flutter_assets/assets/read-only.txt',
);
final File readonlyDebugAssetFile = File(readonlyDebugAssetFilePath);
if (!exists(readonlyDebugAssetFile)) {
return TaskResult.failure('Failed to copy read-only asset file');
}
String modes = readonlyDebugAssetFile.statSync().modeString();
print('\nread-only.txt file access modes = $modes');
if (modes != null && modes.compareTo(fileReadWriteMode) != 0) {
return TaskResult.failure('Failed to make assets user-readable and writable');
}
section('Build release host APK'); section('Build release host APK');
await inDirectory(hostApp, () async { await inDirectory(hostApp, () async {
...@@ -272,6 +322,30 @@ Future<void> main() async { ...@@ -272,6 +322,30 @@ Future<void> main() async {
) { ) {
return TaskResult.failure("Release host APK doesn't contain metadata: flutterProjectType = module "); return TaskResult.failure("Release host APK doesn't contain metadata: flutterProjectType = module ");
} }
section('Check file access modes for read-only asset from Flutter module');
final String readonlyReleaseAssetFilePath = path.join(
hostApp.path,
'app',
'build',
'intermediates',
'merged_assets',
'release',
'out',
'flutter_assets/assets/read-only.txt',
);
final File readonlyReleaseAssetFile = File(readonlyReleaseAssetFilePath);
if (!exists(readonlyReleaseAssetFile)) {
return TaskResult.failure('Failed to copy read-only asset file');
}
modes = readonlyReleaseAssetFile.statSync().modeString();
print('\nread-only.txt file access modes = $modes');
if (modes != null && modes.compareTo(fileReadWriteMode) != 0) {
return TaskResult.failure('Failed to make assets user-readable and writable');
}
return TaskResult.success(null); return TaskResult.success(null);
} on TaskResult catch (taskResult) { } on TaskResult catch (taskResult) {
return taskResult; return taskResult;
......
...@@ -647,6 +647,15 @@ class FlutterPlugin implements Plugin<Project> { ...@@ -647,6 +647,15 @@ class FlutterPlugin implements Plugin<Project> {
extraGenSnapshotOptions extraGenSnapshotOptionsValue extraGenSnapshotOptions extraGenSnapshotOptionsValue
splitDebugInfo splitDebugInfoValue splitDebugInfo splitDebugInfoValue
treeShakeIcons treeShakeIconsOptionsValue treeShakeIcons treeShakeIconsOptionsValue
doLast {
project.exec {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
commandLine('cmd', '/c', 'attrib -r ${assetsDirectory}/* /s')
} else {
commandLine('chmod', '-R', 'u+w', assetsDirectory)
}
}
}
} }
File libJar = project.file("${project.buildDir}/${AndroidProject.FD_INTERMEDIATES}/flutter/${variant.name}/libs.jar") File libJar = project.file("${project.buildDir}/${AndroidProject.FD_INTERMEDIATES}/flutter/${variant.name}/libs.jar")
Task packFlutterAppAotTask = project.tasks.create(name: "packLibs${FLUTTER_BUILD_PREFIX}${variant.name.capitalize()}", type: Jar) { Task packFlutterAppAotTask = project.tasks.create(name: "packLibs${FLUTTER_BUILD_PREFIX}${variant.name.capitalize()}", type: Jar) {
...@@ -859,6 +868,10 @@ class FlutterTask extends BaseFlutterTask { ...@@ -859,6 +868,10 @@ class FlutterTask extends BaseFlutterTask {
return intermediateDir return intermediateDir
} }
String getAssetsDirectory() {
return "${outputDirectory}/flutter_assets"
}
CopySpec getAssets() { CopySpec getAssets() {
return project.copySpec { return project.copySpec {
from "${intermediateDir}" from "${intermediateDir}"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment