7

我在 gradle/android studio 中使用 Product Variants 来实现以下项目设置:

  1. 两个应用程序,在一个 android studio 项目中 80% 相似。
  2. 每个应用程序都应该有自己的清单包路径(它们基本上应该像两个独立的应用程序一样 - 使用它自己的 google api 和按键)

我已经遵循了多个教程来尝试实现这一点(占位符、多个清单),但没有任何效果。

按照本教程,我做了以下事情:http: //www.kevinrschultz.com/blog/2014/03/23/using-android-content-providers-with-multiple-package-names/

我的 build.gradle:

android {
compileSdkVersion 19
buildToolsVersion '20'

defaultConfig {
    minSdkVersion 10
    targetSdkVersion 19
    versionCode 1
    versionName "1.0"
}

buildTypes {
    release {
        runProguard false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}

productFlavors {
    app1 {
        packageName "com.test.app1"
        //applicationId "com.test.app1"
    }

    app2 {
        packageName "com.test.app2"
        //applicationId "com.test.app2"
    }
}}

这里是我的清单文件。

源/主/清单:

<?xml version="1.0" encoding="utf-8"?>

<application>
</application>

src/app1/清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:name="com.test.app1”
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.test.app1.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="portrait" >
        </activity>
    </application>
</manifest>

src/app2/清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:name="com.test.app2”
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="com.test.app2.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleTask"
            android:screenOrientation="portrait" >
        </activity>
    </application>
</manifest>

这给了我以下错误消息:

Manifest merger failed : Main AndroidManifest.xml at AndroidManifest.xml manifest:package attribute is not declared

不幸的是,我无法为每个单独的清单标签设置一个包,因为我的应用程序需要不同的包路径。如果我仍然这样做,则由于包值不同,合并无法合并清单文件。除了 packageName 我还尝试设置“applicationId”,但这也不起作用。使用这样的占位符是package="${applicationId}"行不通的,因为它不能解析变量值。

任何想法如何解决这个问题?我正在使用带有 gradle 0.12.2 的 Android Studio 0.8.2

4

4 回答 4

4

您可以安全地添加

<manifest xmlns:android="..."
  package="com.test.app">
</manifest>

在您的主要清单中。

但是你必须使用applicationId而不是packageName在你的 build.gradle 中。applicationIdbuild.gradle 的 将在您的不同构建期间覆盖此值。

更好的方法是使用applicationIdSuffix而不是applicationId用于您的不同产品口味。

该字段必须存在于您的主清单中。

于 2014-08-12T12:57:49.660 回答
3

除了你src/app1/AndroidManifest.xmlsrc/app2/AndroidManifest.xml你将有一个src/main/AndroidManifest.xml可以包含两个应用程序共享的元素。

在特定于风味的清单中,您不必指定package属性,因为它已经在您的build.gradle.

但是,在您的主清单中,您需要定义一个package属性,以指定用于共享资源的包名称:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.test.app">
</manifest>

确保您没有任何依赖库,它也使用该名称,否则您将收到另一个构建错误。

此外,您将希望指定applicationId而不是packageName在您build.gradle中将用于标识您的应用程序的包名称与用于内部资源访问的包名称分离。

检查此链接以获取更多信息:http ://tools.android.com/tech-docs/new-build-system/applicationid-vs-packagename

于 2015-07-21T15:56:44.827 回答
0

我解决了相同的问题,将相同的内容package="com.ex.app"放入每种风味的清单并将applicationIduseOldManifestMerger true放入build.gradle文件中

android {
    useOldManifestMerger true

    productFlavors{
        app1{
            applicationId = "com.ex.app1"
        }
        app2{
            applicationId = "com.ex.app2"
        }
    }
}
于 2014-08-20T12:35:46.310 回答
0

我重新开始,但这次 Android Studio 为我生成了 Variant 配置(在项目设置下有一个用于风味、变体等的选项卡)

我还决定将所有三个变体的包名称更改为相同的名称,因为每个应用程序(变体)的标识由 applicationId 完成(这与变体的实际包名称无关)。

感谢 pdegand59 的帮助!

于 2014-08-21T06:56:16.233 回答