直接从 Android Studio 进行测试时,应用运行良好,但在设备上作为 APK 安装时却无法运行
App works fine, when testing directly from Android Studio, but not when installed as APK on the Device
我编写了一个应用程序,通过 PHP/Mysqli 登录和改造。当我直接从 Android Studio(单击“运行”按钮)在我的智能手机上测试该应用程序时,登录工作正常!
但是当我生成一个签名的 apk 然后将它安装到我的智能手机上时,登录不起作用。我认为 MySQL 数据库的 Internet 连接有问题?
我搜索了一下,发现应该设置一个network_security_configuration。我已经设置好了,也给了
问题的解决方案是什么?
我为 PHP-Script (https://domain.tld/login.php).
使用了一个 HTTPS/SSL 连接到我自己的网络服务器
这是我的网络配置:
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
和Android-清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.uwbeinternational.weatherapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.UwBeWeatherApp"
android:networkSecurityConfig="@xml/network_security_config">
这里我使用 Retrofit 检查登录凭证:
class RetrofitClient {
private fun getRetrofitClient(): Retrofit {
return Retrofit.Builder()
.baseUrl("https://uwbeinternational.org/")
.addConverterFactory(GsonConverterFactory.create())
.build()
}
fun getInstance(): ApiClient {
return getRetrofitClient().create(ApiClient::class.java)
}
}
以及 ApiClient:
interface ApiClient {
@FormUrlEncoded
@POST("android/login_service.php")
fun login(
@Field("post_username") username : String,
@Field("post_password") password : String
): Call<ResponseLogin>
}
Proguard-Rules 有问题。所以,我现在将这些行添加到 proguard.pro 文件:
#Retrofit2
-keep class com.uwbeinternational.weatherapp.** { *; }
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
现在完美运行了!
我编写了一个应用程序,通过 PHP/Mysqli 登录和改造。当我直接从 Android Studio(单击“运行”按钮)在我的智能手机上测试该应用程序时,登录工作正常!
但是当我生成一个签名的 apk 然后将它安装到我的智能手机上时,登录不起作用。我认为 MySQL 数据库的 Internet 连接有问题?
我搜索了一下,发现应该设置一个network_security_configuration。我已经设置好了,也给了 问题的解决方案是什么?
我为 PHP-Script (https://domain.tld/login.php). 这是我的网络配置: 和Android-清单: 这里我使用 Retrofit 检查登录凭证: 以及 ApiClient: <?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.uwbeinternational.weatherapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.UwBeWeatherApp"
android:networkSecurityConfig="@xml/network_security_config">
class RetrofitClient {
private fun getRetrofitClient(): Retrofit {
return Retrofit.Builder()
.baseUrl("https://uwbeinternational.org/")
.addConverterFactory(GsonConverterFactory.create())
.build()
}
fun getInstance(): ApiClient {
return getRetrofitClient().create(ApiClient::class.java)
}
}
interface ApiClient {
@FormUrlEncoded
@POST("android/login_service.php")
fun login(
@Field("post_username") username : String,
@Field("post_password") password : String
): Call<ResponseLogin>
}
Proguard-Rules 有问题。所以,我现在将这些行添加到 proguard.pro 文件:
#Retrofit2
-keep class com.uwbeinternational.weatherapp.** { *; }
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
现在完美运行了!