Update to Android Studio 3.0 project migration problems encountered solutions

Recently, the Android Studio 3.0 version was officially launched, and some problems encountered in the migration of previous projects were solved.

1. gradle and buildToolsVersion version

Android Studio 3.0 requires that the gradle version is 4.1 and the corresponding buildToolsVersion is 26.0.2. Then remember to add google() to the outermost build.gradle of the project. If you don't add it, some official dependencies will not be downloaded.

buildscript {
    repositories {
        ...
        google()
    }
}

**

2. Module dependency changes

**
Originally relying on the module used compile, now it needs to be replaced with api or implementation.
api: The module is available at compile time, and the user of the module is available at compile time and run time. This is the same as the obsolete compile.
implementation: The module is available at compile time, and the user of the module is available at runtime. For projects that use a large number of libraries, the compile time can be significantly improved because it can reduce the build system to recompile some modules.
So when to use api and when to use implementation? Since the company’s project adopts component-based development, there is a common module that needs to be dependent on each component. At the beginning, implementation was adopted. As a result, it was found that other components could not reference the library in common (common -> A module, A module could not Reference common dependent libraries).
After the experiment, I came to the conclusion that when this module will be referenced multiple times, api should be used, and implementation will not be referenced by other modules.
Send me the way I am now

dependencies {
    api fileTree(include: ['*.jar'], dir: 'libs')
    api 'com.squareup.okhttp3:okhttp:3.4.2'
    ...
    debugApi 'com.squareup.leakcanary:leakcanary-android:1.5.1'
    releaseApi 'com.squareup.leakcanary:leakcanary-android-no-op:1.5.1'
}

compileOnly replaced provided, and runtimeOnly replaced apk.

**

3. apt plug-in replacement

**

The apt plug-in has been deprecated and needs to be replaced with annotationProcessor.

//apply plugin: 'android-apt'

dependencies {
    ...
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
}

**

4. Incompatibility of third-party libraries

**

· Retrolambda
Android Studio 3.0 already supports Java 8 and does not require third-party libraries to support it, so the retrolambda library needs to be removed.
The outermost build.gradle of the project

buildscript {
    ...
    dependencies {
        ...
        //classpath 'me.tatarka:gradle-retrolambda:3.2.5'
    }
}

module 中的 build.gradle

//apply plugin: 'me.tatarka.retrolambda'

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

dependencies {
    ...
    //retrolambdaConfig 'net.orfjackal.retrolambda:retrolambda:2.3.0'
}

· Butterknife The
latest version of butterknife is 8.8.0, which is not compatible with Android Studio 3.0, and you will be prompted

Caused by: java.lang.NoSuchMethodError: com.android.build.gradle.tasks.ProcessAndroidResources.getPackageForR()Ljava/lang/String;
        官方 issue 已经有人提过这个问题,貌似是 gradle 的问题。解决办法:版本降级到 8.5.1 即可解决。

· Projects have multiple versions of the compilation fails with a third library
do not know if Android Studio 3.0 compiler now tighter than ever before and some libraries have not been unified version is now compiled directly enough. Solution: Unify the version of the third-party library.
Enter gradle app:dependencies in Terminal (gradle environment configuration can be Baidu)

...
+--- com.meituan.android.walle:library:1.1.5
|    +--- com.android.support:support-annotations:24.1.1 -> 25.2.0
|    \--- com.meituan.android.walle:payload_reader:1.1.5
...
        如果出现了 com.android.support:support-annotations:24.1.1 -> 25.2.0 代表该库中有自己依赖的库被升级了,需要去除这个依赖。
dependencies {
    ...
    api('com.meituan.android.walle:library:1.1.5') {
        exclude(group: 'com.android.support', module: 'support-annotations')
    }
}

The group is: the package name in front, and the module name in the back.
If the dependency is a jar package, the wording is exclude(module:'libs/xxx.jar').
exclude(group:'com.android.support') is to ignore all com.android.support modules.

    另外 apk 的输出目录变了,多了一层目录结构 debug 。
    由 app/build/outputs/apk 变为 app/build/outputs/apk/debug,如果是用 Jenkins 打包项目拷贝文件的脚本需要修改下。

Guess you like

Origin blog.csdn.net/yjh_SE007/article/details/78470859