检测是否从Android电子市场下载了应用

时间:2021-11-16 15:22:52

I have an Android library that uploads data to a test server and production server. I'd like developers using this library to use the test server when developing, and production server when the app is downloaded from Android Market.

我有一个Android库,可以将数据上传到测试服务器和生产服务器。我希望使用此库的开发人员在开发时使用测试服务器,并在从Android Market下载应用程序时使用生产服务器。

Is this possible for an app to tell where it came from (Market or non-Market?) I would imagine one could detect the presence of the signed JAR file.

这可能是一个应用程序告诉它来自哪里(市场或非市场?)我想象一个人可以检测到签名的JAR文件的存在。

5 个解决方案

#1


38  

Yes, you could use the signature for that. If you use a debug key to sign your app during development and a release key when uploading your app to the market you can check for the signature that the app was signed with and based on that use test or production server. Here is a small code piece to read the signature of your app:

是的,您可以使用签名。如果您在开发期间使用调试密钥签署应用程序,并在将应用程序上传到市场时使用发布密钥,则可以检查应用程序签名的签名,并基于该使用测试或生产服务器。这是一个用于阅读应用签名的小代码:

    try {
        PackageManager manager = context.getPackageManager(); 
        PackageInfo appInfo = manager.getPackageInfo(
            YOUR_PACKAGE_NAME, PackageManager.GET_SIGNATURES);

        // Now test if the first signature equals your debug key.
        boolean shouldUseTestServer = 
            appInfo.signatures[0].toCharsString().equals(YOUR_DEBUG_KEY);

    } catch (NameNotFoundException e) {
        // Expected exception that occurs if the package is not present.
    }

YOUR_PACKAGE_NAME must be something like 'com.wsl.CardioTrainer'. It must be the package name you used in your AndroidManifest.xml. Good Luck

YOUR_PACKAGE_NAME必须是'com.wsl.CardioTrainer'。它必须是您在AndroidManifest.xml中使用的包名称。祝你好运

mark

标记

#2


10  

Starting with API 5, you can use PackageManager.getInstallerPackageName(String). From the documentation:

从API 5开始,您可以使用PackageManager.getInstallerPackageName(String)。从文档:

Retrieve the package name of the application that installed a package. This identifies which market the package came from.

检索安装包的应用程序的包名称。这确定了包裹来自哪个市场。

To get the package of the Android Market, this post may help.

要获得Android电子市场的套餐,这篇文章可能有所帮助。

#3


3  

From what I can tell there is nothing that the Market Application does to "flag" an application to say that it was downloaded from the market.

据我所知,市场应用程序没有做任何事情来“标记”一个应用程序,说它是从市场上下载的。

I have seen this issue approached in a different manner by another Android library. The AdMob Android SDK is free to download and use as described on their wiki. This library serves ads, so they have the same desire to be able to determine if the application that is currently running is being tested by the developer or if it is being used "in the wild". Their approach was to require that the developer set a "testing" attribute in the XML or to call their libraries "setTesting(boolean)" function to let the library know which ads to serve. This is obviously more of a manual approach that relies on the developer to change one line of code or XML before publishing.

我已经看到另一个Android库以不同的方式处理此问题。 AdMob Android SDK可以免费下载和使用,如其维基上所述。这个库提供广告,因此他们有同样的愿望,能够确定当前正在运行的应用程序是否正在由开发人员进行测试,或者是否“在野外”使用它。他们的方法是要求开发人员在XML中设置“测试”属性或调用他们的库“setTesting(boolean)”函数,让库知道要提供哪些广告。这显然更像是一种手动方法,它依赖于开发人员在发布之前更改一行代码或XML。

#4


1  

You could default your configuration to the production environment and use a custom Instrumentation that sets the configuration to testing environment. Intrumentation should be removed before publishing to the android market.

您可以将配置默认为生产环境,并使用自定义Instrumentation将配置设置为测试环境。在发布到Android市场之前应该删除入侵。

#5


0  

Perhaps you could add a setting to your app for "developer mode".

也许您可以为“开发者模式”为您的应用添加设置。

Another possibility, have it look for a special file or configuration file on the SD card, and key off of that.

另一种可能性是,它在SD卡上寻找一个特殊的文件或配置文件,并关闭它。

#1


38  

Yes, you could use the signature for that. If you use a debug key to sign your app during development and a release key when uploading your app to the market you can check for the signature that the app was signed with and based on that use test or production server. Here is a small code piece to read the signature of your app:

是的,您可以使用签名。如果您在开发期间使用调试密钥签署应用程序,并在将应用程序上传到市场时使用发布密钥,则可以检查应用程序签名的签名,并基于该使用测试或生产服务器。这是一个用于阅读应用签名的小代码:

    try {
        PackageManager manager = context.getPackageManager(); 
        PackageInfo appInfo = manager.getPackageInfo(
            YOUR_PACKAGE_NAME, PackageManager.GET_SIGNATURES);

        // Now test if the first signature equals your debug key.
        boolean shouldUseTestServer = 
            appInfo.signatures[0].toCharsString().equals(YOUR_DEBUG_KEY);

    } catch (NameNotFoundException e) {
        // Expected exception that occurs if the package is not present.
    }

YOUR_PACKAGE_NAME must be something like 'com.wsl.CardioTrainer'. It must be the package name you used in your AndroidManifest.xml. Good Luck

YOUR_PACKAGE_NAME必须是'com.wsl.CardioTrainer'。它必须是您在AndroidManifest.xml中使用的包名称。祝你好运

mark

标记

#2


10  

Starting with API 5, you can use PackageManager.getInstallerPackageName(String). From the documentation:

从API 5开始,您可以使用PackageManager.getInstallerPackageName(String)。从文档:

Retrieve the package name of the application that installed a package. This identifies which market the package came from.

检索安装包的应用程序的包名称。这确定了包裹来自哪个市场。

To get the package of the Android Market, this post may help.

要获得Android电子市场的套餐,这篇文章可能有所帮助。

#3


3  

From what I can tell there is nothing that the Market Application does to "flag" an application to say that it was downloaded from the market.

据我所知,市场应用程序没有做任何事情来“标记”一个应用程序,说它是从市场上下载的。

I have seen this issue approached in a different manner by another Android library. The AdMob Android SDK is free to download and use as described on their wiki. This library serves ads, so they have the same desire to be able to determine if the application that is currently running is being tested by the developer or if it is being used "in the wild". Their approach was to require that the developer set a "testing" attribute in the XML or to call their libraries "setTesting(boolean)" function to let the library know which ads to serve. This is obviously more of a manual approach that relies on the developer to change one line of code or XML before publishing.

我已经看到另一个Android库以不同的方式处理此问题。 AdMob Android SDK可以免费下载和使用,如其维基上所述。这个库提供广告,因此他们有同样的愿望,能够确定当前正在运行的应用程序是否正在由开发人员进行测试,或者是否“在野外”使用它。他们的方法是要求开发人员在XML中设置“测试”属性或调用他们的库“setTesting(boolean)”函数,让库知道要提供哪些广告。这显然更像是一种手动方法,它依赖于开发人员在发布之前更改一行代码或XML。

#4


1  

You could default your configuration to the production environment and use a custom Instrumentation that sets the configuration to testing environment. Intrumentation should be removed before publishing to the android market.

您可以将配置默认为生产环境,并使用自定义Instrumentation将配置设置为测试环境。在发布到Android市场之前应该删除入侵。

#5


0  

Perhaps you could add a setting to your app for "developer mode".

也许您可以为“开发者模式”为您的应用添加设置。

Another possibility, have it look for a special file or configuration file on the SD card, and key off of that.

另一种可能性是,它在SD卡上寻找一个特殊的文件或配置文件,并关闭它。