Search code examples
phpandroidkotlinauthenticationretrofit2

App works fine, when testing directly from Android Studio, but not when installed as APK on the Device


I have programmed an App with a Login over PHP/Mysqli and Retrofit. When I test the App on my Smartphone directly from Android Studio (click the "Run"-Button), the Login is working perfectly!

But when I generate a signed apk and then install it on my Smartphone, the Login is not working. I think I have a Problem with Internet-Connection to the MySQL Database?

I have searched and found, that I should set a network_security_configuration. I have setup and also given <uses-permission... INTERNET. But it doesn't work, only when I test directly from Android Studio on my Device.

What could be the solution for the problem? I used an HTTPS/SSL Connection to my own Webserver for the PHP-Script (https://domain.tld/login.php).

Here is my Network-Config:

    <?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <base-config cleartextTrafficPermitted="true" />
</network-security-config>

And Android-Manifest:

<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">

Here I use Retrofit to check Login-Credentials:

    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)
    }
}

And the ApiClient:

    interface ApiClient {

    @FormUrlEncoded
    @POST("android/login_service.php")
    fun login(
        @Field("post_username") username : String,
        @Field("post_password") password : String
    ): Call<ResponseLogin>
}

Solution

  • It was a Problem with Proguard-Rules. So, i have now Add these lines to the proguard.pro File:

    #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.**
    

    Now it works perfectly!