Error: MainActivity must extend android.app.Activity [Instantiatable] - android

I tried upgrading Android Gradle Plugin from 4.2.2 to 7.0.1 using the upgrade assistant which is available in Android Studio at Tools > AGP Upgrade Assistant. The only change it made was to my project-level build.gradle file:
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.1' // changed from 4.2.2 to 7.0.1
// ...
}
}
However, now when I run ./gradlew assemble assembleAndroidTest I get the following error:
/builds/locuslabs/android-team/locuslabs-android-sdk/app/src/main/AndroidManifest.xml:21: Error: MainActivity must extend android.app.Activity [Instantiatable]
android:name="com.locuslabs.appsdk.MainActivity"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Explanation for issues of type "Instantiatable":
Activities, services, broadcast receivers etc. registered in the manifest
file (or for custom views, in a layout file) must be "instantiatable" by
the system, which means that the class must be public, it must have an
empty public constructor, and if it's an inner class, it must be a static
inner class.
1 errors, 0 warnings
Lint found fatal errors while assembling a release target.
To proceed, either fix the issues identified by lint, or modify your build script as follows:
...
android {
lintOptions {
checkReleaseBuilds false
// Or, if you prefer, you can continue to check for errors in release builds,
// but continue the build even when errors are found:
abortOnError false
}
}
My project is multi-module, but I don't suspect that as the problem since it's complaining about the application module, not a library module.
I believe my <activity> tag is well formed in my AndroidManifest.xml for my application module:
<activity
android:name="com.locuslabs.appsdk.MainActivity"
android:label="#string/app_name"
android:windowSoftInputMode="adjustNothing">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Furthermore, I don't think there is anything wrong with extending AppCompatActivity instead of android.app.Activity as I'm doing in my MainActivity.kt:
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
// ...
}
I'm concerned that Android Gradle Plugin 7.0.1 is not really ready for prime-time because the Android Gradle Plugin documentation still says classpath 'com.android.tools.build:gradle:4.2.0' instead of 7.0.1.
I saw that the Android Gradle Plugin 7.0.1 release notes mentioned some changes to linting but none of those changes seemed relevant to me.
I also skimmed through the Android Gradle Plugin source code to see if I could find the linting stage any identify any changes but it looked like a lot of work to find that code and do that analysis.
I searched for answers but all I could find were these two stackoverflow entries where the error was legitimate and the programmer just needed to change their code to ensure they were referencing an actual Activity:
Android Studio Error: Activity must extend android.app.activity
MainActivity cannot be cast to android.app.Activity
I also tried Android Gradle Plugin 7.0.0 but got the same error. Only Android Gradle Plugin 4.2.2 prevents the error.
Is this a bug in Android Gradle Plugin 7.0.1?
Update: could not disable Instantiatable
I tried to disable the Instantiatable lint error the following ways but none of them prevented the error.
First, I tried adding disable "Instantiatable" to my application-level build.gradle file:
android {
lintOptions {
disable "Instantiatable"
}
}
Second, I tried prepending #SdkSuppress("Instantiatable") to the class:
#SdkSuppress("Instantiatable")
class MainActivity : AppCompatActivity() {
// ...
}
Similarly, I tried #SuppressLint("Instantiatable") but that didn't work either.

the Android Gradle Plugin documentation still says classpath 'com.android.tools.build:gradle:4.2.0' instead of 7.0.1.
You need to read further down the page, to this and this. That table is only relevant for pre-7.0.0 versions.
Is this a bug in Android Gradle Plugin 7.0.1?
Quite possibly. Or, perhaps beyond, as the Instantiatable Lint check has a history of problems.
If your scenario does not match one of those three August 2021 bugs, and you are in position to provide a reproducible test case, file a fresh issue! Beyond that, if a clean-and-rebuild is not clearing up your problem, you might need to simply disable the Instantiatable Lint check for the time being by adding the following to all of your build.gradle files at the application or library level (i.e. all except your project-level build.gradle):
android {
lintOptions {
disable "Instantiatable"
}
}

Remember you can use the lint config xml file and add this "Instantiatable" rule as "ignored":
<lint>
[...]
<issue id="Instantiatable" severity="ignore" />
</lint>
In addition, you should configure lint plugin in your build.gradle to use the lint.xml file:
android {
[...]
lint {
lintConfig = file("$rootDir/config/lint.xml")
}
}

Related

How would you troubleshoot dependency errors that arise in Release mode only in Android?

Context
We have an Android app built in Kotlin and we've split our algorithms layer into a separate Android Project. We import that as a dependency using Gradle using the following code:
In build.gradle:
dependencies {
...
implementation project(':algos-android')
}
This algos-android project has its own dependency - koma - defined in the algos-android project's build.gradle file as follows:
dependencies {
...
implementation group: "com.kyonifer", name:"koma-core-ejml", version: "0.12"
}
Issue
This works completely fine when running the app in a debug mode (defined in buildTypes in the build.gradle). However, when running in release mode, we are seeing an error at runtime when our algorithms layer uses its sub-dependency - koma.
The following error shows up in the console:
java.lang.IllegalStateException: No double matrix factories available. (Did you forget to import a koma-core implementation?)
My team was under the assumption that Gradle handles sub-dependencies for us. Has anyone run into issues like this before? Are there any good debug approaches?
Things We've Tried
Moving all of the files from our algos-android project directly into the main project and importing koma directly in the main project's gradle.build file.
Environment
Android Studio 4.0.1
Kotlin Version 1.3.72
Gradle Version 6.3
Subdependency Repo:
https://github.com/kyonifer/koma
We were finally able to track down the issue and resolve this with the following addition to the proguard-rules.pro file:
-keep class koma.** { *; }
We were able to find that since the Matrix factory classes are inflated in koma sources using reflection, proguard considered them as unused and simply deleted them from the release build.
Note: If you see this and make this fix, don't forget to Clean Project before running.

Android: Unresolved reference when using junit 4.13

After updating junit version to 4.13 in gradle dependencies, classes and annotations like Assert, #Test, etc. under the junit package are displayed as red when used in my code. Lint check says:
Unresolved reference: <any junit class>
However, when I build and run my tests, it will build and run just fine.
I have tried:
restarting Android Studio
Invalidate caches and restart
Clean and rebuild project
added testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
What works is downgrading junit to 4.12. How to get rid of this lint error, without downgrading version?
Update:
When I check ALT + ENTER options and select Inspection 'Unresolved reference, in wrong test scope' options > Suppress 'IncorrectScope' for file <name of file>, it gets rid of these lint errors for that particular file. I still like to solve this problem without using Suppress though.
According to this issue, it looks like this bug is not fixed yet. For now I'm downgrading to 4.12.
Removing both espresso and runner from dependencies also fixes the problem.
Just add the following to your build.gradle (App) file:
configurations.all {
resolutionStrategy.force "junit:junit:$junit_version"
}
dependencies {
...
}
The first of all check your external dependencies
In my case I found double jUnit versions which is
Junit 4.12
Junit 4.13
And the older version is come with androidx.test
The solution is
1. exclude dependencies
or
2. remove `androidx.test`
or
3. add conflict startegy
Borrowing from #VaheGharibyan's idea about duplicate junit versions, I discovered that the following code also fixes the problem.
configurations.all {
resolutionStrategy.dependencySubstitution {
substitute module('junit:junit:4.12') with module('junit:junit:4.13')
}
}

Program type already present when generating signed apk [duplicate]

I'm trying to generate a release build but im not able because of mutidex issues my project has all the multidex enabled and dependencies added
The error i'm receiving is :
Execution failed for task ':app:transformClassesWithMultidexlistForRelease
Caused by: com.android.build.api.transform.TransformException: Error while generating the main dex list.
and aslo:
Caused by: com.android.tools.r8.errors.CompilationError: Program type already present: com.myapp.BuildConfig
You are getting this error because you a have library module which has the same package name as the app module.
The solution would be to change package name of your library module. You can follow the accepted answer in this SO which describes how to change the package name in android studio.
In my case It was happening when I try to run older project on new installed Android studio
The problem solved by running Build->Clean Project
Note: As friends say on comments this is solution for a flutter project
I solved this error enabling multiDexEnabled in the build.gradle of my app's module:
defaultConfig {
...
...
...
multiDexEnabled true
}
Error: Program type already present: somemodule/BuildConfig
Cause
In my case I had a (hidden) circular dependency which Android Studio did not find:
testutils/build.gradle uses implementation project(':somemodule')
somemodule/build.gradle had `androidTestImplementation project(":testutils")
Solution
in my case the second dependency was not neccessary so I removed it
Just goto tools>Flutter>Flutter clean in android studio.
It'll resolve the issue. (If you are working with flutter)
I had this problem after an android x upgrade in android studio. To fix this I went to File->Open and opened the android folder within my current flutter project. I was then able to go to Build->Clean Project as suggested by #Seymour Mammadli.
Hopefully this helps someone with the same issue.
I encountered the issue building a Flutter application and I resolved it following the official guide:
https://developer.android.com/studio/build/multidex
You simply have to:
0. (Android: Go to Refactor > Migrate to AndroidX
(If you are on a Flutter project, to migrate the module, you have to go to Tools > Flutter > Open for Editing in Android Studio
1. (Both) Modify the module-level build.gradle file to enable multidex and add the multidex library as a dependency, as shown here:
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 28
multiDexEnabled true
}
...
}
dependencies {
implementation 'com.android.support:multidex:1.0.3'
}
2. (non-Flutter) 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>
2. (Flutter) 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=".App" >
...
</application>
</manifest>
3.(Flutter) Create a custom class under project/android/app/src/main/[java or kotlin folder]/[your/package/appName]
kotlin version: App.kt
package your.package.appName
import io.flutter.app.FlutterApplication
import android.content.Context
import androidx.multidex.MultiDex
class App : FlutterApplication() {
override fun attachBaseContext(base: Context) {
super.attachBaseContext(base)
MultiDex.install(this)
}
}
java version: App.java
package your.package.appName;
import io.flutter.app.FlutterApplication;
import android.content.Context;
import androidx.multidex.MultiDex;
public class App extends FlutterApplication {
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
4. (Both) Celebrate if you did it!! :D
For more info, check out the official guide ;)
https://developer.android.com/studio/build/multidex
I got the same error. This type of application is not as generic as we do except. IN my application development I was using the below mentioned dependencies
youtube_player_flutter: ^6.1.0+7
webview_flutter: ^0.3.20+2
since youtube_player_flutter internally it makes use of webview_flutter as one of the dependencies. I removed the dependencies webview_flutter and it worked.
try add packageBuildConfig(false) in your library module:
android{
...
packageBuildConfig(false)
}
Open with Android studio
Build > Clean Project
build > Build bundle (bundle / apk)
Build with android studio work for me. instead of with gradlew console
go to android folder and fire the below command
./gradlew clean
In my case (Developing a plugin), doing these steps worked:
cd example/android (example is the project path)
./gradlew app:clean
cd ..; flutter clean; flutter packages get
This issue can also occur if your buildDir is the same for two or more modules in a project.
Change your package name, ex:
Your current package name:
com.example.appname
to:
com.example.appname.app
I meet the error when i adding flutter to existing project, the message indicates that AndroidX causes the problem.
when i modify the AndroidX at the bootom of flutter_module/pubspec.yaml
AndroidX:false
and try again , it works;

Android Studio showing Errors(Missing Translation) after Updating

Recently I have Updated my Android Studio from 2.2.3 to 2.3.0 project was running fine in 2.2.3 but after updating project dependency classpath from 2.2.3 to 2.3.0 getting lots of Missing Translations error from string.xml.
Can anyone tell me why it is happening and How to Resolve these Errors ?
Have many methods to fix this:
First method:
Add to build.gradle:
android {
lintOptions {
disable 'MissingTranslation'
}
}
Second method:
It's the ignore attribute of the tools namespace in your strings file, as follows:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:tools="http://schemas.android.com/tools"
tools:ignore="MissingTranslation" >
<!-- your strings here; no need now for the translatable attribute -->
</resources>
Third method:
In your ADT go to window->Preferences->Android->Lint Error Checking
Find there Missing Translation and change its Severity to Warning.
Reference link:
http://www.fasteque.com/missingtranslation-issue-for-release-builds/
Hope it helpful for you.
I was facing the exact same issue. Based on my observation, this problem was introduced with Gradle plugin version 2.3.0. It occurs when there is a module in your project that supports more languages than your app or other modules. You could start editing the translations of such modules but that makes them less maintainable.
If you disable the Lint checks using any of the suggestions here or for similar questions you also ignore actual translation errors in your app where a translation is missing for a language you are supporting. You need to be very careful maintaining your strings after that.
There is one more, also suboptimal, option: change the plugin version back to the last one not showing these symptoms in your build.gradle.
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
}
There is however a way to make this work without losing vital warnings/errors and without reverting to old tool versions. You can find it in Google's documentation and also in this answer.
android {
defaultConfig {
...
resConfigs "en", "fr"
}
}
This removes not needed resources and the warnings and errors along with them. I have updated my code and it builds fine now.
There is another issue that was introduced with the new Gradle plugin version: If you get false positives, i.e. errors complaining about missing translations for a string you have marked non-translatable, check if the string name exists in another module. In that case rename or provide the translations and the error disappears.

MockContentResolver is not found in an Android Test project with separate module

I'm trying to setup a test project like is described in the Android Testing Blueprint but I receive the following NoClassDefFoundError:
java.lang.NoClassDefFoundError: android.test.mock.MockContentResolver
Android Studio resolves this correctly but when running I receive this error.
It's worth to note that I do not have an androidTest configuration on the app project, instead I only have a separate tests module with:
apply plugin: 'com.android.test'
I'm running tests like this:
./gradlew :tests:connectedAndroidTest
Test project to reproduce this issue can be found here:
https://github.com/vexdev/android-testing-templates/tree/master/AndroidTestingBlueprint
EDIT: Also asked on Android Development community
EDIT: Also created following android issue:
https://code.google.com/p/android/issues/detail?id=200182&thanks=200182&ts=1454489567
As #rds said, test package is not part of the framework on the device, therefore you need to include the package.
Seems like com.android.test plugin is not adding those classes, so you can fix it by adding
compile 'com.google.android:android-test:4.1.1.4'
in your dependencies for the module where you are applying the plugin.
Some android packages are not automatically linked, but you have to explicitly specify them using the <uses-library> tag. That is the case with the android.test package (see uses-library docs), like #rds mentioned.
Adding a compile dependency will work if you have minSdkVersion > =15, but will add a legacy Maven artifact that has been updated on the Aug 24, 2012 for the last time.
IMHO, it might be a better solution to add the following snippet to the manifest of the test module:
<application>
<uses-library android:name="android.test.runner" />
</application>
This will tell the system to include the android.test package as well, so the MockContentResolver will be found by the class loader.
Hope it helps.
The MockContentProvider is not part of the framework, it's only shipped in the SDK for test apks.
In your Android Studio project, you must store the source files for local unit tests under a specific source directory (src/test/java)

Categories

Resources