区分Android上的开发模式和发布模式环境设置

时间:2022-02-12 23:29:31

I'm building an Android application and would like to maintain a few environment variables that I can tweak depending on whether I'm in development mode or release mode. For example, I need to invoke a web service and the URL will be slightly different in either mode. I'd like to externalize this and other settings so I can change them easily based on my target deployment.

我正在构建一个Android应用程序,并希望维护一些我可以调整的环境变量,具体取决于我是处于开发模式还是发布模式。例如,我需要调用Web服务,并且在任一模式下URL都会略有不同。我想将此设置和其他设置外部化,以便我可以根据目标部署轻松更改它们。

Are there any best practices or anything in the SDK to assist with this need?

SDK中是否有任何最佳实践或任何内容可以帮助满足此需求?

8 个解决方案

#1


28  

According to this * post, in SDK Tools version 17 (we're on 19 as of this writing) adds a BuildConfig.DEBUG constant that is true when building a dev build.

根据这个*文章,在SDK Tools版本17中(我们在撰写本文时已经是19)添加了一个BuildConfig.DEBUG常量,该常量在构建开发构建时是正确的。

#2


44  

The following solution assumes that in manifest file you always set android:debuggable=true while developing and android:debuggable=false for application release.

以下解决方案假设在清单文件中,您始终在开发时设置android:debuggable = true,并为应用程序版本设置android:debuggable = false。

Now you can check this attribute's value from your code by checking the ApplicationInfo.FLAG_DEBUGGABLE flag in the ApplicationInfo obtained from PackageManager.

现在,您可以通过检查从PackageManager获取的ApplicationInfo中的ApplicationInfo.FLAG_DEBUGGABLE标志来检查代码中此属性的值。

The following code snippet could help:

以下代码段可以提供帮助:

PackageInfo packageInfo = ... // get package info for your context
int flags = packageInfo.applicationInfo.flags; 
if ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
    // development mode
} else {
    // release mode
}

#3


9  

@viktor-bresan Thanks for a useful solution. It'd be more helpful if you just included a general way to retrieve the current application's context to make it a fully working example. Something along the lines of the below:

@ viktor-bresan感谢您提供有用的解决方案。如果您只是包含一般方法来检索当前应用程序的上下文以使其成为一个完全可用的示例,那将会更有帮助。下面的内容:

PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);

#4


4  

I would check out isDebuggerConnected

我会查看isDebuggerConnected

#5


3  

How about something like the code below ...

下面的代码怎么样......

public void onCreate Bundle b ) {
   super.onCreate(savedInstanceState);
   if ( signedWithDebugKey(this,this.getClass()) ) {
     blah blah blah
   }

  blah 
    blah 
      blah

}

static final String DEBUGKEY = 
      "get the debug key from logcat after calling the function below once from the emulator";    


public static boolean signedWithDebugKey(Context context, Class<?> cls) 
{
    boolean result = false;
    try {
        ComponentName comp = new ComponentName(context, cls);
        PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(),PackageManager.GET_SIGNATURES);
        Signature sigs[] = pinfo.signatures;
        for ( int i = 0; i < sigs.length;i++)
        Log.d(TAG,sigs[i].toCharsString());
        if (DEBUGKEY.equals(sigs[0].toCharsString())) {
            result = true;
            Log.d(TAG,"package has been signed with the debug key");
        } else {
            Log.d(TAG,"package signed with a key other than the debug key");
        }

    } catch (android.content.pm.PackageManager.NameNotFoundException e) {
        return false;
    }

    return result;

} 

#6


3  

Android build.gradle has Handles Debug and Release Environment well.

Append the following code snippet in build.gradle file

在build.gradle文件中附加以下代码段

buildTypes {
    debug {
        buildConfigField "Boolean", "IS_DEBUG_MODE", 'true'
    }

    release {
        buildConfigField "Boolean", "IS_DEBUG_MODE", 'false'
    }
}

Now you can access the variable like below

现在您可以访问如下变量

    if (BuildConfig.IS_DEBUG_MODE) { {
        //Debug mode.
    } else {
        //Release mode
    }

#7


1  

I came across another approach today by accident that seems really straight forward.. Look at Build.TAGS, when the app is created for development this evaluates to the String "test-keys".

我偶然遇到了另一种方法,看起来非常简单。看看Build.TAGS,当为开发创建应用程序时,这将评估为字符串“test-keys”。

Doesn't get much easier than a string compare.

没有比字符串比较容易得多。

Also Build.MODEL and Build.PRODUCT evaluate to the String "google_sdk" on the emulator!

此外,Build.MODEL和Build.PRODUCT评估模拟器上的字符串“google_sdk”!

#8


0  

Here's the method I use:

这是我使用的方法:

http://whereblogger.klaki.net/2009/10/choosing-android-maps-api-key-at-run.html

http://whereblogger.klaki.net/2009/10/choosing-android-maps-api-key-at-run.html

I use it to toggle debug logging and the maps API key.

我用它来切换调试日志和地图API密钥。

#1


28  

According to this * post, in SDK Tools version 17 (we're on 19 as of this writing) adds a BuildConfig.DEBUG constant that is true when building a dev build.

根据这个*文章,在SDK Tools版本17中(我们在撰写本文时已经是19)添加了一个BuildConfig.DEBUG常量,该常量在构建开发构建时是正确的。

#2


44  

The following solution assumes that in manifest file you always set android:debuggable=true while developing and android:debuggable=false for application release.

以下解决方案假设在清单文件中,您始终在开发时设置android:debuggable = true,并为应用程序版本设置android:debuggable = false。

Now you can check this attribute's value from your code by checking the ApplicationInfo.FLAG_DEBUGGABLE flag in the ApplicationInfo obtained from PackageManager.

现在,您可以通过检查从PackageManager获取的ApplicationInfo中的ApplicationInfo.FLAG_DEBUGGABLE标志来检查代码中此属性的值。

The following code snippet could help:

以下代码段可以提供帮助:

PackageInfo packageInfo = ... // get package info for your context
int flags = packageInfo.applicationInfo.flags; 
if ((flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
    // development mode
} else {
    // release mode
}

#3


9  

@viktor-bresan Thanks for a useful solution. It'd be more helpful if you just included a general way to retrieve the current application's context to make it a fully working example. Something along the lines of the below:

@ viktor-bresan感谢您提供有用的解决方案。如果您只是包含一般方法来检索当前应用程序的上下文以使其成为一个完全可用的示例,那将会更有帮助。下面的内容:

PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);

#4


4  

I would check out isDebuggerConnected

我会查看isDebuggerConnected

#5


3  

How about something like the code below ...

下面的代码怎么样......

public void onCreate Bundle b ) {
   super.onCreate(savedInstanceState);
   if ( signedWithDebugKey(this,this.getClass()) ) {
     blah blah blah
   }

  blah 
    blah 
      blah

}

static final String DEBUGKEY = 
      "get the debug key from logcat after calling the function below once from the emulator";    


public static boolean signedWithDebugKey(Context context, Class<?> cls) 
{
    boolean result = false;
    try {
        ComponentName comp = new ComponentName(context, cls);
        PackageInfo pinfo = context.getPackageManager().getPackageInfo(comp.getPackageName(),PackageManager.GET_SIGNATURES);
        Signature sigs[] = pinfo.signatures;
        for ( int i = 0; i < sigs.length;i++)
        Log.d(TAG,sigs[i].toCharsString());
        if (DEBUGKEY.equals(sigs[0].toCharsString())) {
            result = true;
            Log.d(TAG,"package has been signed with the debug key");
        } else {
            Log.d(TAG,"package signed with a key other than the debug key");
        }

    } catch (android.content.pm.PackageManager.NameNotFoundException e) {
        return false;
    }

    return result;

} 

#6


3  

Android build.gradle has Handles Debug and Release Environment well.

Append the following code snippet in build.gradle file

在build.gradle文件中附加以下代码段

buildTypes {
    debug {
        buildConfigField "Boolean", "IS_DEBUG_MODE", 'true'
    }

    release {
        buildConfigField "Boolean", "IS_DEBUG_MODE", 'false'
    }
}

Now you can access the variable like below

现在您可以访问如下变量

    if (BuildConfig.IS_DEBUG_MODE) { {
        //Debug mode.
    } else {
        //Release mode
    }

#7


1  

I came across another approach today by accident that seems really straight forward.. Look at Build.TAGS, when the app is created for development this evaluates to the String "test-keys".

我偶然遇到了另一种方法,看起来非常简单。看看Build.TAGS,当为开发创建应用程序时,这将评估为字符串“test-keys”。

Doesn't get much easier than a string compare.

没有比字符串比较容易得多。

Also Build.MODEL and Build.PRODUCT evaluate to the String "google_sdk" on the emulator!

此外,Build.MODEL和Build.PRODUCT评估模拟器上的字符串“google_sdk”!

#8


0  

Here's the method I use:

这是我使用的方法:

http://whereblogger.klaki.net/2009/10/choosing-android-maps-api-key-at-run.html

http://whereblogger.klaki.net/2009/10/choosing-android-maps-api-key-at-run.html

I use it to toggle debug logging and the maps API key.

我用它来切换调试日志和地图API密钥。