I am preparing a Gradle build script for my Android application. Before build task starts I've to change main class name and thus change imports in each *.java file.
For example, if now my main class is com.company.myApp, so the packages in each java file are imported in following way: import com.company.myApp.pacakge.* and when I rename main class to com.company.newName I've also to change imports to: import com.comapny.newName.package.*.
I am beginner with Gradle but I was trying to find information about equivalent of replace() function and I found shadow plugin but it works on JAR files. There is one more soultion which is using ReplaceTokens but it works with tokens and class name is not a token like.
Is there any way to do it with Gradle?
For others with similar issue, changing any string to other using Gradle is quite easy. I've resolved my problemu using:
copy {
from "src_tmp"
into "src"
filter {
String line -> line.replaceAll("com.company.app", "com.new.string")
}
}
Related
I'm using Databinding with one of my project with project name com.abc.def. I've related all my views with binding like
ActivityLoginBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_login);
it worked fine but if I change my package name to com.Abc.Def it generated following error at the time of building Apk.
Cause: couldn't make a guess for
com.Abc.Def.databinding.ActivityLoginBindingImpl .
Please Note:
I have an old build with com.Abc.Def on playstore already live and I'm updating the version. That's why I have to Change package name.
I can't remove Databinding from whole project.as it relates to all views.
If I change my package name to old one ,it works fine.
I have already tried clean , rebuild and invalidate cache and restart .but no luck.
I just bumped into the same issue. I was able to fix it by toggling databinding.enabled inside Build.gradle (Module). Below is a little step-by-step guide, that I went through after renaming my company package (com.abc.myapp -> com.xyz.myapp), which got databinding to work as expected:
Build > Clean Project
go to your Build.gradle (Module) and disable databinding:
android {
dataBinding {
enabled = false
}
}
File > Sync Project with Gradle Files
Build > Rebuild Project (no surprise: you'll get a ton of error messages)
Now enable databinding again:
android {
dataBinding {
enabled = true
}
}
File > Sync Project with Gradle Files
Build > Rebuild Project
Note: Some steps here may be unnecessary, but a little extra sanity checking has never done any harm during project setup, right!?
According to JAVA package naming conventions:
The package name may contain uppercase or lowercase letters[a-z], numbers, and underscores [ _ ].
You can not use capital letters in naming packages.
com..Abc.Def.databinding.ActivityLoginBindingImpl .
Check if there is no empty package there, for those ..
first of all, did you changed package name only in Manifest?
note that it could be different to applicationId - so you can only change it and leave app package as it was.
RCA: probably OS you are using to build is case-insensitive but java compiler is - that's reason why it can't find classes. Bindings are generated alongside other generated classes (for example dagger 2 classes generated by annotation processor), each generator creates own files within folder structure that reflects class package BUT if packages differ only with big/small letters, second generator will use same folder with wrong name. The reason is if OS is case-insensitive it assumes that folder already exist but java compiler not.
Other solution (except leaving app package as it is) is to :
rename all packages in app to other that differ to app package or to
use OS that is case-sensitive (macOS could be formatter this way or
linux)
I had the same issue, After spending several hours had to change the name of the layout to make it work.
Steps I followed to make it work.
Tried clean build, invalidate cache, enable/disable binding. ( didn't work)
Got a suggestion from one of my fellow developer to recreate and rename the XML file. (It worked) !!
I`ve encountered this one also. If ever Basti Vagabond Instruction did not work try to search the entire files.
Just Follow this instruction below:
Edit->Find->Replace in Files (then search the old package name and replace it with your new package name).
This is the library I am using. But I need to change the style java code. But since it's an external dependency, the file is read-only.
I learned here that if I add it as module I can then edit the file. I did that. I added the module successfully.
But now, the problem is how do I use the class from the library module. For instance, in my MainActivity.java, when I declared the global variable CarouselPicker carousel, Android Studio said 'Cannot resolve symbol CarouselPicker' and there is no Alt+Enter option to import any class. CarouselPicker is a class in the library. It worked when I used the library using compile in dependencies.
EDIT:
This is my Android file structure:
screenshot of Android file structure
This is the dependencies block code of my build.gradle(Module:app):
screenshot of code bloack
In my Share.java file, I tried to import CarouselPicker, this is what I get for Alt+Enter:
What Alt+Enter gave me.
please make sure that you have added the dependency line of the module in the gradle
see this for more details How to import class from another module in android studio?
I am currently migrating an Eclipse app to Android Studio.
This app was using the in app billing.
My main problem is to compile the project and the aidl file (I guess you all use this file)
I get this error message:
Gradle: error: cannot find symbol class IInAppBillingService
Gradle: error: package IInAppBillingService does not exist
So, following some tutorials, I move this file from com.mypackage.billing to src/main/aidl
(see this reference)
But as soon, as I do that, I get this message:
Gradle: Execution failed for task ':xxxxxxxxxxx:compileDebugAidl'.
Failed to run command:
(...) C:\Users\xxxx\AndroidStudioProjects\xxxxxxProject\xxxxxxx\src\main\aidl\IInAppBillingService.aidl:45
interface IInAppBillingService should be declared in a file called
com\xxxxxxxx\billing\IInAppBillingService.aidl.
The message is clearly a contradiction with the post from the Google bug page I linked above.
Anyone suceeded to make this aidl file to work and can help me?
Just to inform, some links I followed:
http://tools.android.com/tech-docs/new-build-system/user-guide
http://developer.android.com/guide/components/aidl.html
Adding this as an answer since it seemed to help quite a few people.
Create a new directory named 'aidl' under 'src/main/'. It should look like 'src/main/aidl'.
Add a new package name 'com.android.vending.billing' to the directory 'src/main/aidl'
Locate your sdk location and go to "sdk\extras\google\play_billing". Default location for the sdk is "C:\Program Files (x86)\Android\android-sdk". If you custom changed it, then you will have to figure out the location through the sdk manager.
Copy 'IInAppBillingService.aidl' into the package created above. In the end, it should look similar to the image below.
Rebuild project and it should be good to go.
Note: Make sure you include the necessary import if your reference isn't working
import com.android.vending.billing.IInAppBillingService;
https://issuetracker.google.com/issues/36973270
Edit From Comment
After I did this, the references to IInAppBillingService in my code were still highlighted as errors, but after rebuilding the app, the class was recognized
Just as the error message says, you need to put IInAppBillingService.aidl in the correct directory dictated by it's package (com.android.vending.billing).
Within the src/main/aidl/ folder you already have, put the .aidl file in com/android/vending/billing/.
Create new directory under src/main called aidl
Right click on directory aidl, select new->add package
Enter Name of the package com.android.vending.billing
Copy IInAppBillingService.aidl from the directory Android/Sdk/extras/google/play_billing to the directory App_name/app/src/main/aidl/com/android/vending/billing
Now go ahead with InApp billing coding and while defining InApp related services you will get an error can not resolve symbol IInAppBillingXXXXXX
Now goto to Build from android studio menu , click on Rebuild Project. This will generate IInAppBillingService.java file inside App_Name/app/build/generated/source/aidl/debug/com/android/vending/billing. This will solve the issue.
The rest of posts here didn't work for me till I created a new folder like shown here.
Add this code in build.gradle
android {
sourceSets {
main {
aidl.srcDirs = ['src']
}
}
}
Rebuild and import aidl class
The above answers concentrate on file location, but it appears you already had that set correctly. I experienced this same issue in Android Studio, but none of the listed answers resolved it, and I banged my head against it for an hour. Eventually, I realized that I was missing an obvious import:
import com.android.vending.billing.IInAppBillingService;
Once I added that it resolved this error message.
This import isn't mentioned in any of the Google Billing docs or included in any of the code examples that I found. While it may be obvious to experienced Java developers, beginners just trying to learn their first project may need it explicitly pointed out.
We need to add
create folder - src/main/aidl/packagename and place aidl file under this.
In the aidl file - mention the package name.
package packagename
Now clean the project, rebuild the project - We can the corresponding java file for the aidl generated in app\build\generated\source\aidl\debug\packagename\youraidl.java
I know it sounds so easy, but I copy paste from google sample all folder
https://github.com/googlesamples/android-play-billing/tree/master/TrivialDrive/app/src/main
aidl/com/android/vending/billing
copied into project aidl ( I had set project view in Android Studio)
and next I clean and rebuild project and it found a reference.
I've tried every solutions, but the problem was that Android Studio had compiled, with any apparent reason, in a different build type of the module that contains the AIDL packages than it was specified by the settings.
From debug to release, so the other modules couldn't see the AIDL pkg.
Switching from debug to release and turns back, solved my problem.
restarting Android Studio worked for me
a second silly thing that took me a while. I dropped the code on Android Studio to allow him create the file, but he created a .java (as expected) instead a .aidl Jiji, stupid of me
if you do all the names correct go to Build>rebuild project
it worked for me
I use Android Studio 4.1, just right click mouse -> New -> AIDL -> AIDL File.
A file will be created and placed in the [src/main/aidl] folder automatically. The aidl folder will also be created if it does not exist.
This function only supports min sdk 16+. My old project still can be supported, you can temporarily modify the min sdk to 16, create aidl and build project. After the relative interface and class be generated, recover the min sdk settings, it also works and builds project well then.
I want to use this library to crop images, since the crop intent can cause problems on certain devices: https://github.com/lvillani/android-cropimage
If I import it in Eclipse however, I get some errors in Eclipse.
In the Util class I get an error on this
options.inNativeAlloc = true;
inNativeAlloc cannot be resolved. Also I get another error in the CropImage class:
MenuHelper.showStorageToast(this);
The MenuHelper class isn't included in the library.
Since if seen these kind of errors on another library I want to know if there is something that I'm missing or doing wrong.
I'm saving the library as a zip file, extract the zipfile and import the library folder in Eclipse by using import > Existing Android Code into Workspace. I think this is ok since I checked the source on Github to comfirm if I'm missing something.
This isn't a new issue for the project as you can see on the issue page. To solve it you could follow the recommendations made in that post or you could download/get the develop(the same way) branch which, at a quick look, doesn't seem to have those problems.
The project that you are using already is an android library. You should just download the entire source folder and use Import existing project (the .project file is already in github).
Then in your own project create a reference to it by going into project properties -> android -> library -> add
About inNativeAlloc - it is a hidden field... To set a "true" value to it you must use this code: setInNativeAllocTrue(opts)
I have defined an AIDL android interface to make available a service from other applications.
My problem is that in my project, Android does not generate the Java file from this AIDL.
Note that the project compiles and works fine. However, if I move this AIDL file to another project, Android generates the Java file.
I don't know where I can debug this kind of error or where I can read some log about this.
Can anybody help me?
I met the same issue and work fine for me on Android Studio. There are two ways to fix this.
Method 1:
Put AIDL files under src/main/aidl package to follow default source setting for gradle build.
It is easy way but not be smart.
Method 2:
Keep aidl files under your custom package.
Add source setting mapping in build.gradle file as below
sourceSets {
main {
aidl.srcDirs = ['src/main/java']
}
}
Don't forget clean and rebuild after setting as above.
More information please refer this,
https://issuetracker.google.com/issues/36972230
https://issuetracker.google.com/issues/36972230
https://issuetracker.google.com/issues/36988483
I ran into a similar issue. Using Android Studio, the file is required to be in a specific location unless you override it in Gradle.
I had to put the aidl file in 'src/main/aidl/example/package/name/' <-- The declared package in the aidl file would be 'package example.package.name'.
It requires the file to be in a specific aidl folder and to have a folder structure that matches the package name declared in the aidl. I found the initial hint here: https://code.google.com/p/android/issues/detail?id=56328
Eclipse displays you the errors directly in the .aidl file, so check your file first.
I got similar issue, and found my problem: I prefixed my interface functions with public. That was wrong. For instance:
package com.example.phone.service;
interface IService {
public void placeCall(String a, in String b, in String c);
}
should be:
package com.example.phone.service;
interface IService {
void placeCall(String a, in String b, in String c);
}
Don't forget the .java generated file is located in gen/ not src/.
Try cleaning the project, that's often helping to resolve those types of errors. Also check that it does not contain errors since errors can prevent completion of some compile steps.
Assuming you use Eclipse have a look at the "Console" view (Window > Show View > Console) which should contain the output of the compile process.
if you go to one of the parent directories depending on where you trying to create the interface e.g framework/base/ you will find the make file of this dir called Android.mk
in this file you should add the .aidl name in the proper spots (it is easy to understand exactly where thanks to the comments) and then build. the "your interface".java will be generated.