当我在Android Studio 2.0中创建空白活动并尝试在API 16上运行该活动时,它将正确编译。当我在具有API 23的仿真器上运行它时,它运行无误。当我在API 16上运行它时,在应用启动时会给我很多错误。见下文。奇怪的是该应用程序运行正常。
这可能是什么原因?
在Android Studio中,我无效并清除了缓存。还清理了项目并对其进行了重建。
供参考的代码:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="nl.myapp.test"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
MainActivity.Java
package nl.myapp.test; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="nl.myapp.test.MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" /> </RelativeLayout>
Gradle 2.0.0构建文件build.gradle(Module App)
apply plugin: 'com.android.application' android { compileSdkVersion 23 buildToolsVersion "23.0.3" defaultConfig { applicationId "nl.myapp.test" minSdkVersion 16 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.3.0' }
应用在API 16上启动时的logcat错误
04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.app.ActivityManager$TaskDescription', referenced from method nl.myapp.test.MainActivity.access$super 04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method nl.myapp.test.MainActivity.access$super 04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method nl.myapp.test.MainActivity.access$super 04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method nl.myapp.test.MainActivity.access$super 04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method nl.myapp.test.MainActivity.access$super 04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.transition.TransitionManager', referenced from method nl.myapp.test.MainActivity.access$super 04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.app.SharedElementCallback', referenced from method nl.myapp.test.MainActivity.access$super 04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method nl.myapp.test.MainActivity.access$super 04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method nl.myapp.test.MainActivity.access$super 04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.app.assist.AssistContent', referenced from method nl.myapp.test.MainActivity.access$super 04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.view.SearchEvent', referenced from method nl.myapp.test.MainActivity.access$super 04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.UserHandle', referenced from method nl.myapp.test.MainActivity.access$super 04-10 18:29:29.945 23899-23899/nl.myapp.test E/dalvikvm: Could not find class 'android.os.PersistableBundle', referenced from method nl.myapp.test.MainActivity.access$super
在Android Studio中,我去了 File- > Project Structure-> Project。
首先,我将Gradle升级到 2.11 ,将Android插件版本升级到 2.0.0 。接下来,我同步了项目并对其进行了编译。错误仍然存在。
之后,我将Android插件版本降级为 1.5.0 。
现在,当我编译并运行项目时,错误消失了。