AndroidStudio Library resource sharing - android

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!

Related

Class Not Found when build APK when using dynamic feature

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')
}

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'
}
}
}

Android Gradle excluding jar from APK

Gradle appears to be excluding one of the jars I have listed in the dependencies of my Android project. All the projects are included correctly, as well as everything else except this one jar. Perhaps it's something wrong with the jar, but the project builds just fine.
The jar which is excluded was built from this project: https://github.com/pardom/InAppBillingLibrary
I used apktool (https://code.google.com/p/android-apktool/) to have a look at the resulting apk, and none of the classes from that jar are included.
Here's my Gradle build file
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
apply plugin: 'android'
dependencies {
compile 'com.android.support:support-v4:13.0.0'
compile fileTree('libs')
compile project(':libraries:ActionBarSherlock:actionbarsherlock')
compile project(':libraries:AndroidUtils')
compile project(':libraries:AndroidCommon')
compile project(':libraries:ChartView:library')
}
android {
compileSdkVersion 17
buildToolsVersion '17.0.0'
}
Make sure that the jar is not compiled with Java 1.7. Try recompiling it using Java 1.5 or 1.6.
Currently, Android can't handle any Java 1.7 compiled classes.

Android Studio and Gradle - build fails

I am building a small library project along wit a sample project to illustrate the use. I can't manage to run the sample in Android Studio. I have created the project from scratch. I am experienced with Eclipse but it's my first try at Android Studio & Gradle.
The error given:
Gradle: Execution failed for task ':demo:dexDebug'.
Running C:\DevTools\Android\android-studio\sdk\build-tools\android-4.2.2\dx.bat
failed. See output
I have the following folder structure:
- demo
- build
- libs
- android-support-v4.jar
- src
- main
- java
- res
- build.gradle
- library
- build
- libs
- android-support-v4.jar
- src
- main
- java
- res
- build.gradle
- build.gradle
- settings.gradle
Build.gradle at project root:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
Settings.gradle at project root:
include ':library', ':demo'
Build.gradle for the library module:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android-library'
dependencies {
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
Build.gradle for the sample module:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4'
}
}
apply plugin: 'android'
dependencies {
compile project(':library')
compile files('libs/android-support-v4.jar')
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 7
targetSdkVersion 16
}
}
Specifying compile files('libs/android-support-v4.jar') means that every library includes support v4. What you want to do is just specify that every library depends on it:
dependencies {
compile 'com.android.support:support-v4:13.0.0'
}
This will allow gradle to detect all dependencies and include this only once.
Note: You have to first use the SDK Manager and download and install two Maven repositories: "Android Support Repository" and "Google Repository".
I found the problem:
I removed that line from the sample gradle file.
compile files('libs/android-support-v4.jar')
However, I have no idea why this does not work (if I have 2 or 3 external libraries that all depend on the support library, how are we supposed to do, without touching their gradle files?
You should navigate to your libs folder in the IDE, right click on the jar and select to add the library to the project, it still needs to establish the dependency even though the jar appears to be there. Also look at your gradle built script to make sure the dependency appears there. If that still doesnt work just run a gradle clean on the project. Intellij documentation will give you more details on what clean does. see:
stackoverflow gradle build
This error could be encountered while migrating from Groovy to kotlin DSL as well and here are the steps to get rid of it:
If you are still in the process of migrating please complete the migration of gradle files first, use kts syntax and then sync gradle files.
Use this dependency inside your build.gradle(app level):
implementation("androidx.legacy:legacy-support-v4:1.0.0")
Remove id("kotlin-android-extensions") from plugins block inside build.gradle.kts (app level).
That's it! 3rd Point solved the issue for me but trying all the points should definitely fix the issue.
In my Case replace this line
classpath "com.android.tools.build:gradle:7.0.2"

Android gradle build and the support library

I have a project that uses a few other library projects (SlidingMenu, ActionbarSherlock) and both of these use the android support library, when building I am getting the following:
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Landroid/support/v4/app/LoaderManager;
at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
at com.android.dx.command.dexer.Main.processClass(Main.java:490)
at com.android.dx.command.dexer.Main.processFileBytes(Main.java:459)
at com.android.dx.command.dexer.Main.access$400(Main.java:67)
at com.android.dx.command.dexer.Main$1.processFileBytes(Main.java:398)
at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:245)
at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:131)
at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:109)
at com.android.dx.command.dexer.Main.processOne(Main.java:422)
at com.android.dx.command.dexer.Main.processAllFiles(Main.java:333)
at com.android.dx.command.dexer.Main.run(Main.java:209)
at com.android.dx.command.dexer.Main.main(Main.java:174)
at com.android.dx.command.Main.main(Main.java:91)
Both library projects have a dependency on support lib:
dependencies {
compile files('libs/android-support-v4.jar')
}
This is now possible by downloading Android Support Repository from the SDK Manager, and replacing
compile files("libs/android-support-v4.jar")
with
compile 'com.android.support:support-v4:13.0.0'
This has to be done for all projects that use the support library. The Android Support Repository is automatically added to your list of repositories by the build system (Unsure of which part adds it, don't know enough gradle yet).
Source
Until we have the support library has a repository artifact you cannot include it in more than one library project.
You could create a library project that only contains the support library, and have all other libraries depend on it.
Update: this is now possible.
Based in the answer from Xav, if you have other modules that depends on android-support-v4.jar,
create a library project which contains the android-support-v4.jar and reference this project instead the jar file.
E.g.:
Add a project with this structure:
- android-support
- libs
- android-support-v4.jar
- AndroidManifest.xml
- build.gradle
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.example.support.lib">
<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="7"/>
<application />
</manifest>
build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.4.2'
}
}
apply plugin: 'android-library'
dependencies {
compile files ("libs/android-support-v4.jar")
}
android {
compileSdkVersion 17
buildToolsVersion "17"
defaultConfig {
minSdkVersion 7
targetSdkVersion 7
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
}
}
}
remember to include this project in your projects settings.gradle:
include ':android-support'
now, for each project that requires the support library, instead of
compile files ("libs/android-support-v4.jar")
use the following line:
compile project (':android-support')
FYI, I had to add this to exclude the android-support-v4.jar in my gradle build because I added it as an artifact:
compile fileTree(dir: 'libs', include: '*.jar', exclude: 'android-support-v4.jar')
I created the build.gradle using the project export feature in Eclipse's ADT plugin.
The ADT will throw an exception like UNEXPECTED TOP-LEVEL EXCEPTION if your Eclipse classpath contains more than one class of the same name/package/jars. In this case it is encountering more than one instance of the LoaderManager class.
Solution :
You have same jar library included twice. Check your application and all referenced Android libraries and make sure you have all jars included exactly once.

Categories

Resources