Skip to content

Instantly share code, notes, and snippets.

@cyrilmottier
Last active January 12, 2024 18:04
Show Gist options
  • Star 67 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save cyrilmottier/8234960 to your computer and use it in GitHub Desktop.
Save cyrilmottier/8234960 to your computer and use it in GitHub Desktop.
Using the new Gradle-based Android build system: a second example
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cyrilmottier.android.avelov"
android:installLocation="auto">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-feature
android:name="android.hardware.location"
android:required="false" />
<uses-feature
android:name="android.hardware.location.network"
android:required="false" />
<uses-feature
android:name="android.hardware.location.gps"
android:required="false" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/config_app_name"
android:theme="@style/Theme.CityBikes">
<!-- -->
<!-- Activities -->
<!-- -->
<!-- ... -->
<!-- -->
<!-- Providers -->
<!-- -->
<provider
android:authorities="@string/config_authority"
android:exported="false"
android:name="com.cyrilmottier.android.citybikes.provider.CityBikesProvider" />
<!-- -->
<!-- Google Maps Android API v2 keys -->
<!-- -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="@string/config_com.google.android.maps.v2.api_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="config_app_name">AVélib Debug</string>
<string name="config_authority">com.cyrilmottier.android.avelib.citybikes.debug</string>
<string name="config_com.google.android.maps.v2.api_key">XXX</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="config_app_name">AVélib</string>
<string name="config_authority">com.cyrilmottier.android.avelib.citybikes</string>
<string name="config_com.google.android.maps.v2.api_key">XXX</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="config_app_name">AVélov Debug</string>
<string name="config_authority">com.cyrilmottier.android.avelov.citybikes.debug</string>
<string name="config_com.google.android.maps.v2.api_key">XXX</string>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="config_app_name">AVélov</string>
<string name="config_authority">com.cyrilmottier.android.avelov.citybikes</string>
<string name="config_com.google.android.maps.v2.api_key">XXX</string>
</resources>
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:19.0.0'
compile 'com.google.android.gms:play-services:4.0.30'
compile 'com.squareup.retrofit:retrofit:1.2.2'
}
def versionMajor = 2
def versionMinor = 0
def versionPatch = 0
def versionBuild = 0
def gitSha() {
def res = 'git rev-parse HEAD'.execute([], project.rootDir).text.trim()
def diff = 'git diff'.execute([], project.rootDir).text.trim()
if (diff != null && diff.length() > 0) {
res += "-dirty"
}
return res
}
android {
compileSdkVersion 18
buildToolsVersion '19'
defaultConfig {
buildConfigField "String", "GIT_SHA", "\"${gitSha()}\""
minSdkVersion 14
targetSdkVersion 19
versionCode versionMajor * 10000 + versionMinor * 1000 + versionPatch * 100 + versionBuild
versionName "${versionMajor}.${versionMinor}" + ((versionPatch != 0) ? ".${versionPatch}" : "")
}
signingConfigs {
debug {
storeFile file("../distribution/debug.keystore")
}
release {
storeFile file("../distribution/release.keystore")
storePassword "XXX"
keyAlias "XXX"
keyPassword "XXX"
}
}
productFlavors {
avelov {
packageName = "com.cyrilmottier.android.avelov"
}
avelib {
packageName = "com.cyrilmottier.android.avelib"
}
}
buildTypes {
debug {
packageNameSuffix ".debug"
signingConfig signingConfigs.debug
versionNameSuffix "-debug"
}
release {
proguardFile 'config.proguard'
runProguard true
signingConfig signingConfigs.release
}
}
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.9'
}
package com.cyrilmottier.android.citybikes.provider;
import android.net.Uri;
import com.cyrilmottier.android.avelov.BuildConfig;
/**
* @author Cyril Mottier
*/
public class CityBikesContract {
public static final String CONTENT_AUTHORITY = buildAuthority();
private static String buildAuthority() {
String authority = "com.cyrilmottier.android.";
authority += BuildConfig.FLAVOR;
authority += ".citybikes";
if (BuildConfig.DEBUG) {
authority += ".debug";
}
return authority;
}
public static final Uri CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);
// ...
}
@Yougin
Copy link

Yougin commented Jan 23, 2014

Cyril, thanks for sharing, very useful! One question, beyond the subject though. I thought we need to compile the project with the minimum API defined for it, but I can see 'compileSdkVersion 18' in your build.gradle while 'minSdkVersion 14'.

@dawidhyzy
Copy link

Is it possible to read storePassword etc from external file? I don't want to put it in gradle file.

@tdevaux
Copy link

tdevaux commented Oct 19, 2014

'git diff' can be replaced by 'git diff --shortstat'. I deleted a lot of files in a repo, and it made Gradle go out of memory. Took time to figure out what was going on ;)

@eclectice
Copy link

BuildConfig.FLAVOR may return nothing when it is called from any extended class of ContentProvider. In my case, I use a singleton class to create a static content authority string from something similar to buildAuthority() and set android:authorities="${applicationId}.citybikes.provider.CityBikesProvider" in AndroidManifest.xml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment