Android的图书​​馆项目项目、图书、Android

2023-09-05 05:15:47 作者:回不去的从前

我如何使用一个Android库项目,并覆盖某些类中的最后的项目。

How can I use an Android Library Project, and overwrite certain classes in the "final project".

换句话说,我想引用一个库项目,并在同一时间,改变一些code ..

In other words, I want to reference a library project, and at the same time, change some of the code..

推荐答案

也许并不完全符合Android的库项目是所有有关,但你可以做到这一点通过使用Java语言本身的接口和类继承的功能。

Probably not quite what Android Library projects are all about but you could achieve that by using the interface and class inheritance features of the java language itself.

库项目似乎与合并/从父在子压倒一切的资源,让你使用一个codeBase类有不同的应用程序包名处理好。

Library project seem to deal well with merging/overriding resources from the parent in the child and letting you use one codebase with varying app package names.

鉴于有关使用库项目有一个使用AdMob的和一个犯规的应用程序的一个版本更新后的评论,我已经修订这个答案...

Given your updated comment about using library projects to have one edition of the app that uses AdMob and one that doesnt, I've revised this answer...

假设你与你的全/付费版不介意包装AdMob的库(〜138KB),做最简单的事就是来扩展的applcation类,并在其中声明一个枚举和一个静态方法来决定是否AdMob的广告应示出。

Assuming you dont mind packaging the AdMob library (~138KB) in with your full/paid edition, the simplest thing to do would be to extend an Applcation class and therein declare an enum and a static method to decide whether AdMob ads should be shown.

public class Application extends android.app.Application
{    
    public enum EditionType { LITE, FULL};      
    public static int getAdVisibilityForEdition(Context ctx)
    {
       if (EditionType.valueOf(ctx.getString(R.string.edition)) == EditionType.FULL)
       {
        return View.GONE;
       }
       return View.VISIBLE;
    }
}

您将这个类添加到只有库项目。在这三个项目中,你需要一个条目添加到/res/Strings.xml这样:

You'll add this class to the library project only. In all three of the projects you'll need to add an entry to /res/Strings.xml as such:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="edition">LITE</string>
</resources>

不管从什么这个字符串的值是库项目,但你会改变它的支付与广告支持分别使用full和lite版本。

It doesnt matter what the value of this string is in the library project, but you'll vary it in the paid vs. ad supported editions using FULL and LITE respectively.

最后,为了让您的活动响应这些值调用像这样在你的onCreate()或onResume():

Finally, to make your activities responsive to these values call something like this in your onCreate() or onResume():

View ad = (View)this.findViewById(R.id.your_adMob_view_id)
ad.setVisibility(Application.getAdVisibilityForEdition
                 (this.getApplicationContext()));

这将正常工作与AdMob的作为AdMob的code不会实际尝试得到一个信息,如果认为是不可见的。

This will work fine with AdMob as the AdMob code wont actually try to get an Ad if the view is not visible.

如果你真的不想有额外的138K的AdMob的罐子,并在完整版需要为它的明显的权限,有更优雅的方式来做到这一点,但会涉及使用某种形式的包装围绕AdMob的东西,无论是改变XML布局的版本之间(包括对AdMob的观点与否)或注入AdMob的观点到布局编程。

If you really dont want to have the extra 138K for the AdMob jar and the manifest permissions needed for it in the full edition, there are more elegant ways to do this but would involve using some kind of wrapper around the AdMob stuff and either varying the xml layouts between the editions (to include the AdMob view or not) or inject the AdMob view into the layout programatically.

希望有所帮助。