如何使Android应用程序自动配置调试与发布值?

时间:2021-03-21 23:24:48

I'm working on an Android app, specifically one that uses the Facebook Android SDK. In development mode, I'm working with a test Facebook app that goes by one ID. However, in release mode, the app will be working with second Facebook app with a different ID.

我正在开发Android应用程序,特别是使用Facebook Android SDK的应用程序。在开发模式中,我正在使用一个带有一个ID的测试Facebook应用程序。但是,在发布模式下,该应用程序将使用具有不同ID的第二个Facebook应用程序。

I'm wondering how most Android (or Java might be a suitable enough realm of knowledge) developers here go about having their app automatically build with debug vs. release values. An ideal solution does not involve a manual switch (e.g.: switching public static final DEBUG = false; to true) before building.

我想知道大多数Android(或Java可能是一个适当的知识领域)开发人员如何让他们的应用程序自动构建调试与发布值。在构建之前,理想的解决方案不涉及手动开关(例如:切换公共静态最终DEBUG = false;为真)。

6 个解决方案

#1


9  

It's been a while since you asked but I thought I'd share how I'm doing it.

你提问已经有一段时间,但我想我会分享我是怎么做的。

Like Sebastian hinted, an Ant script can handle that change for you and generate the static final constants that you're looking for. You can configure IntelliJ or Eclipse to make it almost seamless.

就像Sebastian暗示的那样,Ant脚本可以为您处理这种变化并生成您正在寻找的静态最终常量。您可以配置IntelliJ或Eclipse以使其几乎无缝。

I tried to detail the different steps I took over here, let me know if it helps. I know I never have to make any manual changes before releasing, and it's a nice relief!

我试着详细说明我在这里采取的不同步骤,让我知道它是否有帮助。我知道在发布之前我永远不需要做任何手动更改,这是一个很好的解脱!

#2


6  

In eclipse ADT 17.0 and above there is a new feature for this. Check the BuildConfig.DEBUG that is automatically built with your code.

在eclipse ADT 17.0及更高版本中,有一个新功能。检查使用您的代码自动构建的BuildConfig.DEBUG。

For more information see http://developer.android.com/sdk/eclipse-adt.html

有关更多信息,请参阅http://developer.android.com/sdk/eclipse-adt.html

#3


5  

I can't recommend the IMEI method... the main problem with it is that not all Android devices will have IMEIs. A better way is to examine the signature used to sign the .apk.

我不推荐使用IMEI方法......主要问题是并非所有Android设备都有IMEI。更好的方法是检查用于签署.apk的签名。

// See if we're a debug or a release build
try {
    PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
    if (packageInfo.signatures.length>0) {
        String signature = new String(packageInfo.signatures[0].toByteArray());
        isReleaseBuild = !signature.contains("Android Debug");
    }
} catch (NameNotFoundException e1) {
    e1.printStackTrace();
}

#4


1  

I use a slightly more mundane method (in case you're still interested in solutions).

我使用了一种稍微平凡的方法(如果你仍然对解决方案感兴趣)。

At application launch my application checks for the existence of a text file stored in /sdcard/. Each application I have looks for a specific file like "applicationdebug.txt". If the file exists, then the application goes into debug mode and starts being verbose with log statements and using my debug Facebook key, etc.

在应用程序启动时,我的应用程序检查是否存在/ sdcard /中存储的文本文件。我在每个应用程序中查找特定文件,如“applicationdebug.txt”。如果该文件存在,则应用程序进入调试模式并开始使用日志语句和使用我的调试Facebook密钥等进行详细操作。

Then I simply remove (or rename) the file to on the device to see how the application performs in release mode.

然后,我只需将文件移除(或重命名)到设备上,即可查看应用程序在发布模式下的执行情况。

#5


0  

Usually you will use 1 or 2 devices for debugging only. So you can set the DEBUG switch based on the Devices? So you can simply use the IMEI.

通常,您将仅使用1个或2个设备进行调试。那你可以根据设备设置DEBUG开关吗?所以你可以简单地使用IMEI。

  1. add a new Application class to your project and have it initialize the field (suspect to put it in a Const class).

    向项目中添加一个新的Application类并让它初始化该字段(怀疑将它放在Const类中)。

    In your Applications onCreate method, call Const.setupDebug(getApplicationContext());

    在Applications onCreate方法中,调用Const.setupDebug(getApplicationContext());

  2. Implement the setupDebug like this

    像这样实现setupDebug

    public class Const {

    公共类Const {

    private static boolean debug = false;
    
    
    public static boolean isDebug() {
        return debug;
    }
    
    
    private static void setDebug(boolean debug) {
        Const.debug = debug;
    }
    
    
    private static String [] DEBUG_DEVICES = new String[] {
            "000000000000000", "gfjdhsgfhjsdg" // add ur devices
    };
    
    
    public static void setupDebug(Context context) {
        Arrays.sort(DEBUG_DEVICES);
    
    
    
    TelephonyManager mTelephonyMgr = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    String imei = mTelephonyMgr.getDeviceId();
    if (imei == null) imei = "000000000000000";
    
    
    if(Arrays.binarySearch(DEBUG_DEVICES, imei) > -1) {
        setDebug(true);
    }
    
    }

    }

    }

  3. Switch from constant field to constant Method.

    从常量字段切换到常量方法。

    Const.isDebug()

    Const.isDebug()

#6


0  

With Eclipse I create 3 projects in the workspace :

使用Eclipse,我在工作区中创建了3个项目:

  • ApplicationProject
  • ApplicationProject

It is a library project Contain all source code In values/refs.xml I add

它是一个库项目包含所有源代码在values / refs.xml中我添加

<bool name="debug_mode">true</bool>
  • ApplicationProjectDEBUG
  • ApplicationProjectDEBUG

Use ApplicationProject Overide AndroidManifest and other xml file with developement specific config In values/refs.xml I add

使用ApplicationProject覆盖AndroidManifest和其他具有开发特定配置的xml文件在values / refs.xml中我添加

<bool name="debug_mode">true</bool>
  • ApplicationProjectPROD
  • ApplicationProjectPROD

Use ApplicationProject Overide AndroidManifest and other xml file with production specific config In values/refs.xml I add

使用ApplicationProject覆盖AndroidManifest和其他具有生产特定配置的xml文件在值/ refs.xml中我添加

<bool name="debug_mode">false</bool>

I signe APK from this project to put on the store

我从这个项目signe APK到商店

#1


9  

It's been a while since you asked but I thought I'd share how I'm doing it.

你提问已经有一段时间,但我想我会分享我是怎么做的。

Like Sebastian hinted, an Ant script can handle that change for you and generate the static final constants that you're looking for. You can configure IntelliJ or Eclipse to make it almost seamless.

就像Sebastian暗示的那样,Ant脚本可以为您处理这种变化并生成您正在寻找的静态最终常量。您可以配置IntelliJ或Eclipse以使其几乎无缝。

I tried to detail the different steps I took over here, let me know if it helps. I know I never have to make any manual changes before releasing, and it's a nice relief!

我试着详细说明我在这里采取的不同步骤,让我知道它是否有帮助。我知道在发布之前我永远不需要做任何手动更改,这是一个很好的解脱!

#2


6  

In eclipse ADT 17.0 and above there is a new feature for this. Check the BuildConfig.DEBUG that is automatically built with your code.

在eclipse ADT 17.0及更高版本中,有一个新功能。检查使用您的代码自动构建的BuildConfig.DEBUG。

For more information see http://developer.android.com/sdk/eclipse-adt.html

有关更多信息,请参阅http://developer.android.com/sdk/eclipse-adt.html

#3


5  

I can't recommend the IMEI method... the main problem with it is that not all Android devices will have IMEIs. A better way is to examine the signature used to sign the .apk.

我不推荐使用IMEI方法......主要问题是并非所有Android设备都有IMEI。更好的方法是检查用于签署.apk的签名。

// See if we're a debug or a release build
try {
    PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_SIGNATURES);
    if (packageInfo.signatures.length>0) {
        String signature = new String(packageInfo.signatures[0].toByteArray());
        isReleaseBuild = !signature.contains("Android Debug");
    }
} catch (NameNotFoundException e1) {
    e1.printStackTrace();
}

#4


1  

I use a slightly more mundane method (in case you're still interested in solutions).

我使用了一种稍微平凡的方法(如果你仍然对解决方案感兴趣)。

At application launch my application checks for the existence of a text file stored in /sdcard/. Each application I have looks for a specific file like "applicationdebug.txt". If the file exists, then the application goes into debug mode and starts being verbose with log statements and using my debug Facebook key, etc.

在应用程序启动时,我的应用程序检查是否存在/ sdcard /中存储的文本文件。我在每个应用程序中查找特定文件,如“applicationdebug.txt”。如果该文件存在,则应用程序进入调试模式并开始使用日志语句和使用我的调试Facebook密钥等进行详细操作。

Then I simply remove (or rename) the file to on the device to see how the application performs in release mode.

然后,我只需将文件移除(或重命名)到设备上,即可查看应用程序在发布模式下的执行情况。

#5


0  

Usually you will use 1 or 2 devices for debugging only. So you can set the DEBUG switch based on the Devices? So you can simply use the IMEI.

通常,您将仅使用1个或2个设备进行调试。那你可以根据设备设置DEBUG开关吗?所以你可以简单地使用IMEI。

  1. add a new Application class to your project and have it initialize the field (suspect to put it in a Const class).

    向项目中添加一个新的Application类并让它初始化该字段(怀疑将它放在Const类中)。

    In your Applications onCreate method, call Const.setupDebug(getApplicationContext());

    在Applications onCreate方法中,调用Const.setupDebug(getApplicationContext());

  2. Implement the setupDebug like this

    像这样实现setupDebug

    public class Const {

    公共类Const {

    private static boolean debug = false;
    
    
    public static boolean isDebug() {
        return debug;
    }
    
    
    private static void setDebug(boolean debug) {
        Const.debug = debug;
    }
    
    
    private static String [] DEBUG_DEVICES = new String[] {
            "000000000000000", "gfjdhsgfhjsdg" // add ur devices
    };
    
    
    public static void setupDebug(Context context) {
        Arrays.sort(DEBUG_DEVICES);
    
    
    
    TelephonyManager mTelephonyMgr = (TelephonyManager)
            context.getSystemService(Context.TELEPHONY_SERVICE);
    String imei = mTelephonyMgr.getDeviceId();
    if (imei == null) imei = "000000000000000";
    
    
    if(Arrays.binarySearch(DEBUG_DEVICES, imei) &gt; -1) {
        setDebug(true);
    }
    
    }

    }

    }

  3. Switch from constant field to constant Method.

    从常量字段切换到常量方法。

    Const.isDebug()

    Const.isDebug()

#6


0  

With Eclipse I create 3 projects in the workspace :

使用Eclipse,我在工作区中创建了3个项目:

  • ApplicationProject
  • ApplicationProject

It is a library project Contain all source code In values/refs.xml I add

它是一个库项目包含所有源代码在values / refs.xml中我添加

<bool name="debug_mode">true</bool>
  • ApplicationProjectDEBUG
  • ApplicationProjectDEBUG

Use ApplicationProject Overide AndroidManifest and other xml file with developement specific config In values/refs.xml I add

使用ApplicationProject覆盖AndroidManifest和其他具有开发特定配置的xml文件在values / refs.xml中我添加

<bool name="debug_mode">true</bool>
  • ApplicationProjectPROD
  • ApplicationProjectPROD

Use ApplicationProject Overide AndroidManifest and other xml file with production specific config In values/refs.xml I add

使用ApplicationProject覆盖AndroidManifest和其他具有生产特定配置的xml文件在值/ refs.xml中我添加

<bool name="debug_mode">false</bool>

I signe APK from this project to put on the store

我从这个项目signe APK到商店