What is the best way to store api-urls in android

2019-02-07 17:03发布

问题:

I want to store the api urls somewhere , if I require I can change easily. If I keep in my server still I need url to access server which requires authentication. If I store in assets then there is a chance of stealing it by decompiling the apk. I don't want to hardcode urls in code or String.xml. Please suggest the best way where I can keep safe and change url easily. Thanks !!

回答1:

The best way in your case is store api url's in server because you can change them later. You'd create one url to fetch your api urls. Build your app based on an architecture like this.

[
    {url: "/user/login", type:"login"},
    {url: "/user/register", type:"register"}
]

And don't forget anyone can see your server urls. It even doesn't have to decompiled because there are tools to watch device's network traffic:

  • Wireshark
  • Packet Capture


回答2:

make one Globalvariable.class in that you can add links.

String link="http:/---";

and access them any where in project like

String url=Globalvairable.link;

simple.



回答3:

Use google tagmanager for saving url in json format and use that in your app. Its secure to save as it is encrypted and admin can change the url on the fly whenever needed.



回答4:

use buildConfigField in build.gradle :)

More info in https://developer.android.com/studio/build/gradle-tips?hl=es-419