I have an android studio project, the project has an application module and a library module. The application module is simply a demo of the library module. The library module depends on a third party, for the sake of simplicity, let's call it external.jar.
Android studio could successfully build the library module, generate a library named classes.jar.
I noticed that android studio also copied assets from external.jar into classes.jar along with .class files. The problem emerged when I was trying to build an apk from the application module, because the application module depends on external.jar, android studio refused to assemble the final apk due to duplicated assets, and showed the following error message:
Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'.
com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK assets/ap1.data
File1: /Users/skyrim/android/Demo/app/libs/classes.jar
File2: /Users/skyrim/android/Demo/app/libs/external.jar
I have to release the library module in binary form to third parties, they may also use classes.jar along with external.jar, so excluding unnecessary assets is a must.
The following is my library module's build.gradle:
apply plugin: 'com.android.library'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
useLibrary 'org.apache.http.legacy'
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.2.0'
}
How can I prevent android studio from copying assets from external.jar into classes.jar ? Of course, I can simply unzip classes.jar and move unnecessary assets and re-zip it, but I prefer some less hackish solutions.
I'm new to android development and gradle build system, any suggestions are welcome.
Related
I am trying to build and imported Android project in Android Studio/Eclipse.
My goal is to write automated test to the current project. First, I am trying to build the project and then to make an apk file of it so I will be able to execute real device/emulator tests on it.
Here are my available Gradle tasks
There is no build or test or assemble and etc. tasks which are I am looking to use so I will reach my goal.
Here is my project tree and both build.gradle files
`
apply plugin: "java"
repositories {
jcenter()
}
dependencies {
compile 'org.slf4j:slf4j-api:1.7.13
testCompile 'junit:junit:4.12'
}
and
apply plugin: 'com.android.application'
version = "1.2"
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
}
}
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "sdk.mobfox.com.appcore"
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
multiDexEnabled true
archivesBaseName = "MobFox-Android-SDK-Client-" + version + ".apk"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dexOptions {
javaMaxHeapSize = "4g"
}
lintOptions {
abortOnError false
}
}
repositories {
jcenter()
mavenCentral()
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.google.android.gms:play-services-ads:11.0.0'
compile 'com.android.support:multidex:1.0.1'
compile 'com.danikula:videocache:2.7.0'
testCompile 'junit:junit:4.12'}
I tried to open the project in Android Studio but got the same state.
I opened a new Gradle project in Eclipse and saw that the tasks I am looking for are available there - I believe because of the 'java-library' plugin which added to the build.gradle root file but I use the same plugin in my root build file and did not receive what I expected.
I was succeeded to execute the Gradle "tasks" which gave me the next response in console :
Working Directory: C:\Users\orit\Desktop\mobFox\MobFox-Android-SDK-master\MobFox-Android-SDK-master
Gradle User Home: C:\Users\orit.gradle
Gradle Distribution: Specific Gradle version 4.1
Gradle Version: 4.1
Java Home: C:\Program Files\Java\jdk1.8.0_91
JVM Arguments: None
Program Arguments: None
Gradle Tasks: tasks
:tasks
All tasks runnable from root project
Build tasks
assemble - Assembles the outputs of this project.
build - Assembles and tests this project.
buildDependents - Assembles and tests this project and all projects that depend on it.
buildNeeded - Assembles and tests this project and all projects it depends on.
classes - Assembles main classes.
clean - Deletes the build directory.
jar - Assembles a jar archive containing the main classes.
testClasses - Assembles test classes.
Build Setup tasks
init - Initializes a new Gradle build.
wrapper - Generates Gradle wrapper files.
Documentation tasks
javadoc - Generates Javadoc API documentation for the main source code.
Help tasks
buildEnvironment - Displays all buildscript dependencies declared in root project 'MobFox-Android-SDK-master'.
components - Displays the components produced by root project 'MobFox-Android-SDK-master'. [incubating]
dependencies - Displays all dependencies declared in root project 'MobFox-Android-SDK-master'.
dependencyInsight - Displays the insight into a specific dependency in root project 'MobFox-Android-SDK-master'.
dependentComponents - Displays the dependent components of components in root project 'MobFox-Android-SDK-master'. [incubating]
help - Displays a help message.
model - Displays the configuration model of root project 'MobFox-Android-SDK-master'. [incubating]
projects - Displays the sub-projects of root project 'MobFox-Android-SDK-master'.
properties - Displays the properties of root project 'MobFox-Android-SDK-master'.
tasks - Displays the tasks runnable from root project 'MobFox-Android-SDK-master'.
Verification tasks
check - Runs all checks.
test - Runs the unit tests.
Rules
Pattern: clean: Cleans the output files of a task.
Pattern: build: Assembles the artifacts of a configuration.
Pattern: upload: Assembles and uploads the artifacts belonging to a configuration.
To see all tasks and more detail, run gradle tasks --all
To see more detail about a task, run gradle help --task
BUILD SUCCESSFUL in 0s
1 actionable task: 1 executed
**1. What is the reason I cannot get all the task I got when I open a new project?
How can I add these tasks so I will be able to create apk file, build and execute integration tests?**
task, you write in project level build.gradle file. See below pic for the reference.
You are posting module level build.grale file.
Applying com.android.application should be enough, you should not apply java plugin on the root either and add dependencies there.
Operate on your app project, with Java specific stuff.
For more reference how to structure your project.
https://developer.android.com/studio/build/index.html
https://github.com/mustafaakin/image-matcher
I imported this project using AndroidStudio's "Import Eclipse Studio Project". Edited .properties 'OpenCV - x.x.x' to 'opencv'. This is the error:
/home/uttaran/Downloads/h1/h/src/main/res/layout/main_layout.xml
Error:(62) No resource identifier found for attribute 'camera_id' in package 'in.mustafaak.imagematcher'
Error:(62) No resource identifier found for attribute 'show_fps' in package 'in.mustafaak.imagematcher'
Error:Execution failed for task ':h:processDebugResources'.
> com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/home/uttaran/Android/Sdk/build-tools/23.0.3/aapt'' finished with non-zero exit value 1
Information:BUILD FAILED
As Daniel K already stated correctly:
To important an Android Application/Library in Android Studio, the project you want to import needs a build.gradle or pom.xml file in the root of the project so that the build system (Gradle/Maven) that comes with Android Studio can resolve dependencies etc. during the project import...
The Github project you refering too is this?
https://github.com/mustafaakin/image-matcher
Apparently it got written with the Eclipse IDE...and pushed to Github without any build file...
You can import it anyway, by manually creating a build.gradle file in the project root and then try to import the application again.
A possible working build.gradle file (depends on your Android Studio's Gradle plugin Version etc. I just assume now you copied your build.gradle file from a working project of yours) would be:
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion '23.0.3'
defaultConfig {
minSdkVersion 21
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
productFlavors {
}
}
repositories {
mavenCentral()
flatDir {
dirs 'libs'
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.android.gms:play-services-base:8.4.0'
compile 'com.android.support:support-v4:23.3.0'
compile 'com.android.support:appcompat-v7:23.3.0'
}
If you did this, it probably will not find the OpenCV library dependency (red lines under OpenCV classes/methods and in imports) and you have to go in Android Studio to:
File | Project Structure | image-matcher-master | Dependencies | +
And there you add your OpenCV library as "File dependency"...if it is a JAR...otherwise if opencv in your Download folder is the source...you load this in Android Studio as well and choose "Module dependency".
If this doesn't work...this could be helpful as well:
http://docs.opencv.org/3.0-beta/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html
UPDATE:
main_layout.xml contains things like:
<org.opencv.android.NativeCameraView
android:id="#+id/tutorial1_activity_native_surface_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick="cameraclick"
android:visibility="gone"
opencv:camera_id="any"
android:layout_weight="1"
opencv:show_fps="true" />
So your OpenCV library comes with resources and views that can't be found, so you still have some problem with your src structure in your project.
This answer might be related:
Android Studio can't find opencv modules, but compiles ok
If nothing works I would recommend to import an OpenCV Android sample, check if it builds and compiles and then compare the src structure difference from your project to the sample project...
Open the Android native Camera using OpenCV
I'm a newbie in Android especially for libs.
I'm using Android Studio 1.2.2
I'm trying to create a jar library with no activity, resources, etc (only code) that use the AltBeacon library. When complete I whould like to include this jar in my APP that has to use beacons through my lib.
Well, using AltBeacon instructions, I'm able configure gradle to have a "normal" (with activity etc) APP using AltBeacon library. But I'm not able to create a jar library that contains the AltBeacon one.
I google a lot especially in SO, I found a lot of questions about "library inside library" but that doesn't worked for me.
I made this:
I created an empty project with Studio (blank activity)
Then I created a new module -> Java Library and called it libmebeaconmanager and Studio created a new Java class that I called MeBeaconManager
I modified the build.gradle of the library in this way:
apply plugin: 'java'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'org.altbeacon:android-beacon-library:2+'
}
This is the build.gradle of Module:app:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "23.0.0 rc2"
defaultConfig {
applicationId "me.padovame.libbeacontest"
minSdkVersion 18
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
}
And this is the build.gradle of project:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
At this point, in the MeBeaconManager class of the lib, I tried to import AltBeacon classes without success because I got an undefined symbol error for import lines like this:
import org.altbeacon.beacon.Beacon;
I also tried, without success, inserting in lib folder of libmebeaconmanager the downloaded android-beacon-library-2.3.aar
What am I doing wrong?
Thanks in advance.
EDIT:
I tried to include a jar (tried with Picasso) library in my libmebeaconmanager and it worked. So the problem seems to be with aar libs.
I'm going crazy...
EDIT 2:
I extracted classes.jar from the AltBeacon aar lib and used as other jar and it worked.
But I consider this as a workaround: I still do not understand how to manage directly aar files. So thanks to whom can help me.
Seems like there is a problem in inserting library to project structure
You can have a look at answer provided by user Sam Rad here:
How do I add a library project to Android Studio?
I'm trying to use some .jar files as libraries in my Android Studio project.
I've been looking on how to do it, and the regular way is to copy the .jar file in the libs folder, and then add it as library. I know the libs folder must be inside "app" module in the "project" view. But that option to add as library doesn't appear. So my approach is to add it manually doing right click on "app" and:
Open Module Setings/app/Dependencies/ and there add .jar files as file dependency.
Then, I go to my .class but the import suggestions don't let me select these libraries as import.
I have tried another approach from the "android" view. Doing right click on "app" and selecting "new module", there I select Import .JAR or .AAR Package and then I do the same as before from Open Module Setings, but this time I add it as Module Dependency.
In booth ways, the gradle.build file is updated adding these dependencies, but when going to the activity to select the import, it doesn't give me the chance to select the import from these ones.
I have spent a full day trying to solve this, but I don't get my activity to recognize the imports from these files, so I would appreciate if someone could tell me what I'm doing wrong or what I'm missing to do.
Top-level build.gradle file:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
app-level build.gradle file:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.myapp"
minSdkVersion 14
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { res.srcDirs = ['src/main/res', 'src/main/res/values-v14'] } }
packagingOptions {
exclude 'META-INF/LICENSE.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile project(':activation')
compile project(':mail')
}
My current project using gson-2-2.4.jar and YouTubeAndroidPlayerApi.jar and some others. To import those jar, I put it in my libs folder. After that, go to build.gradle and delete some characters in it, and rewrite (1). Finally, click "sync now" (2). I don't know why my sync button does not work when I add jar to libs, so I do that, and it work so well.
UPDATE
On Android 3.x.y we can use Sync Project with Gradle Files or Refresh all Gradle projects
Firstly, I import this JAR, AS can not recognize this JAR and I can not import.
And than I press right click in this JAR and choose "Add as Library". AS can recognize this JAR and solve the problem!!
With Respect to Android Studio 1.5.1:
You need to copy and paste the JAR file into:
APP_NAME/app/libs
Note that the libs folder will already exist. Once copied, change the project view from Android to Project files.
Open up the app/libs folder and right click the JAR file, then click Add As Library.
That should do it. I wasn't able successfully use the Add Module to import a JAR file.
After a chat with M D we arrived to the conclusion that my AS does not recognize the .jar files, so I have had to port the project back to eclipse, and it works fine there.
I have been using zxing module as an android library for some time without problems. I have imported my Eclipse project into Android Studio (which I use for everything else!) and the zxing library is added as a module.
In one of my classes in the main module I reference the com.google.zxing.android.client.Intents class which is in the zxing module. I can write code in Android studio and these references have the correct imports selected.
When I run a gradlew clean build I am getting messages saying 'cannot find symbol class Intents'.
In settings.gradle I have includes for both my main module and the zxing module.
In the build.gradle of my main module I have 'compile project(':zxing'). These were both added during the import from Eclipse. I can also see that the zxing module is being built as part of the gradlew clean build.
I have tried deleting the zxing module and references then trying to add it manually. I have compared the way that this module is implemented to another project with a similar module that works. All looks fine.
Is anyone able to make a suggestion?
Edit:
settings.gradle file
include ':app'
include ':captureActivity'
build.gradle of main module
apply plugin: 'com.android.application'
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.wigglyamps.littlegreenbutton"
minSdkVersion 10
targetSdkVersion 19
versionCode 1
versionName "1.0"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile project(':captureActivity')
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:19.+'
}
Java class file in main module
public class test {
public void hello () {
String i = Intents.Scan.ACTION;
}
}
I don't really know the zxing framework, but have you tried to use the maven artifact instead of referencing a module?
If you want to use the artifact add this to the dependencies of the project/module that uses zxing: compile 'com.google.zxing:core:3.1.0'
Edit: You said you imported the project from eclipse; I'm in the middle of transitioning to gradle and the import function hardly produces anything useful if the eclipse project contained references to library projects (not jar's). I rebuild my projects from scratch by creating a new app(with modules) in Android Studio and copy/paste code and resources into the new app's directories