1. Introduction

When you are developing, the test suddenly tells you that there is something wrong with the formal package and you need to take a look. At this time, you open the test machine and find that the test package is installed on it. It is ok. You need to go back to the test package to continue development. You need to uninstall the test package again and install a test package.

This is a lot of trouble ~

How to solve it? Someone said, I have two test machines in my hand, one for the formal package, the other for the test package. I want to say, Chen Duxiu, you sit down first, most of us only have one test machine.

So, the problem is how to install both the official package and the test package on the same phone. This is the problem to be solved in this paper.

2. Realize the simultaneous installation of formal package and test package on a mobile phone

As we know, the unique identity of an Android app is the package name, which is the applicationId in build.gradle. The unique identification of the two packages installed on the same phone is not allowed to duplicate. Therefore, you just need to change the applicationId, or package name, of the test package

2.1 Changing the Test Package Name

App /build.gradle Android ->buildTypes->debug set applicationIdSuffix to Android ->buildTypes->debug

android {
    // ...
    
    buildTypes {
        debug {
            minifyEnabled false
            applicationIdSuffix ".test"         // Add the suffix to the test package name
        }
        release {
            // ...}}/ /...
}
Copy the code

2.2 Problem ~ compilation failed

Gradle failed to compile, failed to translate, failed, failed, failed, failed…

The error log is as follows:

[...].  :app:compileDebugJavaWithJavac error: The generated com.xxx.xx.test.R class cannot be foundCopy the code

What do you do? I don’t know why, looks like the AndroidAnnotation cooker. Using a debug “applicationIdSuffix” Causes compilation Errors #1888

2.3 Problem Solving

Please refer to the above issue for specific explanation and post solutions. Add javaCompileOptions in Android ->defaultConfig:

javaCompileOptions {
    annotationProcessorOptions {
        arguments = [
            "resourcePackageName": android.defaultConfig.applicationId
        ]
    }
}
Copy the code

The parameter resourcePackageName is defined by AndroidAnnotations (see here for details)

Last

So, finally, you can install both the official package and the test package on the same phone

Patch-2019-03-11

This method is only suitable for small projects. Larger projects with third-party SDKS will not be able to use this method. On Android, many third-party SDKS use the package name as the apiKey. If the package name is changed, many third-party SDK may not work, such as Baidu Map, third-party push these.

At the end of my practice at that time, I also found that there was a problem with the push and I needed to apply again, which was not feasible at that time, so there was indeed a problem with changing the package name. The main purpose of this article is to provide some reference for you, thank you ^_^

Personal

  • Wechat official account: YONGf666
  • The Denver nuggets: https://juejin.cn/user/78820566373432
  • Personal website: https://www.54yongf.com/
  • Weibo: http://weibo.com/2902237231
  • Zhihu: http://www.zhihu.com/people/wang-yong-8-33
  • Email address: [email protected]
  • StackOverflow:http://stackoverflow.com/users/5304207