Android duplicate provider authority problem

前端 未结 4 625
长情又很酷
长情又很酷 2021-01-05 07:08

We\'re trying to publish a pay ad-free version of a casual app that\'s currently published free with ads. We refactored all package names to com.mycompanyname.appname.

4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-05 07:50

    Basically what I did is, create an abstract base class for each of my ContentProviders and inherit from that for each app I want to make, overriding the authority path. So in my AbstractContentProvider I have:

    public AbstractContentProvider() {
        sURIMatcher.addURI(getAuthority(), BASE_PATH, ITEMS);
        sURIMatcher.addURI(getAuthority(), BASE_PATH + "/#", ITEM_ID);
    }
    
    protected abstract String getAuthority();
    

    and then in each subclass I have:

    private static final String AUTHORITY = "my.package.app1.ContentProvider";
    
    @Override
    protected String getAuthority() {
        return AUTHORITY;
    }
    

    In the AndroidManifest I register these with:

        
        
    

    Now the trick is, I want to access these content providers in common (library) code, that doesn't know about the app specific classes. To do that, I define a String in my strings.xml, that I override for each app. Then I can use:

    Uri.parse(getString(R.string.contentProviderUri))
    

    and in every app the right ContentProvider is used without any conflicts. So basically using the configuration mechanism for dependency injection.

提交回复
热议问题