如何在Android APP中隐藏API URL和参数? [英] How to hide API URL and parameters in Android APP?

查看:257
本文介绍了如何在Android APP中隐藏API URL和参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,如果没有用于混淆的商业产品,有什么方法可以安全地存储API URL和参数,而这些参数不能在逆向工程中进行编译?我已经尝试了所有应用程序,并且它们的API网址和代码易于阅读.我担心安全性.

I'm curious to know that without commercial product for obfuscation, is there any way where I can store API url and parameters safely which cannot be compiled in reverse engineering? I have tried all my apps and their API url and code is easy to read. I'm concerned about security.

推荐答案

在环境变量,BuildConfig和Android Studio中隐藏Url

避免这种不良做法的一种简单方法是存储您的值 在一个环境变量中,所以只有您的机器知道它,然后 以某种方式读取此值,然后在构建时将其注入您的代码中 时间.让我们看看如何使用Android Studio,Gradle和 BuildConfig.

One simple way to avoid this bad practice is to store your values inside an environmental variable, so only your machine knows it, then read this values in some way and inject them in your code at build time. Let’s see how to do that using Android Studio, Gradle, and BuildConfig.

首先,我们需要创建这些环境变量.在Linux和Mac中, 创建或编辑文件〜/.gradle/gradle.properties(注意 Gradle用户主目录的实际位置)并添加一些值:

First, we need to create these environmental vars. In Linux and Mac, create or edit the file ~/.gradle/gradle.properties (pay attention to the actual Gradle User Home directory position) and add some values:

WEBServiceBaseURL="http://192.168.2.102:2323/"
WEBServiceBaseSMSURL="https://www.example.com/"

第二,在模块的build.gradle文件中,添加以下行

Second, in your module’s build.gradle file, add these lines

//Add these lines
def Base_URL = '"' + WEBServiceBaseURL + '"' ?: '"Define BASE URL"';
def SMS_Base_URL = '"' + WEBServiceBaseSMSURL + '"' ?: '"Define SMS BASE URL"';

android.buildTypes.each { type ->
    type.buildConfigField 'String', 'Base_URL', WEBServiceBaseURL
    type.buildConfigField 'String', 'SMS_Base_URL', WEBServiceBaseSMSURL
}

在Java File Like中使用

Use in Java File Like

BuildConfig.Base_URL ,它将返回URL字符串

BuildConfig.Base_URL it will return URL String

  public static Retrofit getClient() {
        if (retrofit==null) {
            retrofit =new Retrofit.Builder()
                    .baseUrl(BuildConfig.Base_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

这篇关于如何在Android APP中隐藏API URL和参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆