S
S
stdio962017-01-23 19:52:01
Java
stdio96, 2017-01-23 19:52:01

How to compile 1 of multiple projects on android?

Hello. There is an Android project for working with different APIs (vk, ok, fb...). Now there is 1 class, which contains all the functions for working with the API. For example: vkPost(), vkDeletePost(), okPost(), fbPost() etc. I want to create separate classes for each social. networks (VKConfig, OKConfig, FBConfig...). All other code, except resources, is the same. Resources for each social. networks are in the folder app/src/appForVk/res, app/src/appForOk/res, app/src/appForFb/res. Everything is ok with them. When compiling, I choose which project I want to build. Question: how to add these 3+ .java classes in a similar way. So that not everyone pulls up, but only the one that requested. Now in gragle:

productFlavors {
        appForVk {
            applicationId 'com.social.myapp.vk'
        }
        appForOk {
            applicationId 'com.social.myapp.ok'
        }
}
.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DanilinEgor, 2017-01-23
@stdio96

Try this option:
1) create an interface to work with different APIs

public interface SocialApi {
    void post(Post post);
    void deletePost();
}

2) for each social network implement this interface
public class VkApi implements SocialApi {
    @Override
    void post(Post post){}
    @Override
    void deletePost(){}
}

3) in productFlavors you prescribe
productFlavors {
        appForVk {
            buildConfigField("com.social.myapp.SocialApi", 'api', "new com.social.myapp.vk.VkApi()")
            applicationId 'com.social.myapp.vk'
        }
        appForOk {
            buildConfigField("com.social.myapp.SocialApi", 'api', "new com.social.myapp.ok.OkApi()")
            applicationId 'com.social.myapp.ok'
        }
}

4) next, a field will be generated in BuildConfig
Through BuildConfig.api, you can work with the social network and the class implementation will be substituted for different ProductFlavor

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question