Android Studio 第五十五期 - Studio3.0升级到gradle3.0.0后API失效方案

简介:

    刚刚升级了正式版的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真是越来越叼~

    打版后代码效果图:

    7570ce0b6bb9da081fd7834dd2546d50.png

    官网方案地址:https://developer.android.com/studio/build/gradle-plugin-3-0-0-migration.html?utm_source=android-studio#variant_aware

    本代码git地址:

https://github.com/geeklx/MyApplication/tree/master/p031_mokuaihua_viewpager_fragment

    附图:

    a1ee3bfbd02fd43246c50b9323496cad.jpg









本文转自 吴雨声 51CTO博客,原文链接:http://blog.51cto.com/liangxiao/1976767,如需转载请自行联系原作者

目录
相关文章
|
3月前
|
算法 搜索推荐 Android开发
android的A/B到底是什么?OTA升级又是什么?
android的A/B到底是什么?OTA升级又是什么?
64 0
|
3月前
|
人工智能 IDE 开发工具
Studio Bot - 让 AI 帮我写 Android 代码
Studio Bot - 让 AI 帮我写 Android 代码
153 1
|
4月前
|
Web App开发 移动开发 小程序
"项目中mpaas升级到10.2.3 适配Android 14之后 app中的H5以及小程序都访问不了,
"项目中mpaas升级到10.2.3 适配Android 14之后 app中的H5以及小程序都访问不了,显示“网络不给力,请稍后再试”,预发内网版本不能使用,线上版本可以正常使用,这个是什么原因啊,是某些参数没有配置吗,还是说是一些参数改错了?
56 2
|
4月前
|
Java 开发工具 Android开发
OpenCV(一):Android studio jni配置OpenCV(亲测有效,保姆级)
OpenCV(一):Android studio jni配置OpenCV(亲测有效,保姆级)
158 0
|
21天前
|
Java Android开发
Android Studio的使用导入第三方Jar包
Android Studio的使用导入第三方Jar包
10 1
|
2月前
|
缓存 监控 测试技术
ERP系统对接方案与API接口封装系列(高并发)
企业资源规划(ERP)系统是现代企业管理的核心,它集成了企业内部的各个部门和业务流程。为了实现ERP系统与其他外部系统或应用程序之间的数据交换和协作,需要对接方案。API(应用程序编程接口)是实现系统对接的常用方法之一。
|
2月前
|
数据库 Android开发 数据库管理
【Android】使用android studio查看内置数据库信息
【Android】使用android studio查看内置数据库信息
66 0
|
2月前
|
编译器 开发工具 Android开发
|
2月前
|
Android开发
【Android Studio】小游戏 | 实现两个小动物随手指移动
【Android Studio】小游戏 | 实现两个小动物随手指移动
|
2月前
|
Android开发 数据安全/隐私保护
【Android Studio】简单的QQ登录界面
【Android Studio】简单的QQ登录界面

热门文章

最新文章