[Solved] Android Flavors, BuildConfig

Anything about development not directly related to libgdx, e.g. OpenGL, Android APIs etc.

[Solved] Android Flavors, BuildConfig

Postby eggheadgames » Wed Jun 11, 2014 8:24 pm

Prior to switching to libGDX, I had a nice Android Studio Gradle configuration where I could build 6 APK versions of my app (3 variants, each for Google Play and Amazon Fire) with a single Gradle command: "gradlew assembleRelease".

With the recent introduction of Gradle for libGDX, I'm trying to get back to that (though no idea how it will play out with iOS, which would be ideal... but I suspect the intermingled assets folder is still a problem waiting to happen... one thing at a time!).

Anyhow, has anyone else done this?

Currently, I'm using Eclipse and have managed to get the variations building correctly if I use gradle from the command line and/or the Gradle Task command line inside Eclipse. However, I'm not able to run directly from Eclipse because I need somewhere to specify which flavor is "current". This manifests by the file MyProject-android/src/gen/.../BuildConfig.java not having some necessary declarations.

Has anyone tried this in IntelliJ? As Android Studio is based on that, I'm figuring it might be simpler.

Any other suggestions or comments? Thanks!
Last edited by eggheadgames on Mon Jul 07, 2014 2:02 pm, edited 2 times in total.
Egghead Games - http://eggheadgames.com
eggheadgames
 
Posts: 137
Joined: Sat Apr 12, 2014 9:18 pm

Re: Android Flavors BuildConfig Eclipse vs IntelliJ

Postby eggheadgames » Mon Jul 07, 2014 1:50 pm

Not much interest in this it seems, but I'll update it in case someone comes across this later.

I've added a new project "Pack" alongside the existing standard libGDX projects (Android, iOS, Desktop, ... etc). In there, I've leveraged the Gradle Android plugin to generate multiple app flavors in a very concise and DRY way. I've added to the gradle build script so that iOS happens correctly as well. For example, this is the gradle command set I use to generate a Play Amazon and iOS version of my app. (Play and Amazon have different AndroidManifest settings, as well as different IAP support libraries).

Code: Select all
gradlew :pack:prepLogicPlay
gradlew :android:assembleRelease
git tag ...
gradlew :pack:prepLogicAmazon
gradlew :android:assembleRelease
git tag ...
gradlew :pack:prepLogicIos
Eclipse, refresh, right-click RoboVM Tools -> Package for distribution
git tag ...


And then I can do the whole thing over for a different version of my app (which has different icons, different names), viz:

Code: Select all
gradlew :pack:prepQuicklogicPlay
etc.


Taking this step by step, the workflow is:

Code: Select all
gradlew :pack:prepFlavor1Flavor2


This command generates the correct combination of files and copies them over to the Android/assets folder. It also does some tweaking of the BuildConfig.java file to avoid some name alteration complications (the net result is the ability to set Java constants, like Flurry IDs or Google Play IAP keys directly from the build.gradle script instead of your source code).

Code: Select all
<test and iterate as normal>


Because nothing is changed in the normal libGDX file layout or structure, you can do everything you normally do.

Code: Select all
<git commit, tag, build your final release apk/ipa/html/jar whatever>


Again, just like you normally do.

When you're ready to work on the next variation of your program (e.g. one for Amazon distribution vs Google distribution), you just run gradlew :pack:prepFlavor1Amazon, for example, and then repeat the above steps.

This isn't quite as good as the original Android Studio integration, where you could build all variations simultaneously. But it is much simpler and less error prone than changing files by hand for each variation.

If anyone else is interested, I can write this up in the libgdx wiki along with a sanitized version of my pack/build.gradle file.
Egghead Games - http://eggheadgames.com
eggheadgames
 
Posts: 137
Joined: Sat Apr 12, 2014 9:18 pm

Re: [Solved] Android Flavors, BuildConfig

Postby mzechner » Tue Jul 08, 2014 8:04 pm

This is neat! We havent' really investigated Flavors yet as that part of the new Android Gradle build system still seems to be somewhat in flux. Would appreciate if you transfer the info here to our wiki! Great work.
mzechner
Site Admin
 
Posts: 4879
Joined: Sat Jul 10, 2010 3:50 pm

Re: [Solved] Android Flavors, BuildConfig

Postby eggheadgames » Mon Aug 11, 2014 5:59 am

mzechner wrote:This is neat! We havent' really investigated Flavors yet as that part of the new Android Gradle build system still seems to be somewhat in flux. Would appreciate if you transfer the info here to our wiki! Great work.


I wasn't sure how best to add this to the wiki because the devil is in the details and the details are inherent in the directory structure and stub files. I did create a template build layout from how I'm doing it. You can find it at:

https://github.com/eggheadgames/libgdx-flavors

I released it under Apache 2.0 to match the libGDX license, so feel free to do whtever you want with it and/or suggest to me how I can make it more useful and/or better incorporate it into the libGDX ecosystem.
Egghead Games - http://eggheadgames.com
eggheadgames
 
Posts: 137
Joined: Sat Apr 12, 2014 9:18 pm

Re: [Solved] Android Flavors, BuildConfig

Postby fmmarzoa » Tue Jul 25, 2017 3:21 pm

Hey!

Sorry for bringing this zombie thread back to life, but it's been three years since it was posted and I wonder if there is a more "official" way for creating different Android builds based on the target market or so?

Bests!
fmmarzoa
 
Posts: 122
Joined: Sat May 12, 2012 9:46 am

Re: [Solved] Android Flavors, BuildConfig

Postby MrStahlfelge » Mon Apr 09, 2018 7:23 pm

And now it is another year later. But now I figured it out how to add flavors without going to far away from the normal libgdx project structure!

1) change android/build.gradle like the following example:
Code: Select all
android {
    buildToolsVersion ....
    compileSdkVersion ...
    sourceSets {
        main {
// This is normal libgdx
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
            jniLibs.srcDirs = ['libs']
        }
// and here we go with our flavors!
        amazon {
            java.srcDirs = ['src-amazon']
        }
        google {
            java.srcDirs = ['src-google']
            manifest.srcFile 'AndroidManifest-google.xml'
        }
        generic {
            java.srcDirs = ['src-generic']
        }

// normal again...
        instrumentTest.setRoot('tests')
    }
    packagingOptions {
       ...
    }
    defaultConfig {
      ....
    }

    // now define the flavors here!
    flavorDimensions "type"
    productFlavors {
        amazon { dimension "type" }
        google { dimension "type" }
        generic { dimension "type" }
    }
   
    // normal...
    buildTypes {
        release {
            ....
        }
    }
}

// if you need dependencies for specific flavors, define them here!
dependencies {
    googleCompile "de.golfgl.gdxgamesvcs:gdx-gamesvcs-android-gpgs:$gamsvcsVersion"
}


2) add the src-directories and other resources (in the above example, we have another AndroidManifest-google). For different code in the Android project, I renamed the AndroidLauncher to GeneralAndroidLauncher and use an extending AndroidLauncher in every subproject:

Code: Select all
public class AndroidLauncher extends GeneralAndroidLauncher {
    // override when needed
}


Image
MrStahlfelge
 
Posts: 82
Joined: Thu Jun 15, 2017 6:40 am


Return to General Development

Who is online

Users browsing this forum: No registered users and 1 guest