android.support.multidex.MultiDexApplication无法转换为应用名称

时间:2018-07-10 20:06:18

标签: android multidex

添加multidexandroid:name="android.support.multidex.MultiDexApplication"清单后出现错误,这是我的build.gradle文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 26
buildToolsVersion '26.0.2'

defaultConfig {
    applicationId "com.projects.realestatefinder"
    minSdkVersion 15
    targetSdkVersion 26
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
    vectorDrawables.useSupportLibrary = true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        resValue "string", "google_maps_api_key", "AIzaSyBugjavAKmDVOmR0HMhD83Cyru5V0GsCro"
    }
    debug {
        resValue "string", "google_maps_api_key", "AIzaSyBugjavAKmDVOmR0HMhD83Cyru5V0GsCro"
    }
}

packagingOptions {
    exclude 'META-INF/DEPENDENCIES'
    exclude 'META-INF/NOTICE'
    exclude 'META-INF/LICENSE'
    exclude 'META-INF/LICENSE.txt'
    exclude 'META-INF/NOTICE.txt'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:26.0.2'
compile 'com.android.support:design:26.0.2'
compile 'com.android.support:multidex:1.0.0'
compile files('libs/apache-mime4j-0.6.jar')
compile files('libs/httpclient-4.3.4.jar')
compile files('libs/httpcore-4.3.2.jar')
compile files('libs/httpmime-4.3.4.jar')
compile files('libs/jackson-annotations-2.2.2.jar')
compile files('libs/jackson-core-2.2.2.jar')
compile files('libs/jackson-databind-2.2.2.jar')
compile files('libs/twitter4j-async-4.0.2.jar')
compile files('libs/twitter4j-core-4.0.2.jar')
compile files('libs/twitter4j-media-support-4.0.2.jar')
compile files('libs/twitter4j-stream-4.0.2.jar')
compile files('libs/universal-image-loader-1.9.2-SNAPSHOT-with-sources.jar')
compile 'com.google.android.gms:play-services-maps:8.4.0'
compile 'com.google.android.gms:play-services-location:8.4.0'
compile 'com.google.android.gms:play-services-ads:8.4.0'
compile 'com.facebook.android:facebook-android-sdk:4.+'
compile 'com.jakewharton:butterknife:8.2.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.2.1'
compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.5.0'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.melnykov:floatingactionbutton:1.3.0'
compile 'com.crystal:crystalrangeseekbar:1.1.3'
compile 'com.anthonycr.grant:permissions:1.0'
  }

multidex类

public class YouApplication extends MultiDexApplication {
@Override
protected void attachBaseContext(Context base) {
    super.attachBaseContext(base);
    MultiDex.install(this);
}

}

维护       

xmlns:android="http://schemas.android.com/apk/res/android"
package="com.projects.realestatefinder">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SEND_SMS" />

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.telephony" android:required="false" />

<application
    android:name="com.projects.realestatefinder.YouApplication"
    android:largeHeap="true"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".SplashActivity"
        android:theme="@style/SplashTheme"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name="com.projects.activities.BuiltInActivity"
        android:theme="@style/AppTheme"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:label="@string/built_in"/>

    <activity
        android:name="com.projects.activities.SettingsActivity"
        android:theme="@style/AppTheme.SettingsTheme"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:label="@string/settings"/>

    <activity
        android:name="com.projects.activities.ImageViewerActivity"
        android:theme="@style/AppTheme"
        android:label="@string/view_image"/>

    <activity
        android:name="com.projects.activities.AgentListingActivity"
        android:theme="@style/AppTheme"
        android:label="@string/agents"/>

    <activity
        android:name="com.projects.activities.ProfileActivity"
        android:theme="@style/AppTheme"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:label="@string/profile"/>

    <activity
        android:name="com.projects.activities.PropertyResultActivity"
        android:theme="@style/AppTheme"
        android:label="@string/results"/>

    <activity
        android:name="com.projects.activities.SearchResultActivity"
        android:theme="@style/AppTheme"
        android:label="@string/search_results"/>

    <activity
        android:name="com.projects.activities.RegisterAgentActivity"
        android:theme="@style/AppTheme"
        android:label="@string/register_agent"/>

    <activity
        android:name="com.projects.activities.RegisterActivity"
        android:theme="@style/AppTheme"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:label="@string/register"/>

    <activity
        android:name="com.projects.activities.RealEstateEditActivity"
        android:theme="@style/AppTheme"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:label="@string/edit_real_estate"/>

    <activity
        android:name="com.projects.activities.RealEstateAddActivity"
        android:theme="@style/AppTheme"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:label="@string/add_real_estate"/>

    <activity
        android:name="com.projects.activities.PropertyTypeActivity"
        android:theme="@style/AppTheme"
        android:label="@string/property_type"/>

    <activity
        android:name="com.projects.activities.MyRealEstateActivity"
        android:theme="@style/AppTheme"
        android:label="@string/my_real_estates"/>

    <activity
        android:name="com.projects.activities.MapActivity"
        android:theme="@style/AppTheme"
        android:label="@string/map"/>

    <activity
        android:name="com.projects.activities.SearchActivity"
        android:theme="@style/AppTheme"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:label="@string/search"/>

    <activity
        android:name="com.projects.activities.AgentDetailActivity"
        android:theme="@style/AppTheme"
        android:label="@string/agent_details"/>

    <activity
        android:name="com.projects.activities.DetailActivity"
        android:theme="@style/AppTheme"
        android:label="@string/real_estate_details"/>

    <activity
        android:name="com.projects.activities.LoginActivity"
        android:theme="@style/AppTheme"
        android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
        android:label="@string/login"/>

    <activity
        android:name="com.projects.realestatefinder.MainActivity"
        android:theme="@style/MainTheme"
        android:label="@string/app_name"/>

    <!--
         The API key for Google Maps-based APIs is defined as a string resource.
         (See the file "res/values/google_maps_api.xml").
         Note that the API key is linked to the encryption key used to sign the APK.
         You need a different API key for each encryption key, including the release key that is used to
         sign the APK for publishing.
         You can define the keys for the debug and release targets in src/debug/ and src/release/.
    -->
    <meta-data
        android:name="com.google.android.geo.API_KEY"
    android:value="@string/google_maps_api_key" />

    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>

</application>

当我尝试在android 4.4中运行该应用程序时,它崩溃了。当我打开GPS位置时,我进行了大量搜索,但不知道问题出在哪里。请让我知道如何解决此问题。

崩溃日志如下。

java.lang.ClassCastException: android.support.multidex.MultiDexApplication cannot be cast to com.application.RealEstateApplication
                  at com.projects.fragments.MapFragment.refetch(MapFragment.java:207)
                  at com.projects.fragments.MapFragment.onMapReady(MapFragment.java:200)
                  at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source)
                  at com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source)
                  at android.os.Binder.transact(Binder.java:361)
                  at fs.b(:com.google.android.gms.dynamite_dynamitemodulesb@12685003@12.6.85 (000306-197041431):20)
                  at com.google.android.gms.maps.internal.bg.a(:com.google.android.gms.dynamite_dynamitemodulesb@12685003@12.6.85 (000306-197041431):5)
                  at com.google.maps.api.android.lib6.impl.be.run(:com.google.android.gms.dynamite_dynamitemodulesb@12685003@12.6.85 (000306-197041431):5)
                  at android.os.Handler.handleCallback(Handler.java:733)
                  at android.os.Handler.dispatchMessage(Handler.java:95)
                  at android.os.Looper.loop(Looper.java:136)
                  at android.app.ActivityThread.main(ActivityThread.java:5476)
                  at java.lang.reflect.Method.invokeNative(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:515)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
                  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

扩展com.application.RealEstateApplication

android.app.Application

并在清单中执行:

android:name="com.application.RealEstateApplication"

然后在MultiDex.install(this);中的onCreate()

答案 1 :(得分:0)

您应该从MultidexApplication扩展Application类,并在清单中使用它,而不是直接使用android.support.multidex.MultiDexApplication

public class MyApplication extends MultiDexApplication
 {
 ...
 }

并替换您的清单中

android:name="com.projects.realestatefinder.MyApplication"