简体   繁体   English

Gradle构建在不同产品上失败

[英]Gradle build failing on different productFlavors

I am developing an application with two flavors as follow: 我正在开发具有以下两种风格的应用程序:

productFlavors {
    free {
        applicationId "com.udacity.gradle.builditbigger.free"
        versionName "1.0-free"
    }
    paid {
        applicationId "com.udacity.gradle.builditbigger.paid"
        versionName "1.0-paid"
    }
}

Now I have admob dependency for the free version and want to make the paid version completely ad-free, hence deciding on compiling the dependency only for free version. 现在,我具有免费版本的admob依赖关系,并希望使付费版本完全没有广告,因此决定仅编译免费版本的依赖关系。 I tried doing this as follow: 我尝试这样做,如下所示:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile project(":showmessage")
testCompile 'junit:junit:4.12'
compile project(path: ':backend', configuration: 'android-endpoints')
compile 'com.android.support:appcompat-v7:23.1.0'
freeCompile 'com.google.android.gms:play-services-ads:8.4.0'}

and when I try to call the gradle task installPaidDebug it gives me the following error: 当我尝试调用gradle任务installPaidDebug时,出现以下错误:

/home/hemal/Desktop/Udacity-android-nd/Build It Bigger/app/build/intermediates/manifests/full/paid/debug/AndroidManifest.xml Error:(33, 28) No resource found that matches the given name (at 'value' with value '@integer/google_play_services_version'). / home / hemal / Desktop / Udacity-android-nd / Build Itger / app / build / intermediates / manifests / full / paid / debug / AndroidManifest.xml错误:(33,28)找不到与给定名称匹配的资源( (值)为“ @ integer / google_play_services_version”)。 Error:Execution failed for task ':app:processPaidDebugResources'. 错误:任务':app:processPaidDebugResources'的执行失败。 com.android.ide.common.process.ProcessException: Failed to execute aapt com.android.ide.common.process.ProcessException:无法执行aapt

But when I simple add the following line: compile 'com.google.android.gms:play-services-ads:8.4.0' the project is build seamlessly. 但是,当我简单地添加以下行时: compile 'com.google.android.gms:play-services-ads:8.4.0'该项目将无缝构建。

How do I overcome this? 我该如何克服呢?

As mentioned in the comments you need to move the meta data tag from the manifest in your src/main directory to the manifest in your src/free directory. 如注释中所述,您需要将元数据标签从src / main目录中的清单移动到src / free目录中的清单。

<!-- This meta-data tag is required to use Google Play Services. --> 
         <meta-data 
             android:name="com.google.android.gms.version" 
             android:value="@integer/google_play_services_version" /> 

Your free manifest will look something like: 您的免费清单将类似于:

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


     <!-- Include required permissions for Google Mobile Ads to run --> 
     <uses-permission android:name="android.permission.INTERNET" /> 
     <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 


     <application 
         android:allowBackup="true" 
         android:icon="@mipmap/ic_launcher" 
         android:label="@string/app_name" 
         android:theme="@style/AppTheme" > 


         <!-- This meta-data tag is required to use Google Play Services. --> 
         <meta-data 
             android:name="com.google.android.gms.version" 
             android:value="@integer/google_play_services_version" /> 


         <activity 
             android:name=".MainActivity" 
             android:label="@string/app_name" > 
             <intent-filter> 
                 <action android:name="android.intent.action.MAIN" /> 


                 <category android:name="android.intent.category.LAUNCHER" /> 
             </intent-filter> 
         </activity> 
         <!-- Include the AdActivity configChanges and theme. --> 
         <activity 
             android:name="com.google.android.gms.ads.AdActivity" 
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
             android:theme="@android:style/Theme.Translucent" /> 
     </application> 


 </manifest> 

I think you have to use split manifest files, but note that manifest files are merged while building. 我认为您必须使用拆分清单文件,但请注意,清单文件在构建时会合并。 You can check merging rules to know more 您可以查看合并规则以了解更多信息

Had the same problem before. 以前有同样的问题。 The solution is manifest merging. 解决方案是清单合并。 Remove the meta-tag from common AndroidManifest and create another manifest in your free module and add meta tag in this AndroidManifest. 从常见的AndroidManifest中删除元标记,并在您的免费模块中创建另一个清单,然后在此AndroidManifest中添加元标记。

The problem is while building the paid version, there is a meta tag as defined in the manifest( because it is a common manifest), and no dependency (as you have added the dependency only in the free version). 问题在于构建付费版本时,清单中定义了一个meta标签(因为它是常见清单),并且没有依赖项(因为您仅在免费版本中添加了依赖项)。 So the paid version cannot recognize what this meta tag corresponds to. 因此,付费版本无法识别此元标记对应的内容。

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

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