[TOC]

1.长时间未更新的库时遇到的问题

我在使用image_gallery_saver库时遇到了这个问题,在未更新Flutter版本之前,并没有出现该问题。

报错信息如下

1
2
3
4
5
Could not create an instance of type com.android.build.api.variant.impl.LibraryVariantBuilderImpl.

Namespace not specified. Specify a namespace in the module's build file.See https://d.android.com/r/tools/upgrade-assistant/set-namespace for information about setting the namespace.

If you've specified the package attribute in the source AndroidManifest.xml, you can use the AGP Upgrade Assistant to migrate to the namespace value in the build file. Refer to https://d.android.com/r/tools/upgrade-assistant/agp-upgrade-assistant for general information about using the AGP Upgrade Assistant.

解决办法–参考

找到插件所在路径

1
flutter\Pub_Cache\hosted\pub.flutter-io.cn\image_gallery_saver-2.0.3

打开以下文件

1
\android\build.gradle

添加

1
2
3
4
android {
namespace "com.example.imagegallerysaver" // Add this line
...
}

然后在

1
\android\src\main\AndroidManifest.xml

删除

1
package="com.example.imagegallerysaver"

上述错误的伴生错误

1
2
3
Execution failed for task ':image_gallery_saver:compileReleaseKotlin'

compileReleaseJavaWithJavac' task (current target is 1.8) and 'compileReleaseKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.

解决–参考

打开

1
\android\build.gradle

添加

1
2
3
4
5
6
android{
//添加下列代码
kotlinOptions {
jvmTarget = '1.8'
}
}

其它办法

使用其它库替代,如image_gallery_saver_plus替代image_gallery_saver

2.安卓所有文件访问权限问题

1
PathAccessException: Creation failed, path = '/storage/emulated/0/xxxx/' (OS Error: Permission denied, errno = 13)

解决办法

打开

1
AndroidManifest.xml

添加

1
2
3
4
5
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

以及<application>

1
android:requestLegacyExternalStorage="true"

然后在这里使用permission_handler申请权限

1
Permission.manageExternalStorage

系统就会自动跳转设置界面

3.加载网页或图片问题

加载不出HTTP链接的图片或打开HTTP网页显示

1
ERR_CLEARTEXT_NOT_PERMITTED

原因:Android 9及更高版本,默认情况下,禁止应用程序通过非安全的明文HTTP连接进行网络通信。

解决

AndroidManifest.xml添加

1
2
3
4
5
6

<application
android:usesCleartextTraffic="true"
...>
...
</application>

4.requires a placeholder substitution but no value for applicationName is provided.–参考

1
Error:Attribute application@name at AndroidManifest.xml:5:9-42 requires a placeholder substitution but no value for <applicationName> is provided.

解决

android/app/build.gradle添加

1
2
3
4
5
6
7
8
9
10
11
12
13
buildTypes {
release {
manifestPlaceholders = [applicationName: "android.app.Application"]
}

debug {
manifestPlaceholders = [applicationName: "android.app.Application"]
}

build {
manifestPlaceholders = [applicationName: "android.app.Application"]
}
}

5.Dart使用http库传json参数–参考

需要创建Map并将Map对象转换为JSON字符串再发送POST请求

1
2
3
4
5
6
7
8
9
10
11
12
13
Map<String, dynamic> params = {
'name': 'John',
'age': 25,
};

// 将Map对象转换为JSON字符串
String jsonParams = jsonEncode(params);

var response = await http.post(
Uri.parse('http://example.com'),
headers: headers,
body: jsonParams,
);

6.极光推送使用curl调用REST API出现错误码1002–参考

1
{"error":{"code":1002,"message":"Missing parameter"}}

这是因为 windows版本下单引号要改成双引号,json格式数据中双引号要加\转义

例如

1
2
3
curl --insecure -X POST -v https://api.jpush.cn/v3/push -H "Content-Type: application/json"
-u "appKey:masterSecret"
-d '{"platform":"all","audience":"all","notification":{"alert":"Hi,JPush!"}}'

改成

1
2
3
curl --insecure -X POST -v https://api.jpush.cn/v3/push -H "Content-Type: application/json"
-u "appKey:masterSecret"
-d "{\"platform\":\"all\",\"audience\":\"all\",\"notification\":{\"alert\":\"Hi,JPush!\"}}"

7.Unable to locate Android SDK

1
flutter config --android-sdk "\SDK"

8.系统尚未启用开发者模式–参考

1
2
Building with plugins requires symlink support.
Please enable Developer Mode in your system settings. Run start ms-settings:developers to open settings

这个错误表示你的系统尚未启用开发者模式,所以无法使用Flutter的插件功能。
Flutter插件会通过符号链接的方式与Flutter工程连接,所以需要启用开发者模式和符号链接支持

解决

打开设置,选择更新和安全-开发者选项

打开开发人员模式

9.flutter编译乱码

环境变量添加

1
2
JAVA_TOOL_OPTIONS
-Dfile.encoding=UTF-8 -Dsun.jnu.encoding=UTF-8