简体   繁体   中英

Android login screen works on Android Studio debug apk but not when downloaded from Play Store

My login screen runs fine when I test on the phone connected to android studio. When I upload it to the Play Store and download in the other phone, it won't work. When I press login, I get, immediately the result of execution of

toast("login: " + loginResult + " ok: " + false);

and I do not get the result of

toast("fazendo login");

Which indicates that the async task is returning immediately, it doesn't even try do login. Why this happens? I can't debug because it happens only on the downloaded app from play store.

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import venko.Venko;

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "LoginActivity";

    TextView serverView;
    TextView passwordView;
    Button loginButton;
    Venko venkoAPI;
    String server;
    String password;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        venkoAPI = new Venko();

        serverView = findViewById(R.id.server);
        passwordView = findViewById(R.id.password);
        loginButton = findViewById(R.id.login);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                server = serverView.getText().toString();
                password = passwordView.getText().toString();
                new LoginTask().execute();
            }
        });
        toast("faça login no servidor");
    }
    public void toast(final String text) {
        runOnUiThread(new Runnable() {
            public void run() {
                Toast.makeText(getBaseContext(), text, Toast.LENGTH_LONG).show();
            }
        });
    }
    private class LoginTask extends AsyncTask<Integer, Void, Boolean> {

        private LoginTask() {

        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(Integer... params) {
            try {
                toast("fazendo login");

                venkoAPI.setPassword(password);
                venkoAPI.setAddress(server);
                Log.d(TAG, "doing login to " + server + " with password " + password);
                return venkoAPI.doLogin();
            } catch (Exception exception) {
                Log.d(TAG, exception.toString());
                toast(exception.toString());
                return false;
            }
        }

        @Override
        protected void onPostExecute(Boolean loginResult) {
            super.onPostExecute(loginResult);
            SharedPreferences sharedPref = getSharedPreferences("login", Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPref.edit();
            boolean ok = loginResult;
            if (ok) {
                Log.d(TAG, "saving password info: " + serverView.getText().toString() + " and " + passwordView.getText().toString());
                editor.putString("server", serverView.getText().toString());
                editor.putString("password", passwordView.getText().toString());
                editor.commit();
                //go to main activity
                Intent intent = new Intent(LoginActivity.this, MainViewPager.class);
                startActivity(intent);
            } else {
                Log.d(TAG, "login did not work");
                toast("login: " + loginResult + " ok: " + false);
            }
        }
    }
}

The issue occurs in release build try using proguard-rules

Add this line to your proguard-rules.pro

-keep class YOUR_PACKAGE_NAME.LoginActivity { *; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM