Search code examples
androidandroid-studiogradleandroid-gradle-pluginandroid-productflavors

It is possible to get a .aar with a Product flavor


I work on a big project at work, i have been herited 3 months ago. At the moment the project structure is like this :

  • Engine : its a library with my entire code (all java classes) with some extern libraries
  • App : its a android project but it is just a wrapper for have 2 declinaisons-flavor of the app (different name and icon for commercial purpose). The engine lib is integrated to this app.

Additionnaly, sometimes I do build the engine to .aar for one client who use the library without the app wrapper.

My big problem : the last developepr of this project dont good know gradle and android structure, it is some strange res added twice in Engine and App/ build script / extern lib added twice...

So, before i work on a new major version, i want to refator the build system and simplify it ! In the best world, i want to delete the lib project, add the Engine code to the App, for have just 1 project app and use power of flavors for managing specifics parts.

If i have flavors like this :

  • one
  • two
  • lib

It is possible to use flavor "lib" to get a .aar ?

If you have another solution for simplify or resolve my problem, im over. Thanks in advance :)


Solution

  • Having, has you described it, a single application module that outputs both .aar and .apk is not possible afaik, your module has to be a library OR an application, not both. (i.e. in your build.gradle : apply plugin: 'com.android.application' or 'com.android.library').

    My guess here will be (Android studio 1.5.1) :

    • A single Android Studio project,
    • Two modules, one library module and one application module (File -> new... -> New module... -> one "Android Library" and one "Phone or tablet module")
    • In the app module, the 2 flavors for your branding.
    • Integrate the library module into your app module as a dependency (in your app build.graddle dependencies section : compile project(path : ':yourlibrarymodule'))

    Your ressources can either be stored either in your library (usable in your lib and your app without duplication), in your app in the main flavor (usable in your 2 flavors without duplication) or in your two flavors.

    Therefore, you can produce the .aar by building your library module, and the two .apk by building your app module.