Android Studio - java module recompiled on each application recompile - java

In my Android Studio project, I've added a java module that produces a JAR file which is used by the main module. I understand that Gradle rebuilds all modules on each compile process, but it is clear that it is not needed for java modules if there is no changes since the last build.
So I want to prevent gradle from building my jar file if there is no code changes in the java module, I cannot find such option
Any help is appreciated.

there're two steps to achieve what u want:
Ask the android plugin to pre-dex the libraries.
so you add dexOptions inside the android bracket
android {
dexOptions {
preDexLibraries true
}
}
and the other step is to make your debug build to be minSdkVersion 21, that way the system will build as a multi-dex application so it won't need to merge your dexed library into the APK.
There's a whole section on the dev page about it https://developer.android.com/studio/build/multidex.html#dev-build, but what you want to do is add productFlavors inside the android bracket
android {
productFlavors {
// Define separate dev and prod product flavors.
dev {
// dev utilizes minSDKVersion = 21 to allow the Android gradle plugin
// to pre-dex each module and produce an APK that can be tested on
// Android Lollipop without time consuming dex merging processes.
minSdkVersion 21
}
prod {
// The actual minSdkVersion for the application.
minSdkVersion YOUR_APP_MINIMUM_HERE
}
}
}

Related

targetSDKVersion, minimumSDKVersion, and compileSDKVersion for specific device

I'm programming for one specific device and I'm having trouble understanding how I should be setting minsdkversion, compilesdkversion, and targetsdkversion. I want to get a compiler error when I'm using a library that doesn't exist for API level 25. Here's what I gatherered. Minsdkversion prevents calling API that doesn't exist for API version. Compilesdkversion should always be the latest version. Targetsdkversion is what the app was tested against (don't understand what this means).
minsdkversion-25
compilesdkversion-28 (needed for AndroidX)
targetSDKversion-25
Version settings
Among those version settings, only minSdkVersion takes effect to detect API level error. If you'd like to know details of the version settings, see documentation.
How to let build system report API level error
For release builds, place "lintOptions" in app/build.gradle and mark "NewApi" as "fatal". Then lintVitalRelease gradle task reports error during compilation.
android {
lintOptions {
fatal "NewApi"
}
compileSdkVersion 30
defaultConfig {
minSdkVersion 25
targetSdkVersion 30
:
}
:
}
Checking API level manually
In debug builds, the lintOptions above doesn't have any effects because the lint is not included in debug build tasks (there is no lintVitalDebug gradle task). So rather, after coding, run "code inspection" manually to detect all the NewApi problems in your code. If incompatible API uses found, you'll see "calling new methods on older version" error in the inspection result.

RunTime exception: DexArchiveMergerException: Unable to merge dex in android studio 3 [duplicate]

Error:java.lang.RuntimeException: java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
Error:java.lang.RuntimeException: com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
Error:com.android.builder.dexing.DexArchiveMergerException: Unable to merge dex
Error:com.android.dex.DexException: Multiple dex files define Lcom/google/zxing/integration/android/IntentResult;
I have same problem with Android Studio 3.0 beta 4. I found a solution.
1. From the Build menu, press the Clean Project button.
2. After task completed, press the Rebuild Project button from the Build menu.
For Android Studio 3.0 what I did was to add this to my gradle:
multiDexEnabled true
And it worked!
Example
android {
compileSdkVersion 25
buildToolsVersion '26.0.2'
defaultConfig {
applicationId "com.xx.xxx"
minSdkVersion 15
targetSdkVersion 24
versionCode 9
versionName "1.0"
multiDexEnabled true //Add this
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
shrinkResources true
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
So I solved this issue by doing the following:
Delete the ./gradle folder inside your project
Delete all the build folders and the gradle cache. I ran the following command:
How ?
cd ~/[your project root folder] && find . -name build -exec rm -rf {} \; && rm -rf $HOME/.gradle/caches/
Assuming your gradle config files are in the $HOME/.gradle folder.
Inside Android Studio, went to File > Invalidate caches / Restart... and invalidated the caches and restarted it.
You should be able to get to the cause of this error by inspecting your dependencies with gradle and looking for duplicates like this:
./gradlew -q app:dependencies
In my case, the following error was happening at build time:
Duplicate zip entry [httpcore-4.4.1.jar
and it was resolved by doing this in my build.gradle:
implementation ('me.dlkanth:stetho-volley:1.0') {
exclude group: 'org.apache.httpcomponents'
}
If your minSdkVersion is 21 or higher
android {
defaultConfig {
multiDexEnabled true
}
}
if your minSdkVersion is 20 or lower
1) you must add the following library in dependencies
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
2) Create a java class then extend it from Application and override attachBaseContext method.
public class MyApplication extends Application {
#Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
}
3) Mention the created class inside manifest in application tag.
<application
android:name=".MyApplication"
.
.
.
</application>
Check your dependencies for latest version usually it is inconsistency with the version of any of your dependencies.
1.Build > Clean Project
2. Rebuild your project
Check the verbose log for the dependency causing the merge issue and update the same. Repeat the same for each dependencies.
I faced same issue in Android Studio 3.0.1, I tried all possible cases nothing worked for me as mentioned above, finally I solved it by
Solution1:
Close Android Studio
Delete .gradle folder located at C:\Users\YourComputerName\.gradle not from app's .gradle
Restart android studio
It will download all the necessary jars and add to your build path
Solution2:
Delete duplicate jar from libs folder in app/libs
Rerun the app again
Because if there is duplicate jar file which already defined in build.gradle file it causes issue.
This solved the issue for me. It may help others..
This error can have multiple reasons.
No clean and rebuild or anythging like that did the job for me.
For me the problem was the dependency:
compile 'org.jetbrains:annotations-java5:15.0'
I removed it and everything worked fine.
Change "compile" to "compileOnly"
this is what worked for me.
In my case the culprit was SendGrid lib, added this and it got fixed:
compile 'com.github.danysantiago:sendgrid-android:1',{
exclude group: 'org.apache.httpcomponents', module: 'httpclient'
}
I am using Studio 3.0.0 release. In my case there was the only solution: I have removed old JARs from the "libs" folder in my project: it was "ksoap2" package with dependencies. Packages obtained with gradle usually do not conflict with each other, at least if you are using the most popular ones. But an old JAR in libs folder may crush your build if it includes all own dependencies.
I have tried other comments from removing gradle and bla bla bla, but adding multiDexEnabled true solving my problem, I investigate that my apk has reached Over 64K Method.
Ref: https://developer.android.com/studio/build/multidex.html)
I am using
Android Studio 3.0.1
Build #AI-171.4443003, built on November 9, 2017
I delete jar file from libs folder. And that work fine for me
I am using Android Studio 3.0.1 and was facing the same problem. If others answer doesn't works try this:
Tools -> Android -> Sync Project with Gradle Files
It worked for me Sync Project with Gradle Files
Sometime it can happens due to same library (jar file) present two times or your project has reached 64k methods limit.
Solution:
1) Remove Databinding if your project use this
2) Remove same type library from lib folder or, delete .gradle file from c//user//your_pc//.gradle
3) apply this in your build.gradle
android {
defaultConfig {
multiDexEnabled true
}
}

Android project error

I am trying to run an app that has a Google Maps Activity on my phone. My phone's running on Android 4.1.3 and the Minimum SDK for my project is API 15: Android 4.0.3
I just created the project on Android Studio and tried to run it on my phone. But it won't run and I am keep getting this error. Can someone tell me how to solve this?
That error probably means that you are using too much libraries. And because you are trying to use google maps I'm guessing you included everything google.
Try to just use the libraries you need.
For maps use this:
dependencies {
com.google.android.gms:play-services-maps:9.2.1
}
Reading material:
https://developers.google.com/android/guides/setup
You need to configure your gradle for multidex.
Modify the module-level build.gradle file configuration to include the support library and enable multidex output, as shown in the following code snippet:
android {
compileSdkVersion 21
buildToolsVersion "21.1.0"
defaultConfig {
...
minSdkVersion 14
targetSdkVersion 21
...
// Enabling multidex support.
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.0'
}
See more on same link which you are refering : https://developer.android.com/studio/build/multidex.html#mdex-gradle
You can also see some related question for same error:
How to enable multidexing with the new Android Multidex support library
Android java.exe finished with non-zero exit value 1

Android studio - Failed to find target android-18

I have a problem with Android Studio 0.2.3.
When I run my project the build stops and appears message that says:
Gradle: Execution failed for task ':AppName:compileDebugAidl'.
> failed to find target android-18
although I have installed the SDK platform of Android 4.3 (API 18) and I tried to reinstall all the SDK. I've also added the ANDROID_HOME variable in the system variables.
What seems to be the source of this error?
I think you might not have the Android-18 sdk installed. Go to Tools > Android > SDK Manager and check to see if Android 4.3 (API 18) is installed.
I solved the problem by changing the compileSdkVersion in the Gradle.build file from 18 to 17.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 10
targetSdkVersion 18
}
}
dependencies {
compile 'com.android.support:support-v4:13.0.+'
}
STEP 1) Start Android SDK Manager
With android command something as below,
$ /usr/local/android-studio/sdk/tools/android
STEP 2) Find API 18
STEP 3) Select Android 4.3 (API 18 ) and install packages.
What worked for me in Android Studio (0.8.1):
Right click on project name and open Module Settings
Verify SDK Locations
Verify Gradle and Plugin Versions (Review the error message hints
for the proper version to use)
On the app Module set the Compile SDK Version to android-L (latest)
Set the Build Tools version to largest available value (in my case
20.0.0)
These changes via the UI make the equivalent changes represented in other answers but is a better way to proceed because on close, all appropriate files (current and future) will be updated automatically (which is helpful when confronted by the many places where issues can occur).
NB: It is very important to review the Event Log and note that Android Studio provides helpful messages on alternative ways to resolve such issues.
Thank you RobertoAV96.
You're my hero. But it's not enough. In my case, I changed both compileSdkVersion, and buildToolsVersion. Now it work. Hope this help
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ..
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
If you had the problem, opened SDK manager, installed the requested updates, returned to Android Studio and had the problem again, IT IS RECOMMENDED TO RESTART ANDROID STUDIO befor trying anything else.
Gradle will run automatically and chances are that your problem will be over. You will very possibly be told install the appropriate SDK TOOLS package, which is found in your SDK MANAGER under the second tab (sdk's are not the same as sdk tools, they are complementary packages).
You don't even need to hunt the tools package, if you click on the link under the error message, Android Studio should call SDK Manager to install the package automatically.
Restart Android Studio again and you should be up and running much faster than if you attempted workarounds.
RULE OF THUMB> restart your application before messing with options and configurations.
Check the local.properties file in your Studio Project. Chances are that the property sdk.dir points to the wrong folder if you had set/configured a previous android sdk from pre-studio era.
This was the solution in my case.
I had the same problem when trying out Android Studio. I already had projects running on the ADT under SDK 18. No need to hack the manifest files.
Fixed by:
export ANDROID_HOME= pathtobundle/adt-bundle-linux-x86_64-20130729/sdk
If you don't have the ADT installed, and just want the SDK, it seems like a good solution is to install everything and then point Android Studio to the just the packaged SDK.
cd pathtobundle
wget http://dl.google.com/android/adt/adt-bundle-linux-x86_64-20130729.zip
unzip *.zip
As someone else said, you may need to run the SDK Manager to install the desired packages before running Studio.
I've had a similar problem occurr when I had both Eclipse, Android Studio and the standalone Android SDK installed (the problem lied where the AVD Manager couldn't find target images). I had been using Eclipse for Android development but have moved over to Android Studio, and quickly found that Android Studio couldn't find my previously created AVDs.
The problem could potentially lie in that Android Studio is looking at it's own Android SDK (found in C:\Users\username\AppData\Local\Android\android-studio\sdk) and not a previously installed standalone SDK, which I had installed at C:\adt\sdk.
Renaming Android Studio's SDK folder, in C:\Users... (only rename it, just in case things break) then creating a symbolic link between the Android Studio SDK location and a standalone Android SDK fixes this issue.
I also used the Link Shell Extension (http://schinagl.priv.at/nt/hardlinkshellext/linkshellextension.html) just to take the tedium out of creating symbolic links.
This will also happen if you have written compileSdkVersion = 22 e.g. (as used in the "new new" Android build system) instead of compileSdkVersion 22.
You can solve the problem changing the compileSdkVersion in the Grandle.build file from 18 to wtever SDK is installed ..... BUTTTTT
If you are trying to goin back in SDK versions like 18 to 17 ,You can not use the feature available in 18 or 18+
If you are migrating your project (Eclipse to Android Studio ) Then off course you Don't have build.gradle file in your Existed Eclipse project
So, the only solution is to ensure the SDK version installed or not, you are targeting to , If not then install.
Error:Cause: failed to find target with hash string 'android-19' in: C:\Users\setia\AppData\Local\Android\sdk
Target with hash string 'android-18' is corresponding to the SDK level 18. You need to install SDK 18 from SDK manager.

Android Studio 0.2 App that use Google Maps - Gradle modify

I'm developing an App in Android studio. And I want use a Google maps API, but I can't use UI to configure project settings. I tried some different instruction to add maps, but it didn't work. I have to modify somehow build.gradle file? Can you tell me how? Does someone have experience with that?
Thanks for every advice.
If you are on 0.2 it means that you already have Google Repository and Android Repository installed(from Android SDK: terminal$ android sdk).
One of them, has the Google Play services.
Here is a full build.gradle for your module:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
dependencies {
compile 'com.google.android.gms:play-services:3.1.36'
}
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 8
targetSdkVersion 16
}
}
Notice how(easily) the play services can be included.
Also gradle version is 0.5.+ so it can be auto updated!
Also another VERY important thing, that wasted me a lot of time is the minimum sdk version!
It must be 8 or above, since google play services aren't supported for lower versions!

Categories