android-hidden-api icon indicating copy to clipboard operation
android-hidden-api copied to clipboard

Android Studio 3.2.0 sync error: Failed to transform android.jar

Open jp1017 opened this issue 5 years ago • 39 comments

As 3.2.0 sync error with api27 in this repo:

Could not resolve all files for configuration ':app:androidApis'. Failed to transform file 'android.jar' to match attributes {artifactType=android-mockable-jar, returnDefaultValues=false} using transform MockableJarTransform

jp1017 avatar Sep 25 '18 09:09 jp1017

Sync success with api28

jp1017 avatar Sep 25 '18 09:09 jp1017

I have this issue too. It seems to be some sort of new verification in Gradle 3.2.0.

I changed my classpath back to 3.1.4 and it syncs fine. I wonder if there's a new Gradle setting to disable this or if we're out of luck.

zacharee avatar Sep 25 '18 19:09 zacharee

The old version is ok! Gradle 3.2.0 is error in Sync.

KevinJe avatar Sep 26 '18 00:09 KevinJe

Even I am facing a compilation issue with Android Plugin version 3.2.0 for latest Android Studio which forces Gradle version 4.6. It gives some translation issue with the SDK android.jar. Awaiting android.jar for API version 28

poloolop avatar Sep 27 '18 10:09 poloolop

You can keep using 4.6. Just change the classpath from 3.2.0 to 3.1.4.

zacharee avatar Sep 27 '18 10:09 zacharee

I think the change responsible for this issue is this one or something related to it: https://android.googlesource.com/platform/tools/base/+/acf0ab389c444a85371b897d8b2c81d96d31b988

Maybe it'll help figuring out how to workaround this

daio avatar Oct 25 '18 09:10 daio

I have managed to sync with android gradle plugin 3.2.1, 3.3 RC and 3.4 alpha with android.jar assembled from .class files built from AOSP sources. When adding any .class from device's framework files to android.jar it fails to sync. You can check which classes fail with MockableJarGenerator from AOSP. It fails on line 134(classNode.accept method) on those classes that come from device's framework. You can add try/catch block and print all the class names that fail if you want. If you want to see how this class is used you can check this project.

daio avatar Dec 26 '18 07:12 daio

Of course it won't help with the issue of blacklisted APIs with the new Google policy.

daio avatar Dec 26 '18 07:12 daio

Here's android.jar for API level 28, which I use.

daio avatar Dec 26 '18 08:12 daio

Here's android.jar for API level 28, which I use.

Thanks. You make it.

Gradle 4.10.3, gradle plugin: 3.2.1, compile successful, great job. :+1: :smile:

jp1017 avatar Dec 28 '18 01:12 jp1017

Hey guys, it does not work with Gradle >= 3.2 and SDK 27, still got the error :/

However, it is working with Gradle v3.1.4

NastuzziSamy avatar Jan 10 '19 09:01 NastuzziSamy

NastuzziSamy this is OK. For compile SDK 27 is required gradle plugin 3.1.4. For compile SDK 28 is required gradle plugin 3.2.x.

henrichg avatar Jan 13 '19 19:01 henrichg

@henrichg could you please explain what versions are compatible with each others. I've tried the following configuration for the project and it didn't work:

  • Gradle wrapper
$ ./gradlew --version
---
Gradle 4.10.3
---
  • Gradle Android build tool plugin
dependencies {
        classpath 'com.android.tools.build:gradle:3.3.1'
...
  • SDK version
ext {
    compileSdkVersion = 28
    supportLibVersion = "28.0.0"
    minSdkVersion = 26
    targetSdkVersion = 28
}

android {
    compileSdkVersion rootProject.ext.compileSdkVersion
    buildToolsVersion "28.0.3"
    defaultConfig {
        minSdkVersion rootProject.ext.minSdkVersion
        targetSdkVersion rootProject.ext.targetSdkVersion
        multiDexEnabled true
    }
....

dependencies {
    // android libraries
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.android.support:recyclerview-v7:${rootProject.ext.supportLibVersion}"
    implementation "com.android.support:support-v4:${rootProject.ext.supportLibVersion}"

And after

./gradlew build

I got

> Task :app:lint FAILED
Calling mockable JAR artifact transform to create file: /home/user/.gradle/caches/transforms-1/files-1.1/android.jar/7470a0577854d0dc407ece72235e8952/android.jar with input /home/rgordeev/Android/Sdk/platforms/android-28/android.jar

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:lint'.
> Could not resolve all files for configuration ':app:androidApis'.
   > Failed to transform file 'android.jar' to match attributes {artifactType=android-mockable-jar, returnDefaultValues=false} using transform MockableJarTransform
      > java.lang.IllegalArgumentException (no error message)

rgordeev avatar Feb 14 '19 14:02 rgordeev

Use android.jar from @daio, attached in comment on 26 Dec 2018.

I'm using it with your confguration (except targetSDK = 27 and gradle 4.10.1) an working good. Builded in Android Studio 3.3.1.

henrichg avatar Feb 14 '19 17:02 henrichg

I get the error too, but i replace the Api28 android.jar with origin Api28 android.jar, it is ok

chejdj avatar Apr 19 '19 03:04 chejdj

I wonder how @daio manages to make it work?.

mleeaura avatar May 03 '19 13:05 mleeaura

@mleeaura I've assembled it from AOSP sources. Seems like pulling framework from device no longer works.

daio avatar May 03 '19 15:05 daio

Using the JAR from @daio works great for Pie. You can even bypass the hidden API restriction easily for your app, through Java or Kotlin.

Subclass the Application class, add it to your manifest, and put this in the onCreate() method:

val forName = Class::class.java.getDeclaredMethod("forName", String::class.java)
val getDeclaredMethod = Class::class.java.getDeclaredMethod("getDeclaredMethod", String::class.java, arrayOf<Class<*>>()::class.java)

val vmRuntimeClass = forName.invoke(null, "dalvik.system.VMRuntime") as Class<*>
val getRuntime = getDeclaredMethod.invoke(vmRuntimeClass, "getRuntime", null) as Method
val setHiddenApiExemptions = getDeclaredMethod.invoke(vmRuntimeClass, "setHiddenApiExemptions", arrayOf(arrayOf<String>()::class.java)) as Method

val vmRuntime = getRuntime.invoke(null)

setHiddenApiExemptions.invoke(vmRuntime, arrayOf("L"))

https://stackoverflow.com/questions/55970137/bypass-androids-hidden-api-restrictions/55970138

zacharee avatar May 29 '19 21:05 zacharee

@mleeaura I've assembled it from AOSP sources. Seems like pulling framework from device no longer works.

@daio can you please share how to build the android.jar with hidden APIs from AOSP sources or link to a guide?

adithyag avatar Jun 03 '19 16:06 adithyag

@mleeaura I've assembled it from AOSP sources. Seems like pulling framework from device no longer works.

I did the same, however mine is also not working. I wonder how you are merging framework.jar and existing android.jar. I just unzip them both on a folder and rezip them. What process are you following if you don't mind sharing?

arncore avatar Jun 05 '19 18:06 arncore

Sorry, kind of busy lately and I don't have the environment to repeat the process right now. When Android Q is released I'll probably do it again. Basically I just merged some intermediate jars with command-line utility 'jar'. I don't remember doing anything special, but I may be forgetting something.

daio avatar Jun 05 '19 20:06 daio

Sorry, kind of busy lately and I don't have the environment to repeat the process right now. When Android Q is released I'll probably do it again. Basically I just merged some intermediate jars with command-line utility 'jar'. I don't remember doing anything special, but I may be forgetting something.

Was it something described here https://stackoverflow.com/a/9118795 ?

adithyag avatar Jun 05 '19 22:06 adithyag

Something like that. As I remember, I also used MockableJarGenerator from the post above to determine which classes fail the transform. I could have removed some of those.

daio avatar Jun 06 '19 04:06 daio

Something like that. As I remember, I also used MockableJarGenerator from the post above to determine which classes fail the transform. I could have removed some of those.

Haha. Yeah that's where the money is. I haven't played with it but yours worked for me so I've been using it and it works great.

arncore avatar Jun 06 '19 19:06 arncore

I tried to reproduce the same steps I did last time with P for Q preview 5, but failed. So I had to improvise and generated android.jar from original android.jar + framework.jar made with turbine(it's a tool that makes a headers-only jar during AOSP build). The downside is that now if you try to browse the sources using Android Studio decompiler, then you're out of luck now as all of the methods have the bodies stripped after turbine. Anyway, here's android.jar with hidden APIs available for compilation of your apps. android.zip

daio avatar Jul 24 '19 10:07 daio

Oh yeah, it doesn't have internal resources because I don't need them

daio avatar Jul 24 '19 10:07 daio

Receiving this kind of error from CI pipe.

Calling mockable JAR artifact transform to create file: /home/travis/.gradle/caches/transforms-2/files-2.1/f22ba2b798712eea152cb64aedf1f5a9/android.jar with input /usr/local/android-sdk/platforms/android-28/android.jar

belrvn avatar Aug 29 '19 06:08 belrvn

@daio can you share one with the latest AOSP release Q-R1? I think a few APIs have changed

adithyag avatar Sep 05 '19 16:09 adithyag

Here's android.jar for API level 28, which I use.

把api28下载下来的jar包替换成这个,这个后缀zip要改成jar,然后同步就没报错了

download this .zip file then rename suffix to .jar replace the api28 jar downloaded in this project, it works

gradle/5.1.1 gradle tool:3.4.0 api28

foezzy avatar Sep 30 '19 02:09 foezzy

@daio jar works fine for compilation but when I try to run unit tests, it fails. Does anyone try to fix that? I am using Android 28.

juanjecilla avatar Dec 04 '19 10:12 juanjecilla

@daio I want to compile all the APIs (@hidden,@systemAPi,@unsupportAppUse,etc) to android.jar for androidx(android Q), could you please share the means or steps?

Thanks a lot, Ace

lgxace avatar Mar 05 '20 01:03 lgxace

I've also been able to generate an android.jar with AOSP hidden API that works with newer Gradle versions (tested with 6.2). This week (probably tomorrow) I'll share the steps and even a script.

JetpackDuba avatar Apr 27 '20 00:04 JetpackDuba

Checkout this repo I've just created. It contains a modified android.jar with android 10 & also the steps the generate your own. https://github.com/aeab13/android-jar-with-hidden-api/

JetpackDuba avatar Apr 27 '20 16:04 JetpackDuba

@aeab13 Tested android.jar form https://github.com/aeab13/android-jar-with-hidden-api/ and working good for my application PhoneProfilesPlus. Thank you very much.

henrichg avatar Apr 28 '20 18:04 henrichg

You are welcome @henrichg . Let me know if you find any issues.

JetpackDuba avatar Apr 29 '20 17:04 JetpackDuba

DIsable lint check by adding the config works for me :

android {
    ....
    lintOptions {
        checkReleaseBuilds false
    }
   ...
}

SummerOak avatar May 13 '20 06:05 SummerOak

I fix it .

com.android.tools.build:gradle:4.1.0 , same as other versions.

Create class com.android.builder.testing.MockableJarGenerator by IDEA

method:

 public void createMockableJar(File input, File output) throws IOException {
    }

build project.

Replace class com.android.builder.testing.MockableJarGenerator in builder-4.1.0.jar

located in gradle path\caches\modules-2\files-2.1\com.android.tools.build\builder\4.1.0\aec0494498d0c8b9b46c226bf0ff02d73a299cac

details in https://blog.csdn.net/qq_26413249/article/details/109819624

Reginer avatar Nov 20 '20 07:11 Reginer

Solved this issue by locating the Platforms folder on local machine @ C:\Users\user\Appdata\local\Android\sdk\platforms deleted the platform version that had issues in my case android-30 then reopened my IDE and proceeded to redownload the SDK again. The error dissappeared

gitaudotdev avatar Aug 18 '21 16:08 gitaudotdev