如何在一台设备上安装不同版本的安卓应用-阿里云开发者社区

开发者社区> wei-spring> 正文

如何在一台设备上安装不同版本的安卓应用

简介: 第一步: 1. Create the variant dependent strings in your build script Edit your build.gradle file accordingly:   //This line allows parameterizatio...
+关注继续查看

第一步:

1. Create the variant dependent strings in your build script

Edit your build.gradle file accordingly:

 

 //This line allows parameterization via the terminal and the Gradle VM Options
def debugsuffix = System.  
        getProperty('debugsuffix', project.getProperties().get('debugsuffix', null))

def final yourApplicationId = 'your.application.id'

android {  
  //In the defaultConfig and all your product flavors create build dynamic variables

  defaultConfig {
      applicationId = yourApplicationId

    //These are the values for the authorities and account types.
    //Reference them in the java files with e.g. BuildConfig.ACCOUNT_TYPE.

      buildConfigField "String", "ACCOUNT_TYPE", "\"${applicationId}.account\""
      buildConfigField "String", "AUTHORITY", "\"${applicationId}.provider\""

    //Reference them in .xml files.
      resValue "string", "account_type", "${applicationId}.account"
      resValue "string", "authority", "${applicationId}.provider"
    }
  productFlavors {  
    dev {
      applicationId = yourApplicationId + ".dev" + debugsuffix

    //Reference them again so they get overwritten by the flavor.

      buildConfigField "String", "ACCOUNT_TYPE", "\"${applicationId}.account\""
      buildConfigField "String", "AUTHORITY", "\"${applicationId}.provider\""

      resValue "string", "account_type", "${applicationId}.account"
      resValue "string", "authority", "${applicationId}.provider"
    }
  }
}

  

第二部:

 

2. Refer to them in your Android Manifest

Replace the permissions and provider authorities in yourAndroidManifest.xml with ${applicationId}:

 

<permission android:name="${applicationId}.permission.MAPS_RECEIVE"  
        android:protectionLevel="signature" />
<uses-permission android:name="${applicationId}.permission.MAPS_RECEIVE" />

<provider   android:name=".your.provider"  
            android:authorities="${applicationId}.your.provider" 
            … />

  

 

第三步:

 

3. Refactor your Java source files with the build specific strings

Update every occurrence of static strings for authorities and account types in your source files.

 

 

public static final String AUTHORITY = BuildConfig.AUTHORITY;  
public static final String AUCCOUNT_TYPE = BuildConfig.ACCOUNT_TYPE;  

  

 

第四步:

添加xml文件

 

4. Refactor your SyncAdapter resources

Use the resValue in the syncadapter.xml in res/xml:

 

<sync-adapter android:contentAuthority="@string/authority"  
              android:accountType="@string/account_type" 
           … />

  

 

Another nice way to use this is utilising the the Jenkins build number as a suffix.

Et voilà, you can now build and install different app versions on a single device. Just make sure to do some heavy testing, as some static strings hide themselves and can break something. During research it turned out that using the pre-build variable applicationIdSuffix does not work properly, as the application ID gets changed after the buildFlavor. Another important thing to mention is that you have to sync the project with gradle files after you changed the Gradle VM options.

 

From:

https://blog.grandcentrix.net/how-to-install-different-app-variants-on-one-android-device/

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
如何在Ubuntu上安装最新版本的Node.js
如何在Ubuntu上安装最新版本的Node.js Node.js是一个软件平台,通常用于构建大规模的服务器端应用。Node.js使用JavaScript作为其脚本语言,由于其非阻塞I/O设计以及单线程事件循环机制,使得它可以交付超高的性能。
759 0
用广播监听安卓设备电量状态
  发送通知     这次邮件我们将会讨论怎么获取电量状态在安卓设备上,为了完成这个目标,我们将会使用到广播。   What is BroadcastReceiver?A broadcast receiver is an Android component...
905 0
Redis不同数据类型命令使用及应用场景
Redis不同数据类型命令使用及应用场景
2201 0
如何在一台设备上安装不同版本的安卓应用
第一步: 1. Create the variant dependent strings in your build script Edit your build.gradle file accordingly:   //This line allows parameterizatio...
707 0
Maven本地jar包部署与安装:dubbox-2.8.4.jar
Maven本地jar包部署与安装:dubbox-2.8.4.jar
5765 0
+关注
wei-spring
Android 开发,Java 开发,Python开发
366
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载