简体   繁体   English

在Android Studio / gradle中进行免费/专业版构建变体时,如何处理免费版本中的库导入?

[英]When doing free/pro build variants in Android Studio/gradle, how to handle import for library in free version?

I have an android app which is build with android studio/gradle so that there is a free and a pro build. 我有一个使用android studio / gradle构建的android应用,因此有免费和专业版本。

The free version needs two extra permissions, INTERNET and ACCESS_NETWORK_STATE, to support advertising. 免费版本需要两个额外的权限,即INTERNET和ACCESS_NETWORK_STATE,才能支持广告。 I want my pro edition to not need these permissions. 我希望我的专业版不需要这些权限。

I have set up a separate manifest file for the free edition, with the extra permissions, but when I built the pro version, it still said it needed those permissions. 我为免费版设置了一个单独的清单文件,具有额外的权限,但是当我构建专业版时,它仍然表示需要这些权限。

Only when I changed my gradle file to no longer use com.google.android.gms:play-services-ads did it build without the extra two permissions. 只有当我将gradle文件更改为不再使用com.google.android.gms:play-services-ads时,它才会在没有额外的两个权限的情况下进行构建。

I changed the gradle file from: 我从以下位置更改了gradle文件:

compile 'com.google.android.gms:play-services-ads:8.4.0'

to: 至:

freeCompile 'com.google.android.gms:play-services-ads:8.4.0'

But when I do that, I get errors in my code, because the following imports cannot be resolved: 但是当我这样做时,我的代码中出现错误,因为无法解决以下导入:

import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;

I need this because I need to initialize the ads in the free version: 我需要这样做是因为我需要将广告初始化为免费版本:

if (!isPro) {
        AdView mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }

In CI would use a pre-processor macro and have no problems, but Java is much much more advanced and so doesn't have that useful feature. 在CI中,将使用预处理器宏也没有问题,但是Java更加先进,因此没有该有用的功能。 ;-) ;-)

How do I compile this library only into my free version, but still refer to these classes in my code to set up the ads? 如何仅将此库编译为免费版本,但仍在代码中引用这些类来设置广告?

Another problem is that there are IDs on the free layout that are not in the pro layout. 另一个问题是免费版式上的ID不在专业版式中。 This generates an error in the pro version because R.id.adView is not found. 由于未找到R.id.adView,因此在专业版中会产生错误。 To get around this, I created an empty, invisible text box on the pro layout, and named it adview. 为了解决这个问题,我在专业版面上创建了一个空的,不可见的文本框,并将其命名为adview。 Is there a better way? 有没有更好的办法?

You can create two versions (one per flavor) of the class 您可以创建该类的两个版本(每种口味一个)

..\\app\\src\\ free \\java\\com\\android\\application\\Class.java .. \\ app \\ src \\ 免费 \\ java \\ com \\ android \\ application \\ Class.java

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
...
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

And the other 和另一个

..\\app\\src\\ pro \\java\\com\\android\\application\\Class.java .. \\ app \\ src \\ pro \\ java \\ com \\ android \\ application \\ Class.java

will contain everything but the ad releted imports and code. 将包含除广告重新导入的内容和代码之外的所有内容。

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

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