简体   繁体   English

如何使用Gradle更改Android风味应用程序的包名称?

[英]How to change the package name of an Android flavor app with Gradle?

I am currently migrating a very old Android app from Eclipse to Android Studio. 我目前正在将一个非常古老的Android应用程序从Eclipse迁移到Android Studio。

In the old version of my app, I was able to create some "flavors" with an Ant script. 在我的应用程序的旧版本中,我能够使用Ant脚本创建一些“风格”。 The aim of this script was to do a copy of the Eclipse project with a different package name and with different resources (drawable, etc.) 这个脚本的目的是使用不同的包名和不同的资源(drawable等)来复制Eclipse项目。

I have migrated the project to Android Studio, deleted the Ant script and created some flavors. 我已将项目迁移到Android Studio,删除了Ant脚本并创建了一些风格。

But now, I have some issues to deserialize - in the new version of the app - an object serialized in the old version of the app. 但是现在,我有一些问题要反序列化 - 在新版本的应用程序中 - 在旧版本的应用程序中序列化的对象。

Here a part of the stackstrace : Caused by: java.lang.ClassNotFoundException: com.company.app.flavor.bo.Player 这里是stackstrace的一部分: Caused by: java.lang.ClassNotFoundException: com.company.app.flavor.bo.Player

In the old version, each flavors had it own package name : 在旧版本中,每种口味都有自己的包名:

  • com.company.app
  • com.company.app.flavor

In this new version, the code structure is com.company.app.flavor and I play with Gradle in order to generate the good applicationId for the flavor. 在这个新版本中,代码结构是com.company.app.flavor ,我和Gradle一起玩,以便为flavor生成好的applicationId

So, the applicationId for the flavor is correct, but the package name of the class seems to respect the project structure so, I cannot deserialize the original object with the package com.company.app.flavor.bo.Player into the object com.company.app.bo.Player . 因此,flavor的applicationId是正确的,但类的包名似乎尊重项目结构,因此,我无法使用包com.company.app.flavor.bo.Player将原始对象反序列化到对象com.company.app.bo.Player

If I change the structure of the project with com.company.app.flavor it will work for the flavor but not for the original one with the applicationId com.company.app . 如果我用com.company.app.flavor更改项目的结构,它将适用于味道,但不适用于带有applicationId com.company.app的原始结构。

So, is there a way, with Gradle or maybe another tool (proguard ?) to change the applicationId AND the package name for a flavor ? 那么,有没有办法,使用Gradle或者其他工具(proguard?)来更改applicationId和包装名称?

Thank you in advance for your help. 预先感谢您的帮助。

Yes you can have ProductFlavor in gradle and this supposed to change the package name of the app. 是的,您可以在gradle中使用ProductFlavor,这应该会更改应用程序的包名称。 If you didn't hardcoded the classpath then serialization also should work without any issues. 如果你没有对类路径进行硬编码,那么序列化也应该没有任何问题。

      android {
          .
          .
          .
     defaultConfig {
            applicationId "lib4.com.stackoverflow"
        }

         productFlavors {
                staging {
                   applicationIdSuffix ".staging"
                }

                preProduction {

                    applicationIdSuffix ".preProduction"

                }
        }
  }

Once the apk got created, you can verify whether the package name has been changed or not by dragging and dropping apk on android studio and open Android Manifest. 创建apk后,您可以通过在Android工作室上拖放apk并打开Android Manifest来验证包名是否已更改。 You will see the updated package name in the manifest in apk. 您将在apk中的清单中看到更新的包名称。 But in source code, you will still see the package you provided earlier. 但在源代码中,您仍会看到之前提供的包。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM