代码之家  ›  专栏  ›  技术社区  ›  Joe Holloway

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

  •  54
  • Joe Holloway  · 技术社区  · 16 年前

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

    8 回复  |  直到 11 年前
        1
  •  32
  •   Community Mohan Dere    9 年前

    据此, post ,在SDK工具版本17中(截至撰写本文时,我们的版本为19),添加了 BuildConfig.DEBUG 常量,该常量在构建开发人员构建时为true。

        2
  •  44
  •   Viktor BreÅ¡an    16 年前

    以下解决方案假定在清单文件中始终设置 android:debuggable=true 在开发和 android:debuggable=false 用于应用程序发布。

    ApplicationInfo.FLAG_DEBUGGABLE 国旗 ApplicationInfo 从 PackageManager

    以下代码段可能会有所帮助:

    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
  •   Eron Villarreal    15 年前

    @viktor bresan感谢您提供了一个有用的解决方案。如果您只是提供了一种检索当前应用程序上下文的通用方法,使其成为一个完全可以工作的示例,这将更有帮助。大致如下:

    PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
    
        4
  •  8
  •   Community Mohan Dere    6 年前

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

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

    现在您可以访问如下所示的变量

        if (BuildConfig.IS_DEBUG_MODE) { {
            //Debug mode.
        } else {
            //Release mode
        }
    
        5
  •  4
  •   Joseph    16 年前
        6
  •  3
  •   prayagupa soxunyi    14 年前

    下面的代码怎么样。。。

    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;
    
    } 
    
        7
  •  1
  •   Jeff S    15 年前

    今天我偶然发现了另一种方法,它看起来非常直截了当。。看看Build.TAGS,当为开发创建应用程序时,它的计算结果是字符串“testkeys”。

    没有比字符串比较更容易的了。

        8
  •  0
  •   wirbly    16 年前