Exclude a Class from the Build in Android Studio

Exclude a class from the build in Android Studio

AFAIK, IntelliJ allows to exclude packages. Open Project Structure (Ctrl + Alt + Shift + S in Linux) → ModulesSources tab.

However, if you would like to exclude only one class, use the Gradle build file.

Android Studio uses Gradle, so in the build.gradle file, add a custom SourceSet inside the android configuration that excludes your class, e.g.:

android {
compileSdkVersion 19
buildToolsVersion "19.0.3"

defaultConfig {
minSdkVersion 19
targetSdkVersion 19
packageName "org.homelab.lab"
testPackageName "org.homelab.lab.test"
}

sourceSets {
main {
java {
exclude '**/SomeExcludedClass.java'
}
}
androidTest {
java {
exclude '**/TestSomeExcludedClass.java'
}
}
}
}

Exclude one class file in gradle Android Studio

The exclude method of the configuration closure for a dependency excludes transitive dependencies. So, if your module dependency depends on other modules, you can exclude them from your build. You can check out the transitive dependencies of the 'com.facebook.android:facebook-android-sdk:4.14.0' module on its Maven repository info page.

If the BundleJSONConverter class exists in a transitive dependency, you can exclude the specific module in the same way you are trying now. Just specify the group, the module and the version, like you do for dependencies.

If you just want to exclude one class for a dependency jar, take a look at the jar jar links tool and its Gradle plugin. It allows you to alter included jars, e.g. to change packages or remove classes.

The following (shortened) example shows the usage of the plugin and some methods to alter the dependency jar:

compile jarjar.repackage {
from 'org.apache.hive:hive-exec:0.13.0.2.1.5.0-695'

archiveBypass "commons*.jar"
archiveExclude "slf4j*.jar"

classDelete "org.apache.thrift.**"
classRename 'org.json.**', 'org.anarres.hive.json.@1'
}

How can I exclude class in Android projects with Kotlin

To exclude all Kotlin files in a folder you can use the kotlin block in main sourceSets in your Gradle file like this:

android {
...

sourceSets {
main {
kotlin {
exclude '**/exclude/*.kt'
}
}
}
}

In this example the name of the folder to exclude is exclude and all of the Kotlin files in it are excluded from the final build.

Android build.gradle Exclude duplicate classes

You can find all the dependencies of your project with the command

./gradlew :app:dependencies

Bitmovin and Purchasely seems to both use exoplayer.
So to avoid conflicts you can remove the module exoplayer from either one of those dependencies.

In your case, I think you should remove it from Purchasely

implementation ("io.purchasely:core:2.4.7") {
exclude module: 'exoplayer-core'
exclude module: 'exoplayer-hls'
exclude module: 'exoplayer-dash'
exclude module: 'exoplayer-ui'
exclude module: 'extension-okhttp'
exclude module: 'extension-mediasession'
}

Gradle task to build Jar excluding android classes

The actual solution for this was not to exclude the classes in the jar gradle task but to change the dependencies of the project, using compileOnly instead of implementation when setting the com.android.support dependency.

The fix is:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compileOnly 'com.android.support:appcompat-v7:27.1.1'
testImplementation 'junit:junit:4.12'
}

Gradle exclude java class from lib replaced by own class to avoid duplicate

This is what I ended up adding after Fabio's suggestion:

//Get LUAJ
buildscript { dependencies { classpath 'de.undercouch:gradle-download-task:3.1.1' }}
apply plugin: 'de.undercouch.download'
task GetLuaJ {
//Configure
def JARDownloadURL='http://central.maven.org/maven2/org/luaj/luaj-jse/3.0.1/luaj-jse-3.0.1.jar' //compile 'org.luaj:luaj-jse:3.0.1'
def BaseDir="$projectDir/luaj"
def ExtractToDir='class'
def ConfirmAlreadyDownloadedFile="$BaseDir/$ExtractToDir/lua.class"
def JarFileName=JARDownloadURL.substring(JARDownloadURL.lastIndexOf('/')+1)
def ClassesToDeleteDir="$BaseDir/$ExtractToDir/org/luaj/vm2/lib/jse"
def ClassNamesToDelete=["JavaMethod", "LuajavaLib"]

//Only run if LuaJ does not already exist
if (!file(ConfirmAlreadyDownloadedFile).exists()) {
//Download and extract the source files to /luaj
println 'Setting up LuaJ' //TODO: For some reason, print statements are not working when the "copy" directive is included below
mkdir BaseDir
download {
src JARDownloadURL
dest BaseDir
}
copy {
from(zipTree("$BaseDir/$JarFileName"))
into("$BaseDir/$ExtractToDir")
}

//Remove the unneeded class files
ClassNamesToDelete=ClassNamesToDelete.join("|")
file(ClassesToDeleteDir).listFiles().each {
if(it.getPath().replace('\\', '/').matches('^.*?/(?:'+ClassNamesToDelete+')[^/]*\\.class$')) {
println "Deleting: $it"
it.delete()
}
}
}
}

I'll upload a version that works directly with the jar later.



Related Topics



Leave a reply



Submit