Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't the app run under Android 4.x in Kotlin?

Tags:

android

kotlin

There is a very simple app made by Kotlin, it can work well under Android 8.0, Android 6.0 and Android 5.1 in Android Studio 3.0

but I get the error message "java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{info.dodata.mirror/ui.UIMain}: java.lang.ClassNotFoundException: ui.UIMain" when I run the app under Android 4.1. 2 (This is a real mobile phone) and Android 4.2.2

You can test it by download the app at https://www.dropbox.com/s/9f0yxp5pqqxtcxq/Mirror.zip?dl=0

enter image description here

UIMain.kt

package ui
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import info.dodata.mirror.R
class UIMain : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_main)
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="info.dodata.mirror">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name="ui.UIMain">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

build.gradle (Module:app)

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 26
    flavorDimensions "default"

    defaultConfig {

        // Enabling multidex support.
        multiDexEnabled true

        applicationId "info.dodata.mirror"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.01"
        archivesBaseName = "BackupSettings-V" + versionName
    }

    productFlavors {
        free {
            applicationId "info.dodata.mirror"
        }

        pro {
            applicationId "info.dodata.mirror.pro"
        }
    }


    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            buildConfigField "boolean", "IsDebugMode", "false"
        }

        debug {
            buildConfigField "boolean", "IsDebugMode", "true"
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:multidex:1.0.2'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    implementation "org.jetbrains.anko:anko-commons:$anko_version"
    implementation "org.jetbrains.anko:anko-sqlite:$anko_version"
}

build.gradle (Top)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.1.51'
    ext.anko_version = '0.10.2'

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
like image 440
HelloCW Avatar asked Nov 10 '17 02:11

HelloCW


People also ask

Is Kotlin better than flutter?

Conclusion. When it comes down to making a choice between either Flutter or Kotlin for mobile application development, all frameworks and programming languages have their ups and downs. But for startups or companies looking to keep the cost relatively low in building out their MVP, Flutter is a great choice.

Is Kotlin recommended for Android?

Kotlin is an expressive and concise programming language that reduces common code errors and easily integrates into existing apps. If you're looking to build an Android app, we recommend starting with Kotlin to take advantage of its best-in-class features.

Is Android Studio good for Kotlin?

Android Studio provides first-class support for Kotlin. It even has built-in tools to help you convert Java-based code to Kotlin. The Show Kotlin Bytecode tool lets you to see the equivalent Java-based code as you learn Kotlin.

Can you write Android apps in Kotlin?

Kotlin is an officially supported language for developing Android apps, along with Java.


1 Answers

On Android 5.0 and later, multiple .dex files are automatically loaded by ART together. However, on earlier Android versions, Dalvik starts the application with only the main classes.dex, and the application must load the secondary the .dex files before anything else is done.

This is handled by the multidex support library, but you did not follow the instructions. This means that some of your classes are not present when the system is trying to start your activity, leading to a crash.

Depending on whether you override the Application class, perform one of the following:

  • If you do not override the Application class, edit your manifest file to set android:name in the <application> tag as follows:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.myapp">
        <application
                android:name="android.support.multidex.MultiDexApplication" >
            ...
        </application>
    </manifest>
    
  • If you do override the Application class, change it to extend MultiDexApplication (if possible) as follows:

    public class MyApplication extends MultiDexApplication { ... }
    
  • Or if you do override the Application class but it's not possible to change the base class, then you can instead override the attachBaseContext() method and call MultiDex.install(this) to enable multidex:

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

You are currently not overriding Application, so you should follow the first case.

like image 68
ephemient Avatar answered Oct 27 '22 23:10

ephemient