本文共 9527 字,大约阅读时间需要 31 分钟。
刚刚升级了正式版的Android Studio3.0部分API不能用了,但是官网还是给出了解决方案,不过很模糊,需要自己琢磨,今天带大家一起看看。
首先gradle3.0之前的多版本打版方法:(可以看出挺费劲的~)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | apply plugin: 'com.android.application' apply plugin: 'android-apt' def demo1 = '0001' ; //DemoAPK1 def demo2 = '0002' ; //DemoAPK2 android { // signingConfigs { // debug { // keyAlias '****' // keyPassword '****' // storeFile file('签名文件.jks路径') // storePassword '****' // } // release { // keyAlias '****' // keyPassword '****' // storeFile file('签名文件.jks路径') // storePassword '****' // } // } compileSdkVersion 25 buildToolsVersion "25.0.2" sourceSets { main { jniLibs.srcDirs = [ 'libs' ] } } packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' } defaultConfig { applicationId "com.example.p031_mokuaihua_viewpager_fragment" minSdkVersion 21 targetSdkVersion 25 versionCode 1 versionName "1.0" multiDexEnabled true renderscriptTargetApi 21 renderscriptSupportModeEnabled true ndk { moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main { jni.srcDirs = [] //LOCAL_LDFLAGS += -fuse-ld=bfd //jni.srcDirs 'src/main/jni' jniLibs.srcDir 'src/main/libs' } signingConfig signingConfigs.debug manifestPlaceholders = [ DEMO3_CONFIG_VALUE: ".demo3.factorys.Demo3Factory" , DEMO4_CONFIG_VALUE: ".demo4.factorys.Demo4Factory" ] } buildTypes { release { minifyEnabled true zipAlignEnabled true shrinkResources false proguardFiles getDefaultProguardFile( 'proguard-android.txt' ), 'proguard-rules.pro' signingConfig signingConfigs.debug } } def int minSdk = 21 ; def int targetSdk = 25 ; def String appId = 'com.example.p031_mokuaihua_viewpager_fragment' ; def int vCode = 1 ; def String vNameCode = vCode + "" ; productFlavors { //demo1 DemoAPK1_T_1_0001 线上 DemoAPK1 { minSdkVersion minSdk applicationId appId targetSdkVersion targetSdk versionCode vCode versionName "DemoAPK1_" + "T_" + vNameCode + "_" + demo1 multiDexEnabled true renderscriptTargetApi 21 renderscriptSupportModeEnabled true ndk { moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main { jni.srcDirs = [] jniLibs.srcDir 'src/main/libs' } signingConfig signingConfigs.debug } //demo2 DemoAPK2_D_1_0002 线上 DemoAPK2 { minSdkVersion minSdk applicationId appId targetSdkVersion targetSdk versionCode vCode versionName "DemoAPK2_" + "D_" + vNameCode + "_" + demo2 multiDexEnabled true renderscriptTargetApi 21 renderscriptSupportModeEnabled true ndk { moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main { jni.srcDirs = [] jniLibs.srcDir 'src/main/libs' } signingConfig signingConfigs.debug } } // 自定义输出配置 applicationVariants.all { variant -> variant.outputs.each { output -> def outputFile = output.outputFile if (outputFile != null && outputFile.name.endsWith( '.apk' )) { // def fileName = "UerbT_v${variant.versionName}_${releaseTime()}_${variant.flavorName}.apk" def fileName = "${variant.versionName}.apk" output.outputFile = new File(outputFile.parent, fileName) } } } productFlavors.all { flavor -> def currentMode = flavor.versionName.split( "_" )[ 3 ] def currentEnvironment = flavor.versionName.split( "_" )[ 1 ] def stValue = true // t == currentEnvironment 以前的判断条件 if (currentEnvironment.endsWith( "T" )) { //判断是否为测试版 是否以T结尾 stValue = false } else { stValue = true } if (currentMode == demo1) { flavor.manifestPlaceholders = [DEMO2_CONFIG_VALUE: ".demo2.factorys.Demo2Factory1" , STATISTICS_VALUE: stValue] } else if (currentMode == demo2) { flavor.manifestPlaceholders = [DEMO2_CONFIG_VALUE: ".demo2.factorys.Demo2Factory2" , STATISTICS_VALUE: stValue] } } } dependencies { compile fileTree(dir: 'libs' , include: [ '*.jar' ]) androidTestCompile( 'com.android.support.test.espresso:espresso-core:2.2.2' , { exclude group: 'com.android.support' , module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:recyclerview-v7:25.3.1' compile 'com.android.support:design:25.3.1' compile 'com.android.support:cardview-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7' //the third file compile 'com.jakewharton:butterknife:8.2.1' apt 'com.jakewharton:butterknife-compiler:8.2.1' compile 'com.android.support:multidex:1.0.1' } |
这里说一下:gradle3.0后apt就废除了,现在都是这样的方法:
1 2 | compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1' |
再来看看gradle3.0后是什么样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | apply plugin: 'com.android.application' def demo1 = '0001' ; //DemoAPK1 def demo2 = '0002' ; //DemoAPK2 android { // signingConfigs { // debug { // keyAlias '****' // keyPassword '****' // storeFile file('签名文件.jks路径') // storePassword '****' // } // release { // keyAlias '****' // keyPassword '****' // storeFile file('签名文件.jks路径') // storePassword '****' // } // } compileSdkVersion 25 buildToolsVersion '26.0.2' sourceSets { main { jniLibs.srcDirs = [ 'libs' ] } } packagingOptions { exclude 'META-INF/DEPENDENCIES' exclude 'META-INF/NOTICE' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE.txt' } defaultConfig { applicationId "com.example.p031_mokuaihua_viewpager_fragment" minSdkVersion 21 targetSdkVersion 25 versionCode 1 versionName "1.0" multiDexEnabled true renderscriptTargetApi 21 renderscriptSupportModeEnabled true javaCompileOptions { annotationProcessorOptions { includeCompileClasspath true } } ndk { moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main { jni.srcDirs = [] //LOCAL_LDFLAGS += -fuse-ld=bfd //jni.srcDirs 'src/main/jni' jniLibs.srcDir 'src/main/libs' } signingConfig signingConfigs.debug manifestPlaceholders = [ DEMO3_CONFIG_VALUE: ".demo3.factorys.Demo3Factory" , DEMO4_CONFIG_VALUE: ".demo4.factorys.Demo4Factory" ] } buildTypes { release { minifyEnabled true zipAlignEnabled true shrinkResources false proguardFiles getDefaultProguardFile( 'proguard-android.txt' ), 'proguard-rules.pro' signingConfig signingConfigs.debug } } def int minSdk = 21 ; def int targetSdk = 25 ; def String appId = 'com.example.p031_mokuaihua_viewpager_fragment' ; def int vCode = 1 ; def String vNameCode = vCode + "" ; // 自定义输出配置 classpath 'com.android.tools.build:gradle:3.0.0' android.applicationVariants.all { variant -> variant.outputs.all { // outputFileName = "${variant.name}-${variant.versionName}.apk" outputFileName = "${variant.versionName}.apk" } } // Specifies two flavor dimensions. flavorDimensions "keep" , "change1" productFlavors { demo { dimension "keep" minSdkVersion minSdk applicationId appId targetSdkVersion targetSdk versionCode vCode multiDexEnabled true renderscriptTargetApi 21 renderscriptSupportModeEnabled true ndk { moduleName "native-modbus-jni,libxmediaplayer" ldLibs "log" , "z" , "m" , "android" , "c" abiFilters "armeabi" , "armeabi-v7a" , "x86" } sourceSets.main { jni.srcDirs = [] jniLibs.srcDir 'src/main/libs' } signingConfig signingConfigs.debug } apk1 { dimension "change1" versionName "DemoAPK1_" + "T_" + vNameCode + "_" + demo1 def stValue = false manifestPlaceholders = [DEMO2_CONFIG_VALUE: ".demo2.factorys.Demo2Factory1" , STATISTICS_VALUE: stValue] } apk2 { dimension "change1" versionName "DemoAPK2_" + "D_" + vNameCode + "_" + demo2 def stValue = true manifestPlaceholders = [DEMO2_CONFIG_VALUE: ".demo2.factorys.Demo2Factory2" , STATISTICS_VALUE: stValue] } } } dependencies { compile fileTree(dir: 'libs' , include: [ '*.jar' ]) androidTestCompile( 'com.android.support.test.espresso:espresso-core:2.2.2' , { exclude group: 'com.android.support' , module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' compile 'com.android.support:recyclerview-v7:25.3.1' compile 'com.android.support:design:25.3.1' compile 'com.android.support:cardview-v7:25.3.1' compile 'com.android.support.constraint:constraint-layout:1.0.0-alpha7' //the third file annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0' compile 'com.jakewharton:butterknife:8.6.0' compile 'com.android.support:multidex:1.0.1' } |
是不是简单很多,Google真是越来越叼~
打版后代码效果图:
官网方案地址:
本代码git地址:
附图: