Android studio - How to use an executable jar file from gradle - android

So I have a custom_rules.xml file Im trying to rebuild in gradle. so far I have no problem figuring everything else out but the last part Im trying to accomplish is running a special apk signing tool for widevine drm on the unsigned apk that I generate with my gradle build process. Ive been looking online and in the gradle documentation all day but I have yet to find how to run an executable jar file from gradle. I know I have to create a task but thats about it. Any help would be much appreciated.
PS. Ive read a lot of the gradle documentation so please spare me any read the docs responses
EDIT:
this is what my task looks like at the moment and it compiles but Im not sure its actually doing anything
task (runApkSigTool , dependsOn: 'android', type: JavaExec) {
classpath files('apksigtool.jar')
main 'com.widevine.tools.android.apksigtool.ApkSigTool'
args[0] = apkLocation
args[1] = 'private_key.der'
args[2] = 'my.crt'
}
Im not sure if I have to use it like a method in the android section of my build.gradle file or what.

You could probably use the JavaExec task. Something like this:
task signApk(type: JavaExec) {
classpath files('path/to/executable.jar')
main 'com.foo.MainClass'
args ['-foo', 'bar']
}

Related

Cannot create new activity in android studio

I keep getting an IllegalStateException error in the event log while creating new activity in android studio:
11:27:15 InvalidReferenceException: Error executing FreeMarker template: The following has evaluated to null or missing:
==> srcDir [in template "root://activities/common/common_globals.xml.ftl" at line 41, column 34]
Tip: If the failing expression is known to be legally null/missing, either specify a default value with myOptionalVar!myDefault, or use when-presentwhen-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthessis: (myOptionVar.foo)!myDefault, (myOptionVar.foo)??
I've encountered the same problem and trying the following steps were working for me:
close android studio
delete folders:
.idea and
.gradle
re-open project using android studio
Method 1:Just Remove the following code from your app level build.gradle
debug {
storeFile file('/Users/Razz/Documents/keys/keystore.jks')
storePassword 'ABC#123'
keyAlias 'key0'
keyPassword 'ABC#123'
}
Method 2: In case, if there are two packages together (kotlin and java)
I was facing the same problem as whenever I create new activity form the activity templates provided by the android studio. Nothing was happening even after Gradle sync completes successfull.
After a long item, I figure out that, java directory name inside my project changed into kotlin somehow. And I supposed Android Studio looking for the java package to create new Activity.
Then, to solve this issue, I follow the below steps:
Closed Android Studio
Went to the project location up to java directory as ../ProjectName/app/src/main/
In this main directory, I had the kotlin directory instead of java.
So, I just renamed it to java.
Then opened the project with Android Studio, and that's all.
I created new activity it easily created one as it works before.
I have been faced that, after I setup my android environment.I already set up and configured everything, but this still occur.After searching a few mins, I found the problem in Logcat,
Then, I know project is missing Build Tool version. You have to give which build tool version you will use to run for project. Go to >> Project Structure , ( Cmd + ; ) in mac. Choose Compile Sdk Version and Build Tools Version,
After the gradle build, creating new activity from menu error has been fixed. I hope this will help for you.
Notes : Some of the projects from Github or bitbucket also missing Build Tools version when you cloned and run the project. These are the same errors and this approach can help to solve for them.
Comment the code in build.gradle which creates folders in tmp directory and then create the Activity.
After successful creation of activity ,revert back the commented code in build.gradle (Co)
allprojects {
/*String osName = System.getProperty("os.name").toLowerCase();
if (osName.contains("windows")) {
buildDir = "C:/tmp/${rootProject.name}/${project.name}"
}*/
repositories {
jcenter()
}
}
In the file build.gradle, add
buildDir = "C:/tmp/${rootProject.name}/${project.name}"
sychronize gradle then remove it and synchronize again. This worked for me.
allprojects {
buildDir = "C:/tmp/${rootProject.name}/${project.name}"
repositories {
jcenter()
}
}
I experienced to same issue 2 day ago and i solve it by update the below text. Check to gradle.build app and module to newer versions.
classpath 'com.google.gms:google-services:4.3.4'
I had the same issue.
Visit this Link.
https://code.google.com/p/android/issues/detail?id=232076
this happens if you have your project in a different drive other then C:
android studio will automatically create a copy of your project (i hope they are cache files) in the C: drive's tmp folder. this folder is the cause for this issue.
try File-> Invalidate Caches/Restart. or
just deleted the tmp folder in the c: drive and restart everything and it will work fine.
removing the build folder redirect code in the gradle file is a workaround.
allprojects {
//String osName = System.getProperty("os.name").toLowerCase();
//if (osName.contains("windows")) {
// buildDir = "C:/tmp/${rootProject.name}/${project.name}"
//}
repositories {
jcenter()
}
}
Found it here https://github.com/udacity/ud851-Exercises/issues/67
I have the same exact problem when trying to create a new activity.
IDE Fatal Errors
I was running Android Studio 3.2.1 on Windows 7 Professional. I did not have this problem until I started to switch loading project from different drives - I used to run my projects from F: drive and when I downloaded sample code to C: drive, creating new activities started to give this IDE fatal error.
Got a clue from online sources of a potential issue caused by buildDir path in build.gradle(Project:) shown below;
buildDir = "C:/tmp/${rootProject.name}/${project.name}"
Since my project working directory is in F: drive, I changed the above C: drive to F:
buildDir = "F:/tmp/${rootProject.name}/${project.name}"
With this simple change, I can now create new Activity.
If nothing else works for you I suggest you try this solution.
I had tried most of the above suggestions without any success; the directory was fine, invalidation of caches, etc. But if you right-click on the project folder, New > Activity > Gallery (all others are greyed-out), clicking on any activity template will show you the exact problem. In my case, for some reason, all of my Activities (including Basic and Empty) required minSdkVersion 16; not the original 15. I hope this works for you if others fail.

Unzip downloaded file in gradle downloaded from own nexus server

I have downloaded a file from our nexus server by following this post: gradle - download and unzip file from url in my android project, first specifying the target:
compile 'net.myapp.gwt:MyAppGWT:1.0:android#zip'
and then I have this unzip task:
task unzip(type: Copy) {
def zipPath = project.configurations.compile.find {it.name.startsWith("net.myapp.gwt")}
println zipPath
def zipFile = file(zipPath)
def outputDir = file('src/main/assets/')
from zipTree(zipFile)
into outputDir
}
The problem is that the line
project.configurations.compile
generates the weirdest error. Applying it gives me following error:
Error:Could not find com.android.support:appcompat-v7:23.0.1.
Searched in the following locations:
file:/Applications/AndroioStudio.app/Contents/gradle/m2repository/com/android/support/appcompat-v7/23.0.1/appcompat-v7-23.0.1.pom
file:/Applications/AndroidStudio.app/Contents/gradle/m2repository/com/android/support/appcompat-v7/23.0.1/appcompat-v7-23.0.1.jar
https://jcenter.bintray.com/com/android/support/appcompat-v7/23.0.1/appcompat-v7-23.0.1.pom
https://jcenter.bintray.com/com/android/support/appcompat-v7/23.0.1/appcompat-v7-23.0.1.jar
https://maven.fabric.io/public/com/android/support/appcompat-v7/23.0.1/appcompat-v7-23.0.1.pom
https://maven.fabric.io/public/com/android/support/appcompat-v7/23.0.1/appcompat-v7-23.0.1.jar
https://nexus.domain.net/content/repositories/apps-releases/com/android/support/appcompat-v7/23.0.1/appcompat-v7-23.0.1.pom
https://nexus.domain.net/content/repositories/apps-releases/com/android/support/appcompat-v7/23.0.1/appcompat-v7-23.0.1.jar
Required by:
apps-android:app:unspecified
Please install the Android Support Repository from the Android SDK Manager.
Open Android SDK Manager
Now, all of a sudden it can't find
com.android.support:appcompat-v7:23.0.1
which isn't a problem if I remove that line.
#RaGe was right. Adding the libraries to our local repo server did the trick. Even though, as you state, this is a workaround and perhaps in the long run would be nice to find a better solution.
appcompat comes from your local sdk folder. But when you try to access configs.compile, gradle is trying to resolve all dependencies including appcompat from the external repos, and fails when it (unsurprisingly) doesn't find appcompat.
I'm not sure what the "right" solution is, but a possible workaround is to publish android sdk dependencies to your local maven or nexus repo. Here's one way to do that: github.com/simpligility/maven-android-sdk-deployer
Some answers from here might help too.
Lastly, it might be possible to treat your SDK folder as a local maven repo. See here.

How to Build AAR and Sample Application

I'm running into a collection of gradle problems in setting up a multi-module project. I'm trying to produce an AAR that contains an SDK for our customers use. I'm also trying to produce a sample application that uses that AAR both as a development platform internally and as an example for our customers of how to use the platform.
settings.gradle:
include :sdk
include :SampleApplication
build.gradle:
...
// copy the AAR produced by the SDK into the SampleApplication
task import_aar(type: Copy) {
dependsOn ":sdk:build"
from(new File(project(':sdk').getBuildDir(), 'outputs/aar')) {
include '*-release.aar'
rename '(.*)-release.aar', '$1-v1.0.0.aar'
}
into new File(project(':SampleApplication').projectDir, 'aars')
}
...
SampleApplication/build.gradle:
...
repositories {
...
flatDir {
dirs 'aars'
}
}
...
dependencies {
...
// This causes gradle to fail if the AAR hasn't been copied yet
compile 'com.moxiesoft.netagent:moxieMobileSDK:+#aar'
compile project(':moxieMobileSDK')
...
}
So the biggest problem that I'm having right now is getting the import_aar task to run before the compileDebug/ReleaseSources tasks. I've tried adding explicit dependencies to the compile tasks, but I'm apparently not finding the right way to do it.
I've tried putting this in SampleApplication/settings.gradle:
tasks['compileReleaseSources'].dependsOn(':import_aar')
but gradle fails because there's no compileReleaseSources task, even though gradle :SampleApplication:tasks shows one.
I also tried putting similar stuff in settings.gradle, but it also failed with an error that the task compileReleaseSources didn't exist.
I did have limited success by putting this in my SampleApplication/settings.gradle:
tasks['build'].dependsOn(':import_aar')
But that only has the correct affect if I use "gradle build", which doesn't happen if I'm debugging or running from Android Studio.
I was finally able to get this to work by putting the dependsOn on the preBuild task, but I'm still not particularly happy with the solution, because:
It requires me to have the aar in place before gradle runs, which
means I wind up putting the .aar into git, which isn't a
particularly good idea.
I'd rather not have the AAR generation leaking into the
SampleApplication/build.gradle file, since that's intended for
customer usage.
Is there a better way of handling the problem in general?
I also had problem adding a dependency to compileReleaseSources task and described here a solution that worked for me. In short, the dependency need to be added in tasks.whenTaskAdded closure.

Ignore Gradle Build Failure and continue build script?

Managing Android's dependencies with Gradle is done in a weird way. They have to be downloaded differently into a local repo. This is a pain when setting up CI build as there are multiple nodes this could run on. As such I'm using the sdk-manager-plugin to have the Android dependencies downloaded at build time. There seems to be an old bug that I'm experiencing with the sdk-manager-plugin though in that it will download the dependencies at build time, but they won't be available on that command.
The next time the command is run everything works fine (as everything is already downloaded), but I need to find a way to ignore the build failure of the first gradle command so that everything is downloaded and good to go for the second. I realize this is hacky, but I'm done messing with this.
Ideally something like this would work:
./gradlew clean --ignoreBuildFailures
./gradlew distributeCIBuild
The closest thing I could find in the Gradle documentation is --quite but that doesn't look like it'd work.
Any creative solutions welcome.
The flag to use is --continue.
From the documentation:
Continues task execution after a task failure.
add this in the build.gradle file :
tasks.withType(JavaCompile) {
options.failOnError(false)
}
You can use ignoreExitValue
task ktlint(type: JavaExec, group: "verification") {
description = "Check Kotlin code style."
ignoreExitValue = true
}

Issues with Maven and Android project : transitive dependencies, compilation time and IDEA integration

I have an Android project which (unfortunately) uses Maven as a building manager. The thing is that I have a lot of standard java .jar dependencies.
The first issue I am facing is the following :
1) These jar have other dependencies, and sometimes are redundant. The problem is that the removeDuplicates functionality of the android-maven-plugin does not seem to work as intended and I encounter the classic "multiple dex files define class X". I have resolved this issue by excluding the redundant dependencies one by one, which is very tedious and provides me with a pom which can only be qualified as disgusting. So here is my question : is there any way to easily manage transitive dependencies with android-maven-plugin and avoid the "class defined in multiple dex files" error ? Does Gradle handle this kind of stuff in a better way ? Or am I doomed to use "*" in every dependency exclusions and basically remove the transitive dependency feature from Maven ?
The second issue is about compilation time and the behavior of IDEA when using Maven dependencies in an Android project :
2) First, having to dex that many dependencies is obviously very time consuming. Is there any way to avoid pre-dexing all the jars everytime ? If I understand things correctly, if the jar does not change between 2 builds, then it should not be dexed again. I guess it is because I call mvn clean everytime, however if I do not the "class defined in multiple dex files" error shows up again. The temporary workaround for me was to adequatly declare dependencies in my pom using Maven and use the "Make" function of IDEA, which I guess calls the android tools and build the stuff appropriately. It worked for some time, and it was actually great. The pre-dexing worked as I expected and did not re-dex everything at each compile command. However, I hit the same wall all over again, the "Make" command produces "class defined in multiple dex files", I guess that removing duplicates here does not work either (just in case I was not clear, the same project, with the same pom, builds successfully with Maven but encounters the error when using "Make").
Basically, what I really would like is to declare and manage my dependencies using Maven (actually I do not really have a choice in the matter) and build by calling "Make" in IDEA. The ideal case would be to have the same behavior as "Make" when invoking "mvn clean install". Just in case, I would like to point out the fact that using parallel building is a noticeable improvement but it is quite far from what I would expect.

Categories

Resources