📜 ⬆️ ⬇️

What did you encounter when translating the project to Android Studio 3.0 Preview and Gradle 4.0-milestone-1

After on Google IO 2017 Keynote announced a new Android Studio 3.0 Preview and Gradle 4.0-milestone-1 , of course, my hands itched to try it all at once. If the first one just appeared a lot of interesting features, then the second one seriously changed the API.


Therefore, I would like to briefly share what I encountered while translating the current application to these innovations. It will not be any generalized tutorial or review of all the buns. This is just a step-by-step list of problems that we personally encountered at LiveTyping for one specific project.



We will immediately list what dependencies are in the project and what problems might arise:



Step 1.


I advise you to install Android Studio 3.0 Preview next to the old Android Studio 2.3, and not instead of it. There was a bitter experience that quickly weaned to update from the Canary channel. I was lucky to be one of those lucky ones who, when upgrading to Android Studio 2.2 Preview 1, the studio began to randomly crash and not save the last 2-5 minutes of work . It was repeated - not at all, and was not treated before Preview 6. The rollback did not help.


Step 2.


Running your project on a new studio, we will immediately see the message


image


Either agree immediately, or do everything by hand


In %PROJECT%/gradle/wrapper/gradle-wrapper.properties replace


 distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip 

on


 distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-milestone-1-all.zip 

In %PROJECT%/build.gradle replace


 buildscript { dependencies { - classpath 'com.android.tools.build:gradle:2.3.0' } } 

on


 buildscript { dependencies { + classpath 'com.android.tools.build:gradle:3.0.0-alpha1' } } 

But here one is nuance. Android Studio itself does not add a repository for download android gradle plugin 3.0.0-alpha1 . Therefore, in any case, you have to do it by hand. Add to %PROJECT%/build.gradle


 buildscript { repositories { maven { + url 'https://maven.google.com' } } 

Step 3.


After synchronization of the project a new error appeared


image


image


This is our samopisny task, which renamed apk files in a convenient format.
We go to the instructions for migration to the new android gradle plugin and at the very bottom we see the following


 API change in variant output Using the Variant API to manipulate variant outputs is broken with the new plugin. If you're using this API with the new plugin, you'll see the following error message: Error:(41, 0) Not valid. This build error occurs because variant-specific tasks are no longer created during the configuration stage. This results in the plugin not knowing all of its outputs up front, but it also means faster configuration times. As an alternative, we will introduce new APIs to provide similar functionality. 

Well, for now there is nowhere to go - just comment out the whole task and live like this. We are waiting for the new API to appear.


Later we will see that now apk files are not placed in %PROJECT%/app/build/outputs/apk/ as before, but separate directories for buildtypes and flavors are created for them. In our case, this is 2 directories.


 %PROJECT%/app/build/outputs/apk/debug %PROJECT%/app/build/outputs/apk/release 

And in each of them are 2 files:


 app-debug.apk output.json 

If everything is clear with the first one, then the content of the second file is interesting.


 [ { "outputType": { "type": "APK" }, "apkInfo": { "type": "MAIN", "splits": [], "versionCode": 10000 }, "outputFile": { "path": <FULL_APK_PATH> }, "properties": { "packageId": <YOUR_PACKAGE_NAME>, "split": "" } } ] 

Well, it can be used automate apload apk file.


Step 4.


After the next synchronization of the project, we will see the following message


image


 Information:Gradle tasks [:app:generateDebugSources, :app:mockableAndroidJar, :app:generateDebugAndroidTestSources] Warning:The Jack toolchain is deprecated and will not run. To enable support for Java 8 language features built into the plugin, remove 'jackOptions { ... }' from your build.gradle file, and add android.compileOptions.sourceCompatibility 1.8 android.compileOptions.targetCompatibility 1.8 Future versions of the plugin will not support usage of 'jackOptions' in build.gradle. To learn more, go to https://d.android.com/r/tools/java-8-support-message.html Warning:One of the plugins you are using supports Java 8 language features. To try the support built into the Android plugin, remove the following from your build.gradle: apply plugin: 'me.tatarka.retrolambda' To learn more, go to https://d.android.com/r/tools/java-8-support-message.html Warning:One of the plugins you are using supports Java 8 language features. To try the support built into the Android plugin, remove the following from your build.gradle: apply plugin: 'me.tatarka.retrolambda' To learn more, go to https://d.android.com/r/tools/java-8-support-message.html 

In general, here we see 2 messages


a. The Jack toolchain is deprecated and is required to completely get rid of its mention.
Remember that this is true and remove jackOptions from the %PROJECT%/app/build.gradle


 android { defaultConfig { - jackOptions { - enabled false - } } } 

b. New Android Studio 2.4 Preview partially supports Java8 and Retrolambda is no longer needed. In accordance with the recommendations on migration, we remove it from the project.


In the %PROJECT%/build.gradle


 buildscript { dependencies { - classpath 'me.tatarka.retrolambda.projectlombok:lombok.ast:0.2.3.a2' - classpath 'me.tatarka:gradle-retrolambda:3.3.0' - // NOTE: Do not place your application dependencies here; they belong - // in the individual module build.gradle files } - // Exclude the lombok version that the android plugin depends on. - configurations.classpath.exclude group: 'com.android.tools.external.lombok' } 

In the %PROJECT%/app/build.gradle


 - apply plugin: 'me.tatarka.retrolambda' 

Add Java8 support if it wasn’t


 android { compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 } } 

Step 5.


In accordance with the new Gradle API, we replace dependency configurations


 compile -> implementation provided -> compileOnly 

neither and to heighten the understanding


 debugCompile -> debugImplementation releaseCompile -> releaseImplementation debugProvided -> debugCompileOnly releaseProvided -> releaseCompileOnly testCompile -> testImplementation androidTestCompile -> androidTestImplementation 

well, or something like that, according to your buildtypes and flavors.


Step 6.


Earlier in the project we have already replaced apt plugin with annotationProcessor. Removed all references to android-apt in the %PROJECT%/build.gradle


 buildscript { dependencies { - classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } } 

And in the %PROJECT%/app/build.gradle


 - apply plugin: 'com.neenbedankt.android-apt' 

And replaced depending


 apt -> annotationProcessor 

After that, you will need to review the repositories of the relevant libraries (such as Dagger2, StorIO, AutoValue, Butterknife, Timber, Moxy and others) and replace the addresses of their dependencies as a whole. However, here we are faced with one unsolvable problem with the extremely useful IcePick library, which, unfortunately, does not support annotationProcessor and Kotlin .


image


However, the guys from Evernote have made their similar Android-State library, the SNAPSHOT version of which does an excellent job and has an API similar to the IcePick API.


 repositories { jcenter() maven { + url 'https://oss.sonatype.org/content/repositories/snapshots/' } } dependencies { + implementation 'com.evernote:android-state:1.1.0-SNAPSHOT' + annotationProcessor 'com.evernote:android-state-processor:1.1.0-SNAPSHOT' } 

Step 7.


And at the very end, another surprise awaited us.


image


It may immediately seem that there is some kind of problem with multidex. In fact, it turned out that the problem is with the dexcount-gradle-plugin we use. On the page of the plugin we saw the message:


 NOTE: dexcount does not currently work with the new Android Build Tools as of 3.0.0-alpha1; it has removed APIs that we depend on and, while replacements have been promised, none have yet been provided. 

Well, for now, we also comment on the use of this plugin and wait for edits.


Total


This was the last point, after which the project successfully started. This is quite a special case, but maybe someone will help to quickly understand their migration problems. Write in the comments whether you are going to try on a new studio and Gradle in the near future or are going to wait for release. What problems have you encountered?


Useful links:


  1. Android Studio 3.0 Preview 1
  2. New android plugin migration
  3. Java8 support
  4. Java8 language features support update

')

Source: https://habr.com/ru/post/329530/


All Articles