Class Not Found when build APK when using dynamic feature - android

I have menu module setup by 'com.android.dynamic-feature'. Everything working fine when I coding and run by android studio. When package apk by Build -> Build APK(s) it crash Class Not Found when I start activity in module.
Notes: the activity path is correct, I guess my module doesn't attach into app
Manifest module:
<dist:module
dist:instant="false"
dist:onDemand="false"
dist:title="">
<dist:delivery>
<dist:install-time />
</dist:delivery>
<dist:fusing dist:include="true" />
</dist:module>
module gradle
apply plugin: 'com.android.dynamic-feature'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply from: '../shared_dependencies.gradle'
Is there any mistake here? Thanks

If you use <dist:fusing dist:include="true" /> and sign your app by APK it still not include into your APK. dynamic-feature only use with AAB format.
If you still want to use APK format you have to use bundle-tool to generate universal APK from AAB
https://developer.android.com/studio/command-line/bundletool

Building Android project that has dynamic feature modules will always work on emulator as there is no delivery system as on Google Play. Also you should use Build -> Build Bundle(s)/APK(s)->Build Bundle(s) option when you are using feature modules.
In your main module's build.gradle file you need to set dynamicFeatures, e.g.:
android {
...
dynamicFeatures = [
':module1',
':module2',
':module3'
]
...
}
While in dynamic feature module's build.gradle file you need dependency for main module:
dependencies {
implementation project(':app')
}

Related

How to get android gradle project to prefer library dependency AAR over a JAR?

I have an android library project (call it my-lib) that produces both an AAR and a JAR using this trick in the build.gradle file:
android.libraryVariants.all { variant ->
def name = variant.buildType.name
def task = project.tasks.create "jar${name.capitalize()}", Jar
task.dependsOn variant.javaCompile
task.from variant.javaCompile.destinationDir
artifacts.add('archives', task);
}
This library project contains no Android resources and a JAR is produced for convenient use by some other systems that do not use maven or gradle. Android apps which declare they depend on this android library in their gradle file like so in their gradle file:
dependencies {
compile 'com.mycompany:my-lib:VERSION'
}
But these android apps are picking up the JAR file instead of the AAR and thus missing out on the proguard.txt file in the AAR which was placed there using a declaration like so in the library gradle file:
android {
buildTypes.all {
consumerProguardFiles 'proguard-rules.pro'
}
}
The pom file produced by the android gradle plugin is missing the packaging entry, it just looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>my-lib</artifactId>
<version>VERSION</version>
</project>
The packaging entry should specify aar so that other projects get the AAR instead of the JAR, but how do I do that? Maybe there is a way to produce a JAR with a different name so it doesn't conflict with the AAR?
Update:
I can't specify #aar in all my dependencies because some of the dependencies are libraries which specify the project using implementation project(':my-lib') and gradle doesn't accept #aar there. Then when I try to add the dependency to the app I get this error: D8: Program type already present with the name of a class that is in my-lib. I made sure that all my dependencies are referencing my-lib via implementation and not compile and I see that in the intermediate POM files for dependent libraries the reference looks like this:
<dependency>
<groupId>mycompany</groupId>
<artifactId>my-lib</artifactId>
<version>1</version>
<scope>runtime</scope>
</dependency>
So the runtime scope seems to be correct.
I'm using the https://github.com/dcendents/android-maven-gradle-plugin/ plugin to publish the library to my local maven cache so many other Android apps in different git repos can access the android library. This appears at the top of my library build.gradle file:
apply plugin: 'com.android.library'
apply plugin: 'android-maven'
group = 'com.mycompany'
version = '1'
repositories {
mavenLocal()
}
As the Gradle Android Maven plugin is a modification of the standard Maven plugin, according to the documentation, you can try this :
uploadArchives {
repositories {
mavenDeployer {
pom.packaging = 'aar'
}
}
}

'Unresolved reference' errors for android library module referenced in app module

I have problems referencing my android library modules in my projects. Beside the main app module I use to have an android library module with either util stuff or as data module. I reference it in app module like that:
dependencies {
implementation project(":data")
}
When I build the project, it´s giving me lot of error messages 'Unresolved reference: ...' for all stuff that I reference in the app module to the android library module. But the IDE itself doesn´t have a problem, Intelligent finds all classes, interfaces etc., imports are fine, nothing is red. The android library module itself builds and creates aar-file in the output. It´s the compileDebugKotlin task that fails
Any general idea what may be related to that?
Found the problem, my android library module was missing the kotlin configuration:
apply plugin: 'kotlin-android'
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlinVersion:<version>"
}
Although I used kotlin .kt files in it, it could build without and also
Tools -> Kotlin -> 'Configure Kotlin in projects'
had told me 'All modules with Kotlin files are configured'
Your module's build.gradle file should have:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
dependencies {
...
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
...
}
In my case It was apply plugin: 'kotlin-android',
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
and also added it on build.gradle
androidExtensions {
experimental = true
}
If it is a kotlin module make sure to add in its build.gradle file
apply plugin: 'kotlin'
I was using CoroutineWorkers and in my case I had to add work-runtime-ktx dependency to use it
implementation "androidx.work:work-runtime-ktx:2.4.0"

google-services.json No matching client found for package name with any module

I renamed my app module in application to presentation module.
I put google-services.json in my presentation/
Added classpath "com.google.gms:google-services:3.1.0"
Added plugin: apply plugin: 'com.google.gms.google-services'
And when i try Sync gradle, i get error:
Error:Execution failed for task ':presentation:processDebugGoogleServices'.
No matching client found for package name 'ru.company.acitive.activelife'
My build.gradle snippet:
dependencies {
classpath "com.android.tools.build:gradle:$android_plugin_version"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.google.gms:google-services:3.1.0"
}
My presentation/build.gradle snippet:
dependencies {
...
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
apply plugin: 'com.google.gms.google-services'
That means your app's application ID is "ru.company.acitive.activelife", but that same string wasn't found in your google-services.json file.
Looks like there's a typo in the part where it says "acitive". Should it be "active" instead?
In addition to ensuring that your gradle androidApplicationID matches your google-services.json (as discussed here), this message can also result from gradle not finding your keystore file.
Be sure to have these entries in your project level build.gradle file:
keystoreStoreFile = _________
keystoreStorePassword = ____________
keystoreKeyAlias = _______________
keystoreKeyPassword = ____________
And be sure to use the same four values when Android Studio prompts you for them during a "Generate Signed APK" opertation.
In my case, I was trying to build the example application ClassyTaxi that Google provides for illustrating subscription services. It has a project level build.gradle file that references a keystorePropertiesFile=keystore.properties. That properties file didn't exist, but there was an example-keystore.properties file that I renamed to keystore.properties and populated it with the four values I had selected. I then used those exact same four values when using Android Studio to generate my signed APK.
In my own case, the line below in my app-level build.gradle file was the culprit:
applicationIdSuffix ".debug"
I inherited the codebase from another developer, so I wasn't initially aware that this line existed.

InstantAppBundleException: No base split found! Base split APK is the one with no 'splitName' attribute set on the <manifest> tag

When trying to launch the Instant App, it reported
Side loading failed with message: Failure when trying to read bundel.
Failed to parse app: /data/local/tmp/aia/my_app.zip
When look at the logcat, it has this error
InstantAppBundleException: No base split found!
Base split APK is the one with no 'splitName' attribute set on the <manifest> tag
What did I miss?
I think you may have forgetten the baseFeature tag in your base module.
If you have a base module and 2 feature modules for examples your gradle should look like these (You need to pay attention to the correct plugins, the baseFeature=true tag and correct dependency declaration).
Base Module Gradle file:
apply plugin: 'com.android.feature'
android {
//this is mandatory for the base module of the project
baseFeature = true
...
}
dependencies {
...
feature project(':feature1')
feature project(':feature2')
application project(':hello-installed')
}
Feature1 module and Feature2 module Gradle files:
apply plugin: 'com.android.feature'
android {
...
}
dependencies {
...
implementation project(':base')
}
Instant App Module Gradle files:
apply plugin: 'com.android.instantapp'
dependencies {
implementation project(':base')
implementation project(':feature1')
implementation project(':feature2')
}
Full App Module Gradle files:
apply plugin: 'com.android.application'
android {
//classic gradle declaration for legacy apps
}
dependencies {
implementation project(':base')
implementation project(':feature1')
implementation project(':feature2')
//non instant libraries will only appear here
implementation project(':nonInstantLibrary')
}
Non Instant Compatible Module Gradle files:
//it will use the legacy library plugin
apply plugin: 'com.android.library'
dependencies {
...
implementation project(':base')
}

AndroidStudio Library resource sharing

today i was encounting a problem with Android Studio.
I have this project tree:
application (this is executable app)
ui (this is library)
ui-common (this is library)
ui-tablet (this is library)
ui-phone (this is library)
application use ui
ui use ui-tablet and ui-phone
ui-phone use ui-common
ui-tablet use ui-common
ui-common contains common java code, drawable and strings resources
now, i try compile project, but have next error:
"error: package home.test.ui.common does not exist on import home.test.ui.common.R;"
java classes are available normal, but R class did not exists... file build\source\r\debug\sta\android\ui\common generated normally
gradle's file for my projects:
-ui
...
apply plugin: 'android-library'
dependencies {
compile project(":ui-common")
compile project(":ui-phone")
compile project(":ui-tablet")
}
...
-ui-common
...
apply plugin: 'android-library'
dependencies {
}
...
-ui-phone
...
apply plugin: 'android-library'
dependencies {
compile project(":ui-common")
}
...
-ui-tablet
...
apply plugin: 'android-library'
dependencies {
compile project(":ui-common")
}
...
-application
...
apply plugin: 'android'
dependencies {
compile project(":ui")
}
...
in .iml files library is included too:
for ui-phone and ui-tablet project:
<orderEntry type="module" module-name="ui-common" exported="" />
for ui project:
<orderEntry type="module" module-name="ui-phone" exported="" />
<orderEntry type="module" module-name="ui-tablet" exported="" />
for application project
<orderEntry type="module" module-name="ui" exported="" />
how i can setup projects ui-common for sharing resource with ui-tablet and ui-phone project?
Update 1
ui-common\build\source\r contains only release folder, no debug folder are generated.
i try to change type of dependency from 'compile' to 'compile debug' for ui-common in ui-phone project. after this compilation of ui-phone project run normally, but i have error for other projects. why android studio / gradle not generated debug output? for other library both (debug and release generated normally)
ok, i was solve my problem. my mistake relative to package name and resource merge
in ui-phone project i must use ui-phone.R because resource already merged!

Categories

Resources