Skip to content

Instantly share code, notes, and snippets.

@lovellfelix
Created October 18, 2013 00:09
Show Gist options
  • Save lovellfelix/7034488 to your computer and use it in GitHub Desktop.
Save lovellfelix/7034488 to your computer and use it in GitHub Desktop.
//use build variant resource folders!
android.applicationVariants.all { variant ->
variant.mergeResources.doFirst
{
def dirBuildType = variant.buildType.name + '/res';
def dirBuildVariant = variant.productFlavors.first().name + '/' + variant.buildType.name + '/res';
def folder = new File(projectDir.absolutePath + '/src/' + dirBuildVariant);
if (folder.exists())
{
//if the folder for the buildVariant exists, use that one
android.sourceSets."${variant.buildType.name}".res.srcDirs = [dirBuildVariant];
println "Using custom build variant resource folder: $dirBuildVariant"
}
else
{
//it doesn't exist, just use the build type version!
android.sourceSets."${variant.buildType.name}".res.srcDirs = [dirBuildType];
println "Using normal build type resource folder: $dirBuildType"
}
}
}
//START custom overriding of content providers
def overrideContentProviderAuthority (buildVariant)
{
println "Overriding content provider authorities"
def flavor = buildVariant.productFlavors.get(0).name;
def buildType = buildVariant.buildType.name;
def workspaceDir = projectDir.absolutePath + "/src/";
def uniqueSuffix = "." + flavor + "." + buildType;
def buildTypeResourceDir = workspaceDir + "/" + flavor + '/' + buildType + "/res/values/";
def xmlStringsFileAsNode = new XmlParser().parse(workspaceDir + "main/res/values/contentproviders.xml")
// Loop through each ContentProvider authority string resource in the file and append a unique suffix
xmlStringsFileAsNode.each {
it.setValue(it.text() + uniqueSuffix)
}
createOverrideDirIfNecessary(buildTypeResourceDir)
def fileWriter = new FileWriter(buildTypeResourceDir + "contentproviders.xml")
def printWriter = new PrintWriter(fileWriter)
printWriter.print("""<?xml version="1.0" encoding="utf-8"?>\n""")
def xmlPrinter = new XmlNodePrinter(printWriter)
xmlPrinter.setPreserveWhitespace(true)
xmlPrinter.print(xmlStringsFileAsNode)
}
def createOverrideDirIfNecessary(dirPath) {
def file = new File(dirPath)
if (!file.exists()) {
file.mkdirs()
}
}
//for all application variants, we need to do some special stuff to make the content providers work
android.applicationVariants.all { variant ->
variant.mergeResources.dependsOn {
overrideContentProviderAuthority(variant)
}
}
//end content providers
<provider
android:name=".providers.AssetProvider"
android:authorities="@string/provider_asset"
android:multiprocess="true"
android:exported="true"/>
<provider
android:name=".providers.AssetTagProvider"
android:authorities="@string/provider_assettags"
android:multiprocess="true"
android:exported="false"/>
public static final String AUTHORITY = createProviderAuthority("myapp.asset");
public final static String createProviderAuthority(String inName)
{
return inName + "." + BuildConfig.PRODUCT_FLAVOR + "." + BuildConfig.BUILD_TYPE;
}
apply plugin: 'android'
apply from: "$rootDir/android_hacks.gradle"
android {
compileSdkVersion spCompileSdkVersion
buildToolsVersion spBuildToolsVersion
defaultConfig {
minSdkVersion spMinSdkVersion
targetSdkVersion spTargetSdkVersion
versionCode 8
versionName '1.0.2'
}
buildTypes.all {
ext.canLog = false
}
//3 build types in total
//release,debug are created automatically, alpha is added
buildTypes {
debug {
canLog = true
packageNameSuffix ".debug"
versionNameSuffix "(debug)"
}
alpha {
canLog = true
packageNameSuffix ".alpha"
versionNameSuffix "(alpha)"
}
release {
canLog = false
}
}
//some product flavors
productFlavors {
flavor1 {
packageName "com.example.flavor1"
}
flavor2 {
packageName "com.example.flavor2"
}
}
//For all the build-types, we add the next line to BuildConfig.java (in their specific folder)
buildTypes.all {
it.buildConfig "public static final String BUILD_TYPE = \"" + it.name + "\";"
}
//For all the product flavors, we add the next line to BuildConfig.java (in their specific folder)
productFlavors.all {
it.buildConfig "public static final String PRODUCT_FLAVOR = \"" + it.name + "\";"
}
}
// add all the libraries!
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':libraries:menudrawer')
compile project(':libraries:support')
compile project(':libraries:http')
compile project(':libraries:utils')
compile project(':libraries:google-play-services_lib')
}
//start native libs to jar
task copyNativeLibs(type: Copy) {
//copy all native libraries to the folder build/native-libs
from(new File('libs')) { include '**/*.so' }
into new File(buildDir, 'native-libs')
}
tasks.withType(JavaCompile) {compileTask ->
//let the JavaCompile task depend on copyNativeLibs
compileTask.dependsOn copyNativeLibs
}
tasks.withType(com.android.build.gradle.tasks.PackageApplication) { pkgTask ->
//add the native-libs dir to the packageApplication
pkgTask.jniDir new File(buildDir, 'native-libs')
}
//end native libs to jar
<resources>
<string name="provider_asset">myapp.asset</string>
<string name="provider_assettags">myapp.assettags</string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment