├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── keystore.jks ├── proguard-rules.pro ├── signing.properties ├── src │ ├── androidTest │ │ └── java │ │ │ └── com │ │ │ └── linkzhang │ │ │ └── gradlesample │ │ │ └── ApplicationTest.java │ ├── main │ │ ├── AndroidManifest.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── linkzhang │ │ │ │ └── gradlesample │ │ │ │ └── MainActivity.java │ │ └── res │ │ │ ├── layout │ │ │ ├── activity_main.xml │ │ │ └── content_main.xml │ │ │ ├── menu │ │ │ └── menu_main.xml │ │ │ ├── mipmap-hdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ └── ic_launcher.png │ │ │ ├── values-v21 │ │ │ └── styles.xml │ │ │ ├── values-w820dp │ │ │ └── dimens.xml │ │ │ └── values │ │ │ ├── colors.xml │ │ │ ├── dimens.xml │ │ │ ├── strings.xml │ │ │ └── styles.xml │ └── test │ │ └── java │ │ └── com │ │ └── linkzhang │ │ └── gradlesample │ │ └── ExampleUnitTest.java └── version.properties ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | GradleSample -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # GradleSample 2 | 一个Android Studio项目的gradle模板,包括自动签名,版本号自增,多渠道打包 3 | 4 | [Document](http://linkzhang.com/2016/09/12/android-gradle-config/) 5 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | // 获取当前系统时间 4 | static def releaseTime() { 5 | return new Date().format("yyyy-MM-dd", TimeZone.getTimeZone("UTC")) 6 | } 7 | 8 | // 获取程序名称 9 | static def getProductName(){ 10 | return "gradlesample" 11 | } 12 | 13 | android { 14 | compileSdkVersion 27 15 | buildToolsVersion "27.0.3" 16 | 17 | 18 | def currentVersionCode = getVersionCode() 19 | 20 | defaultConfig { 21 | applicationId "com.linkzhang.gradlesample" 22 | minSdkVersion 15 23 | targetSdkVersion 27 24 | versionCode currentVersionCode 25 | versionName "1.0" 26 | manifestPlaceholders = [UMENG_CHANNEL_VALUE: "example"]//默认渠道 27 | flavorDimensions "default" 28 | } 29 | 30 | 31 | 32 | signingConfigs { 33 | debug { 34 | 35 | } 36 | 37 | release { 38 | storeFile 39 | storePassword 40 | keyAlias 41 | keyPassword 42 | } 43 | 44 | } 45 | 46 | buildTypes { 47 | release { 48 | minifyEnabled true //开启代码混淆 49 | zipAlignEnabled true 50 | shrinkResources true // 移除无用的resource文件 51 | signingConfig signingConfigs.release 52 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 53 | 54 | //修改生成的apk名字,格式为 app名_版本号_打包时间_渠道名_release.apk 55 | applicationVariants.all { variant -> 56 | variant.outputs.all { output -> 57 | def channel = variant.getMergedFlavor().getManifestPlaceholders().get('UMENG_CHANNEL_VALUE'); 58 | if (variant.buildType.name == 'release') { 59 | outputFileName = getProductName() + "_v${defaultConfig.versionName}_${releaseTime()}_" + channel + '_release.apk' 60 | } 61 | } 62 | } 63 | } 64 | } 65 | 66 | 67 | 68 | getSigningProperties() 69 | //自动多渠道打包 70 | productFlavors { 71 | xiaomi {} 72 | _360 {} 73 | baidu {} 74 | wandoujia {} 75 | //... 76 | } 77 | 78 | productFlavors.all { 79 | flavor -> flavor.manifestPlaceholders = [UMENG_CHANNEL: name] 80 | } 81 | } 82 | 83 | 84 | //读取签名配置文件 85 | def getSigningProperties(){ 86 | 87 | def propFile = file('signing.properties') 88 | if (propFile.canRead()){ 89 | Properties props = new Properties() 90 | props.load(new FileInputStream(propFile)) 91 | if (props!=null && props.containsKey('STORE_FILE') && props.containsKey('STORE_PASSWORD') && 92 | props.containsKey('KEY_ALIAS') && props.containsKey('KEY_PASSWORD')) { 93 | android.signingConfigs.release.storeFile = file(props['STORE_FILE']) 94 | android.signingConfigs.release.storePassword = props['STORE_PASSWORD'] 95 | android.signingConfigs.release.keyAlias = props['KEY_ALIAS'] 96 | android.signingConfigs.release.keyPassword = props['KEY_PASSWORD'] 97 | } else { 98 | println 'signing.properties found but some entries are missing' 99 | android.buildTypes.release.signingConfig = null 100 | } 101 | }else { 102 | println 'signing.properties not found' 103 | android.buildTypes.release.signingConfig = null 104 | } 105 | } 106 | 107 | // 读取版本号 108 | def getVersionCode() { 109 | def versionFile = file('version.properties') 110 | if (versionFile.canRead()){ 111 | Properties versionProps = new Properties() 112 | versionProps.load(new FileInputStream(versionFile)) 113 | def versionCode = versionProps['VERSION_CODE'].toInteger() 114 | def runTasks = gradle.startParameter.taskNames 115 | if ('assembleRelease' in runTasks) { 116 | versionProps['VERSION_CODE'] = (++versionCode).toString() 117 | versionProps.store(versionFile.newWriter(), null) 118 | } 119 | return versionCode 120 | } else { 121 | throw new GradleException("Could not find version.properties!") 122 | } 123 | } 124 | 125 | 126 | dependencies { 127 | compile fileTree(dir: 'libs', include: ['*.jar']) 128 | testCompile 'junit:junit:4.12' 129 | compile 'com.android.support:appcompat-v7:27.1.1' 130 | compile 'com.android.support:design:27.1.1' 131 | } 132 | -------------------------------------------------------------------------------- /app/keystore.jks: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkZhang/GradleSample/9d646f9a8e35fb3118b3910e046dca3da6273ffc/app/keystore.jks -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in E:\Android SDK/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/signing.properties: -------------------------------------------------------------------------------- 1 | STORE_FILE = keystore.jks 2 | STORE_PASSWORD = 123456 3 | KEY_ALIAS = example 4 | KEY_PASSWORD = 123456 -------------------------------------------------------------------------------- /app/src/androidTest/java/com/linkzhang/gradlesample/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.linkzhang.gradlesample; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 15 | 16 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /app/src/main/java/com/linkzhang/gradlesample/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.linkzhang.gradlesample; 2 | 3 | import android.os.Bundle; 4 | import android.support.design.widget.FloatingActionButton; 5 | import android.support.design.widget.Snackbar; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.support.v7.widget.Toolbar; 8 | import android.view.View; 9 | import android.view.Menu; 10 | import android.view.MenuItem; 11 | 12 | public class MainActivity extends AppCompatActivity { 13 | 14 | @Override 15 | protected void onCreate(Bundle savedInstanceState) { 16 | super.onCreate(savedInstanceState); 17 | setContentView(R.layout.activity_main); 18 | Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 19 | setSupportActionBar(toolbar); 20 | 21 | FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 22 | fab.setOnClickListener(new View.OnClickListener() { 23 | @Override 24 | public void onClick(View view) { 25 | Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 26 | .setAction("Action", null).show(); 27 | } 28 | }); 29 | } 30 | 31 | @Override 32 | public boolean onCreateOptionsMenu(Menu menu) { 33 | // Inflate the menu; this adds items to the action bar if it is present. 34 | getMenuInflater().inflate(R.menu.menu_main, menu); 35 | return true; 36 | } 37 | 38 | @Override 39 | public boolean onOptionsItemSelected(MenuItem item) { 40 | // Handle action bar item clicks here. The action bar will 41 | // automatically handle clicks on the Home/Up button, so long 42 | // as you specify a parent activity in AndroidManifest.xml. 43 | int id = item.getItemId(); 44 | 45 | //noinspection SimplifiableIfStatement 46 | if (id == R.id.action_settings) { 47 | return true; 48 | } 49 | 50 | return super.onOptionsItemSelected(item); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 15 | 16 | 22 | 23 | 24 | 25 | 26 | 27 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /app/src/main/res/layout/content_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 15 | 16 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 5 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkZhang/GradleSample/9d646f9a8e35fb3118b3910e046dca3da6273ffc/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkZhang/GradleSample/9d646f9a8e35fb3118b3910e046dca3da6273ffc/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkZhang/GradleSample/9d646f9a8e35fb3118b3910e046dca3da6273ffc/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkZhang/GradleSample/9d646f9a8e35fb3118b3910e046dca3da6273ffc/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LinkZhang/GradleSample/9d646f9a8e35fb3118b3910e046dca3da6273ffc/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-v21/styles.xml: -------------------------------------------------------------------------------- 1 | > 2 | 3 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 16dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | GradleSample 3 | Settings 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 |