HTML5 a situation where the Android local method cannot be invoked (due to confusion)

Source: Internet
Author: User

Recently, while developing a module, an activity nested the HTML5 page, but found that the debug package was able to invoke the Android local method normally, while the release package could not invoke the same local method. The
code is as follows:

public class Flaggingactivity extends Appbaseactivity {@Initialize Navigatebar navigatebar;

    @Initialize WebView WebView;

    String URL = netcomment.getins (). GetH5 () + "/html/fedback/index.html?";
    String Strid;
    String Strtel;
    String strtype;
        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);

        Intent intent=getintent ();
        Strid = Intent.getstringextra ("id");
        Strtel = Intent.getstringextra ("tel");
        Strtype = Intent.getstringextra ("type");
        url = url + "id=" +strid+ "&type=" +strtype+ "&tel=" +strtel+ "&device=2";
        Setcontentview (r.layout.activity_flagging);

        Findallviewbyrid (R.id.class);
        WebSettings websetting = Webview.getsettings ();
        Set JS available websetting.setjavascriptenabled (TRUE);
        Add JS Call Interface Webview.addjavascriptinterface (this, "Android");
    Webview.getsettings (). Setjavascriptenabled (True);    Webview.setwebviewclient (New Webviewclient () {public Boolean shouldoverrideurlloading (WebView view, Strin
                G URL) {view.loadurl (URL);
            return true;
        }
        });
    Webview.loadurl (URL);
    @JavascriptInterface public void Close (String token) {//closes the current Activity this.finish (); }


}

As you can see from the code above, I'm here to embed a webview,webview content in an activity that points to an HTML page. Then in the HTML page through JS to reverse the call to the Android local Close method.
However, here is the problem: the Debug package can invoke the Android local method normally, while the release package cannot invoke the same local method.
After the investigation, I found: In my Android project, the debug package is not confusing, and the release package is confusing, and the problem is indeed confusing. The confusion of the role is: in order to prevent the results of their own work by others to steal, confusing code can effectively prevent being decompile. Similarly, because the obfuscation function is turned on, the local close () method, which may not be the function name of close () after being confused, causes our JS to not find this method and cannot invoke the close () method.
The corresponding solution is to let the close () method not participate in confusion, I have a slightly larger scope here, is directly to the current class settings can not be confused, as follows (add in Proguard-rules.pro file):

#html交互本地方法不能被混淆
-keepclassmembernames class Com.android.app.activity.house.FlaggingActivity {*;}

After adding the above code block, our release package can also find the close () method.

In fact, the problem is well resolved, the reason is recorded here is that in general, the debug package is not confusing, and the release package is usually used as an online package to be confused. But we usually use the debug package when we test, and the test of the release package is usually taken lightly when the debug package is tested correctly. When we use JS to invoke the local method or reflection, this is not allowed to play. So it is also required that we be on the line when the release package is also a relatively complete test to reduce similar errors.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.