应用程序停止使用已签名的 APK Playstore 版本,但可以使用来自 Android Studio 的已编译版本

App stops working with signed APK playstore version but does work from the compiled one from Android Studio

当我登录时间表页面时,从 android studio 下载到我的 phone 的版本,它工作正常并带我到时间表。但是,当我尝试从从 Playstore 下载的版本登录时间表页面时,它只会永远加载。

那么我是否可以通过在控制台中查看消息来测试从 Playstore 下载的版本? (https://play.google.com/store/apps/details?id=com.gmail.gogobebe2.thedayahead)

这是完整的源代码:https://github.com/gogobebe2/TheDayAhead

这是它停止并且永远不会登录的地方 - 登录按钮 onclick 侦听器:(https://github.com/gogobebe2/TheDayAhead/blob/master/app/src/main/java/com/gmail/gogobebe2/thedayahead/timetable/TimetableFragment.java#L192):

    @SuppressLint({"AddJavascriptInterface", "SetJavaScriptEnabled"})
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.login_button) {
            if (kmarDocument == null) initKmarLoginConnection();
            else {
                EditText usernameEditText = (EditText) loginRelativeLayout.findViewById(R.id.editText_username);
                EditText passwordEditText = (EditText) loginRelativeLayout.findViewById(R.id.editText_password);

                updateLoginPreferences((CheckBox) loginRelativeLayout.findViewById(R.id.checkBox_rememberMe),
                        usernameEditText, passwordEditText);

                WebView webView = new WebView(getContext());

                webView.setVisibility(View.INVISIBLE);

                webView.clearCache(true);
                webView.clearHistory();
                clearCookies(this);

                webView.addJavascriptInterface(new HTMLRetrieverJavaScriptInterface(this), "HtmlRetriever");

                webView.setWebViewClient(new WebViewClient() {

                    @Override
                    public void onLoadResource(WebView webView, String destinationUrl) {
                        CheckBox rememberMeCheckbox = (CheckBox) loginRelativeLayout.findViewById(R.id.checkBox_rememberMe);
                        ProgressBar progressBar = (ProgressBar) loginRelativeLayout.findViewById(R.id.progressBar);
                        progressBar.setVisibility(View.VISIBLE);
                        rememberMeCheckbox.setVisibility(View.INVISIBLE);
                        super.onLoadResource(webView, destinationUrl);
                    }

                    @Override
                    public void onPageFinished(WebView webView, String urlLoaded) {
                        final String LOGIN_JAVASCRIPT = "javascript:document.getElementById(\"loginSubmit\").click()";
                        final String HTML_RETRIEVER_JAVASCRIPT = "javascript:window.HtmlRetriever.showHTML" +
                                "('<html>' + document.getElementsByTagName('html')[0].innerHTML + '</html>');";
                        if (urlLoaded.equals(KMAR_TIMETABLE_URL)) {
                            // debug: webView.setVisibility(View.VISIBLE);
                            webView.loadUrl(HTML_RETRIEVER_JAVASCRIPT);
                        } else if (!urlLoaded.equals(LOGIN_JAVASCRIPT)) {
                            webView.loadUrl(LOGIN_JAVASCRIPT);
                        }
                    }
                });

                WebSettings webSettings = webView.getSettings();
                webSettings.setJavaScriptEnabled(true);

                loginRelativeLayout.addView(webView);

                Element loginUsernameElement = kmarDocument.getElementById("loginUsername");
                Element loginPasswordElement = kmarDocument.getElementById("loginPassword");

                loginUsernameElement.attr("value", usernameEditText.getText().toString());
                loginPasswordElement.attr("value", passwordEditText.getText().toString());

                webView.loadData(kmarDocument.html(), "text/html", "UTF-8");

                // I then call the click() function on the loginSubmit button when the page is finished
                // loading in the overridden onPageFinished(WebView webView, String url) method.
            }
        }
}

这是混淆文件。我不知道我这样做是否正确,但我在其他类似于我的堆栈溢出问题上看到问题通常出在这个问题上。 (https://github.com/gogobebe2/TheDayAhead/blob/master/app/proguard-rules.pro):

# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /home/william/Android/Sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
-keepclassmembers class com.gmail.gogobebe2.thedayahead.timetable.TimetableFragment$HTMLRetrieverJavaScriptInterface {
   public *;
}

这是 Javascript 接口 (https://github.com/gogobebe2/TheDayAhead/blob/master/app/src/main/java/com/gmail/gogobebe2/thedayahead/timetable/TimetableFragment.java#L177):

public class HTMLRetrieverJavaScriptInterface {
        TimetableFragment timetableFragment;

        HTMLRetrieverJavaScriptInterface(TimetableFragment timetableFragment) {
            super();
            this.timetableFragment = timetableFragment;
        }

        @JavascriptInterface
        void showHTML(String html) {
            kmarTimetableHTML = html;
            MainActivity.timetable = new Timetable(kmarTimetableHTML, timetableFragment);
        }
}

编辑:build.gradle 中,我将 minifyEnabledfalse 更改为 true。现在,使用从 Play 商店下载的应用程序,当我导航到时间表页面时,该应用程序完全崩溃了。但是,如果我使用从 android studio 下载的那个,它工作正常。

自己解决了。

新Javascript界面class:

    @Keep
    public class HTMLRetrieverJavaScriptInterface {
        TimetableFragment timetableFragment;

        public HTMLRetrieverJavaScriptInterface(TimetableFragment timetableFragment) {
            super();
            this.timetableFragment = timetableFragment;
        }

        @Keep
        @JavascriptInterface
        public void showHTML(String html) {
            kmarTimetableHTML = html;
            MainActivity.timetable = new Timetable(kmarTimetableHTML, timetableFragment);
        }
}

新建亲卫档案:

-keepclassmembers class com.gmail.gogobebe2.thedayahead.timetable.TimetableFragment$HTMLRetrieverJavaScriptInterface {
   public *;
}
-keepattributes JavascriptInterface