简体   繁体   English

Android Studio使用具有相同包名称的库和项目

[英]Android Studio using a library and project with the same package name

I have a library I use as a base for all my android apps and has the following manifest file: 我有一个库用作所有android应用程序的基础,并具有以下清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android"
    xmlns:tools="http://schemas.android.com/tools"
    android:versionCode="36"
    android:versionName="1.b" >

    <uses-sdk
        android:minSdkVersion="5"
        android:targetSdkVersion="21"

        tools:overrideLibrary="com.facebook.android"/>

I then try to use it in one of my projects which has the following manifest: 然后,我尝试在具有以下清单的一个项目中使用它:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android"
    xmlns:tools="http://schemas.android.com/tools"
    android:versionCode="36"
    android:versionName="1.6" >

    <uses-library
        android:name="com.example.android"
        android:required="true"/>

    <uses-sdk
        android:minSdkVersion="5"
        android:targetSdkVersion="21"

        tools:overrideLibrary="com.facebook.android"/>

Note that the package name used is the same: com.example.android. 请注意,使用的包名称是相同的:com.example.android。 Since the app is published under com.example.android, I cannot change it for the app. 由于该应用程序是在com.example.android下发布的,因此我无法对其进行更改。 As for the library, for historical reasons, it has the same pacakge name. 至于图书馆,出于历史原因,它具有相同的名称。 When I build the project, I get the following error: 生成项目时,出现以下错误:

FAILURE: Build failed with an exception. 失败:构建失败,发生异常。

  • What went wrong: Execution failed for task ':exampleCom:processDebugResources'. 出了什么问题:任务':exampleCom:processDebugResources'的执行失败。

    Error: A library uses the same package as this project: com.example.android You can temporarily disable this error with android.enforceUniquePackageName=false However, this is temporary and will be enforced in 1.0 错误:一个库使用与此项目相同的包:com.example.android您可以使用android.enforceUniquePackageName = false暂时禁用此错误,但这是临时的,将在1.0中强制执行

I do not want to change the package name of either library or app. 我不想更改库或应用程序的软件包名称。 I am not sure where to add the "android.enforceUniquePackageName=false" . 我不确定在哪里添加“ android.enforceUniquePackageName = false”。 Any ideas? 有任何想法吗? Also, how to solve beyond 1.0 (which I am already using)? 另外,如何解决超过1.0(我已经在使用)的问题?

You can add enforceUniquePackageName=false in the app modules build.gradle file under android: 您可以在android下的应用模块build.gradle文件中添加forceUniquePackageName = false:

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    enforceUniquePackageName = false
    ...
}

Unfortunately this results in another problem with an unfixed bug from the build tools. 不幸的是,这导致了另一个问题,即构建工具中未修复的错误。

Error:Execution failed for task ':app:packageAllDebugClassesForMultiDex'. 错误:任务':app:packageAllDebugClassesForMultiDex'的执行失败。 java.util.zip.ZipException: duplicate entry: ... BuildConfig.class java.util.zip.ZipException:重复项:... BuildConfig.class

See https://stackoverflow.com/a/27310034/668400 参见https://stackoverflow.com/a/27310034/668400

An ounce of prevention is worth a pound of cure. 一盎司的预防值得一磅的治疗。 It may not help this particular situation, but for future developers to avoid getting into this situation, please note the JLS recommendation in Chapter 6. Names which addresses this very problem: 这可能对这种情况没有帮助,但是对于将来的开发人员来说,避免陷入这种情况,请注意第6章中的JLS建议。解决这个问题的名称

Package Names 包装名称

Developers should take steps to avoid the possibility of two published packages having the same name by choosing unique package names for packages that are widely distributed. 开发人员应采取步骤,为分布广泛的软件包选择唯一的软件包名称,以避免两个已发布的软件包具有相同名称的可能性。 This allows packages to be easily and automatically installed and catalogued. 这样可以轻松,自动地安装和分类软件包。 This section specifies a suggested convention for generating such unique package names. 本节指定了用于生成此类唯一程序包名称的建议约定。 Implementations of the Java SE platform are encouraged to provide automatic support for converting a set of packages from local and casual package names to the unique name format described here. 鼓励Java SE平台的实现提供自动支持,以将一组软件包从本地和临时软件包名称转换为此处描述的唯一名称格式。

If unique package names are not used, then package name conflicts may arise far from the point of creation of either of the conflicting packages. 如果不使用唯一的程序包名称,则与创建任何一个冲突程序包的位置相距很远,可能会引起程序包名称冲突。 This may create a situation that is difficult or impossible for the user or programmer to resolve. 这可能会导致用户或程序员难以解决或无法解决的情况。 The class ClassLoader can be used to isolate packages with the same name from each other in those cases where the packages will have constrained interactions, but not in a way that is transparent to a naïve program. 在某些情况下,如果程序包之间的交互受到约束,则可以使用ClassLoader类将具有相同名称的程序包彼此隔离开来,但这种方式对于天真的程序不透明。

You form a unique package name by first having (or belonging to an organization that has) an Internet domain name, such as oracle.com. 通过首先拥有(或属于拥有互联网域名)例如oracle.com的组织来形成唯一的软件包名称。 You then reverse this name, component by component, to obtain, in this example, com.oracle, and use this as a prefix for your package names, using a convention developed within your organization to further administer package names. 然后,您逐个组件颠倒此名称,以获取com.oracle(在此示例中),并将其用作包名称的前缀,并使用组织内部开发的约定来进一步管理包名称。 Such a convention might specify that certain package name components be division, department, project, machine, or login names. 这样的约定可以指定某些程序包名称组件为部门,部门,项目,机器或登录名。

Package name is a unique identifier for a package and therefore you should not have the same package name for both the library and the application. 软件包名称是软件包的唯一标识符,因此对于库和应用程序,您不应使用相同的软件包名称。 Change the package name for the library if the application is already published. 如果应用程序已发布,请更改该库的软件包名称。

worked on as 2.2, gradle 2.14.1 工作于2.2,gradle 2.14.1

android {
    enforceUniquePackageName = false
    //it was deprecated, but still work
    android.packageBuildConfig = false 
    ...
}

details: new-build-system 详细信息: 新建系统

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

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