Android studio 2.3. 3 Upgrade to 3.1. 2 Pit of Notes

  • 2021-10-15 11:35:23
  • OfStack

1. grade configuration Error: Could not find com. android. tools. build: gradle: 2.2. 1.

The solution is a bit like Maven repository: enter

D:\software\android\android-studio-ide-145.3276617-windows\android-studio\gradle\m2repository\com\android\tools\build\gradle

In the build. gradle file in the project


dependencies { classpath 'com.android.tools.build:gradle:2.2.1'}

Replace with


dependencies { classpath 'com.android.tools.build:gradle:3.1.2'}

2.All flavors must now belong to a named flavor dimension.

Add 1 line of code in build. gradle under module to solve this problem:


android{ ... flavorDimensions "versionCode" ...}

3. Upgrade to Android Studio 3.1 and rebuild the project with errors. The main errors are:

The SourceSet 'instrumentTest' is not recognized by the Android Gradle Plugin. Perhaps you misspelled something?
instrumentTest is obsolete and the new Gradle plug-in is no longer supported. instrumentTest needs to be modified to androidTest.

After modification, the related contents of sourceSets are similar:

android { buildToolsVersion "27.0.3" compileSdkVersion 24 sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] aidl.srcDirs = ['src'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] jniLibs.srcDirs = ['libs'] } androidTest.setRoot('tests') }}

4. AAPT2 compilation error AAPT2 error

Report an error

Error:java.util.concurrent.ExecutionException: com.android.tools.aapt2.Aapt2Exception: AAPT2 error: check logs for details
Resolution: Turn off APPT2 compilation in gradle. properties


android.enableAapt2=false

Note: If it is a project on eclipse going to as, there may be no gradle. properties file, please create it manually in the project root directory

5. After upgrading Android Stadio 3.1

After changing all compile to implementation, no errors were found in clean and rebuild, except for the following errors in run:

Causes and solutions

Reason: As required by android stadio 3.1, gradle version needs to be upgraded to 4.4 and above, but gradle 4.4 and above requires compile relying on api to be replaced by implementation. However, the dependencies declared by implementation cannot be passed outside module, that is, module outside module cannot reference api declared by implementation.

That is, module A implementation libA, and moduleB implementation module A, then module B boots api in libA, otherwise org. gradle. api. internal. tasks. CompilationFailedException will be reported when build.
Solution: Replace compile with api, that is, if there is an external reference, replace it with api, and replace the rest with implementation.

Example:

dependencies { api fileTree(dir: 'libs', include: ['*.jar']) testImplementation 'junit:junit:4.12' api 'com.android.support:support-v4:26.1.0' api 'com.android.support:appcompat-v7:26.1.0' api 'com.google.code.gson:gson:2.8.2' api 'com.j256.ormlite:ormlite-core:4.48' api 'com.j256.ormlite:ormlite-android:4.48'

ps: Solution:

Android 6.0 (api 23) no longer supports HttpClient, so just add useLibrary 'org. apache. http. legacy' to build. gradle, as shown in the figure:

Error: (633, 16) Error: Symbol not found Symbol: Method sqrt (float) Location: Class FloatMath

Solutions

The reason is that FloatMath. sin () is not supported by Android 6.0. There are two main ways to solve this problem.

Method 1:

Compile with SDK version under 231. Set compileSdkVersion below 23 in the gradle. build file (including gradle.build for project and gradle.build for module).

Method 2:

Where the error is reported above, that is, replace FloatMath class with Math class, Math. sin ();

Method 3:

(float) Math. sqrt Replaces FloatMath. sqrt


Related articles: