English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

About the incompatibility problem after adding fragment in Android

The generation and introduction of Fragment

Android runs on a variety of devices, from small-screened phones to extra-large-screen tablets and TVs. In many cases, a set of apps for phones is developed first, then a copy is made, and the layout is modified to adapt to tablets and super-large screens. Can't an app adapt to both phones and tablets at the same time? Of course, it must be able to. The emergence of Fragment is to solve this problem. You can treat Fragment as a component of an Activity's interface, even the interface of the Activity can be composed entirely of different Fragments. Even more impressive is that Fragment has its own lifecycle and can receive and process user events, so there is no need to write a lot of control event handling code in the Activity. More importantly, you can dynamically add, replace, and remove a Fragment.

Yesterday, I learned about Android's Fragment. According to the tutorial on the official website, I created a class called BlankFragment, inheriting from Fragment. Then, an error occurred during compilation:

Error:Execution failed for task ':app:processDebugManifest'.
> Manifest merger failed : Attribute meta-data#android.support.VERSION@value value=(26.0.0-beta1) from [com.android.support:design:26.0.0-beta1] AndroidManifest.xml:28:13-41
  is also present at [com.android.support:support-v4:26.1.0] AndroidManifest.xml:28:13-35 value=(26.1.0).
  Suggestion: add 'tools:replace="android:value"' to <meta-data> element at AndroidManifest.xml:26:9-28:44 to override.

A search was conducted, and all sorts of things were said. The most unreliable one was to say that the tag in the manifest file should be changed from android:name to class. It is clearly related to the version from the logs! Later, a solution that is compatible with the version was finally found, and the following code needs to be added to the build.gradle under the app directory:

configurations.all {}}
  resolutionStrategy.eachDependency { DependencyResolveDetails details ->
    def requested = details.requested
    if (requested.group == 'com.android.support') {
      if (!requested.name.startsWith("multidex")) {
        details.useVersion '26.0.0-beta1'
      }
    }
  }
}

试了一下确实好了。但不能满足于此。

注意,就在添加的这段代码上面,描述的是工程依赖的库:

依赖项 {
  实现 fileTree(dir: 'libs', include: ['*.jar'])
  实现 'com.android.support:appcompat-v7:26.0.0-beta1'
  实现 'com.android.support.constraint:constraint-布局:1.0.2'
  实现 'com.android.support:design:26.0.0-beta1'
  实现 'com.android.support:support-v4:26.1.0'
  testImplementation 'junit:junit:4.12'
  androidTestImplementation 'com.android.support.test:runner:0.5'
  androidTestImplementation 'com.android.support.test.espresso:espresso-核心:2.2.2'
}

而这一行下面有红线:

实现 'com.android.support:appcompat-v7:26.0.0-beta1'

鼠标悬停提示:

所有 com.android.support 库必须使用完全相同的版本规范 
(混合版本可能导致运行时崩溃). 
找到的版本 26.1.0, 26.0.0-beta1. 示例包括 com.android.support:support-compat:26.1.0 
and com.android.support:animated-vector-drawable:26.0.0-beta1

This is said very clearly: all libraries that depend on com.android.support must use the same version! Look at the Dependency, com.android.support:appcompat-v7:26.0.0-beta1and com.android.support:support-v4:26.1.0 two versions are contradictory! Look at the git records,26.1.0 on this line is the newly added one, it must be the automatically added by AndroidStudio when creating the BlankFragment class. Look back at the error log, isn't it also the same meaning?

I have to complain about AndroidStudio. If you are going to automatically add version dependencies, why not check the version compatibility at the same time? Otherwise, it's better not to add it, let us do it ourselves. I hate this kind of semi-automated thing the most, and it's the most deceptive.

In addition, the syntax of the gradle file is also very interesting. The comments are C/C++The style is Python, the function declaration is Python, and the Lambda expression is a bit like C#.

Summary

The above-mentioned is about the incompatibility problem after adding fragment in Android introduced by the editor to everyone. I hope it will be helpful to everyone. If you have any questions, please leave a message, and the editor will reply to everyone in time. Thank you very much for your support of the Naoan tutorial website!

Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You may also like