Skip to content

Instantly share code, notes, and snippets.

@jnagels
Created January 6, 2014 10:01
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jnagels/8280622 to your computer and use it in GitHub Desktop.
Save jnagels/8280622 to your computer and use it in GitHub Desktop.
Building with Android Gradle Plugin 0.7.+
//location: android_hacks.gradle
//start custom permissions
def processManifest(variant)
{
//here we get the content of the manifest file, and we replace {packagename} with the actual packagename.
variant.processManifest.doLast {
copy {
from("${buildDir}/manifests") {
include "${variant.dirName}/AndroidManifest.xml"
}
into("${buildDir}/processed_manifests")
}
def manifestFile = file("${buildDir}/processed_manifests/${variant.dirName}/AndroidManifest.xml");
def packagename = variant.productFlavors.get(0).packageName + (variant.buildType.packageNameSuffix ?: "");
def content = manifestFile.getText().replaceAll(/\{packagename\}/, packagename);
manifestFile.write(content);
}
variant.processResources.manifestFile = file("${buildDir}/processed_manifests/${variant.dirName}/AndroidManifest.xml");
}
//end custom permissions
//START custom overriding of authorities content providers
def overrideContentProviderAuthority (buildVariant)
{
println "Overriding content provider authorities"
def flavor = buildVariant.productFlavors.get(0).name;
def buildType = buildVariant.buildType.name;
def workspaceDir = rootProject.projectDir.absolutePath + "/App/src/";
def uniqueSuffix = "." + flavor + "." + buildType;
def buildTypeResourceDir = workspaceDir + "/" + flavor + buildType.capitalize() + "/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)
}
processManifest(variant)
}
//end content providers
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<!-- {packagename} will be replaced by the actual packagename -->
<!-- Custom permission to safely send broadcasts between processes -->
<permission
android:name="{packagename}.permission.RECEIVE_BROADCASTS"
android:protectionLevel="signature"/>
<!-- Custom permission so 'com.showpad' is the only app that can receive push notifications -->
<permission
android:name="{packagename}.gcm.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<!-- Receive our custom broadcasts -->
<uses-permission android:name="{packagename}.permission.RECEIVE_BROADCASTS" />
<!-- For Google Cloud Messaging -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="{packagename}.gcm.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- SDK -->
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19"/>
<application ...>
<!-- Activities -->
<!-- ... -->
<!-- Providers -->
<provider
android:name=".providers.AssetProvider"
android:authorities="@string/provider_asset"
android:exported="true"/>
<provider
android:name=".providers.AssetTagProvider"
android:authorities="@string/provider_assettags"
android:exported="false"/>
<!-- ... -->
<!-- Services in the same process -->
<<!-- ... -->
<!-- Services in a different process -->
<!-- ... -->
<!-- Broadcast Receivers -->
<receiver
android:name="com.showpad.gcm.receivers.GCMReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="{packagename}" />
</intent-filter>
</receiver>
</application>
</manifest>
//location: App/src/main/java/com/example/myapp/providers/AssetProvider.java
public class AssetProvider extends AbstractSessionContentProvider
{
public static final String AUTHORITY = createProviderAuthority("myapp.asset");
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/");
public final static String createProviderAuthority(String inName)
{
return inName + "." + BuildConfig.FLAVOR + "." + BuildConfig.BUILD_TYPE;
}
...
}
//location: build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.9'
}
ext {
//build tools version
myBuildToolsVersion='19.0.0'
//target sdk level is 19 or KitKat 4.4
myCompileSdkVersion=19
myTargetSdkVersion=19
// minimum sdk level is 14( Ice Cream Sandwich, 4.0)
myMinSdkVersion=14
//support library version
mySupportLibraryVersion='com.android.support:support-v13:19.0.0'
// Java version 7
myJavaVersion=JavaVersion.VERSION_1_7
}
<!-- Location: App/src/main/res/values/contentproviders.xml -->
<?xml version="1.0" encoding="utf-8"?>
<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