Android: Library (AAR) module build.gradle coding - android

I have a project tree (a Solution in terms of Visual Studio) with some Executable and AAR library modules.
I need to get something like this in their build.gradle files..
buildTypes {
release {
//if not exist :- create extra folder in standard or customised output folder
//with the name 'Release'
//say builder to put the release build version into this folder with
//names ModuleName.aar or .apk
}
debug {
//if not exist :- create extra folder in standard or customised output folder
//with name 'Debug'
//say builder to put the debug build version into this folder with
//names ModuleName.aar or .apk
}
}
I've spent 2 days looking for a documentation how to do that.. I found just fragmentary parts of some specific settings, which ain't interesting now.
Actually, I want to get the same behaiviour of the compiler and builder as it's in Visual Studio
ProjectBuildDir\Debug\ModuleName.aar
ProjectBuildDir\Release\ModuleName.aar
Please, if you know, this would be very good to share that knowledge..
Otherwise.. I think I'll spend whole week without success.. or refer to full documentation of how to program gradle build code.
UPD: I've seen such kinds of solution, but it makes my project structure unreadable by AS. It denies to sync project with its gradle.. it hides Tool window "Build variants".. it hangs on AS itself.. :)
Maybe it happens because it's applied to Module (AAR library)?
I don't know.. I just also unable to compile and build the release version :)
I can see debug but whem I change variamt in that Tool Window in front of the AAR-Module name - then run Rebuild (Clean and Sync..) - there is no any realease in the output folder.
And I can't find the description of this meta language..
I use the AAR as Android UserControl library - extended View.. because it's handy to develop it as separate module.. simply faster..
I am the Android-kid, but MicroSoft-man :) need to see the logic of the project meta data language..

Related

"Uses reserved file or directory name 'lib'" error when trying to build android app bundle

I'm trying to build an app bundle but I'm getting the following error:
File 'root/lib/x86_64-MacOSX-gpp/jni/libjunixsocket-native-2.0.4.jnilib' uses reserved file or directory name 'lib'.
For what I've seen from similar questions, this issue is normally solved juggling dependencies or files in the project structure, but in this case it seems to point to a native library involved in app architecture if i'm not mistaken. Any ideas how to solve this?
It looks like you are adding a dependency as a jar instead of an aar.
The aar contains the information of what files should be considered as Android resources, native libraries, etc. in the app. A jar is just a plain list of files without Android concept. Because the jar you're depending on contains a directory lib, the files would normally end up being considered as native libraries, but because the files come from a jar instead of an aar, the build system warns that it's unlikely to be a native library and may have unintended consequences at runtime.
Try to package that library as an .aar instead of a .jar. See this documentation: https://developer.android.com/studio/projects/android-library
Edit:
Note that this file could not be loaded by the Android platform if it was included as is in the APK, so even though the previous build systems would allow you to put anything in an APK, the Android App Bundle is more restrictive to ensure that you don't accidentally put unnecessary files which would increase unnecessarily the size of your app.
Ok it is working now! Steps I used to found the problem (thanks for pointing me in the right direction #Pierre)
Run a gradle build --scan from your terminal or go to the Gradle tab in Android Studio, select :app, help , androidDependencies to see your dependency graph.
Search the dependency graph for the library name related to the problem ( in my case I searched for socket, there was no match for libjunixsocket for example).
Going upwards on the dependency tree I realized it was caused by the 'io.voucherify.android.client:voucherify-android-sdk:2.1.0' dependency.
I just added #aar at the end of the dependency implementation, and I managed to build the app bundle.
implementation 'io.voucherify.android.client:voucherify-android-sdk:2.1.0#aar'

Composite Builds in Android Studio

I am looking to use Gradle's new composite build feature in an Android Studio project called MyProject that includes both an Android Archive library (.aar) called DroidLib and a Java library (.jar) called JavaLib. These libraries are currently checked into version control due to my inability to get Gradle to pull dependencies from our internal Artifactory. That is, the DroidLib project has the JavaLib.jar checked into git under the libs folder and the MyProject project has the DroidLib.aar checked into git under its libs folder. I would like to make changes in both DroidLib & JavaLib while debugging and consuming the changes in MyProject w/o manually rebuilding from the dependency projects.
The new Composite build feature in Gradle offers just the thing I need however it looks to be something triggered from the command-line via a flag such like this:
--include-build ../DroidLib
In the Gradle docs it looks like this flag allows Gradle to override a dependency declared in the local project with a similarly the project declared in the included build. This would make sense since they both would use the same group/name/version scheme. I'm having trouble trying to understand how I would use this from Android Studio where my DroidLib dependency is declared as a local filesystem based dependency:
compile(name:'DroidLib-1.19', ext:'aar')
How would the command line flag tell Gradle which dependency is overridden since there is no apparent group/name/version declared on DroidLib? Also how could I make use of this in my app launch run config which uses the Gradle-aware make feature? Is there a way to pass add'l options to the Gradle-aware make or am I over-thinking what has to happen here?
Short answer
It is not available yet (it is in IntelliJ 2016.3).
More details
According to this blog post, the feature is in IntelliJ 2016.3:
With composite builds, everything is much, much simpler. All you have to is to attach the Gradle projects of these libraries via the Add button in the Gradle tool window (my-utils in our case), and then select Compose Build Configuration from the context menu for the original project.
Unfortunately, according to this release note, Android-Studio 2.3 is based on IntelliJ 2016.2. So we'll have to wait for IntelliJ 2016.3 to be merged into Android Studio =/.

How do I use this file from Github on Android Studio?

I understand this might be an extremely obvious and ridiculous question to ask, but please excuse me as I'm a beginner. I want to integrate this file into my project so I can call its methods:
https://github.com/RomainPiel/Shimmer-android
The question is, what do I do with it? Is it considered a "module", "library" or "file"? Should I manually copy the source files and create new classes in my project, and then call the methods from there? This would be the most straight-forward but in the "how to use" section in that package, it was mentioned "compile 'com.romainpiel.shimmer:library:1.4.0#aar'"
What I tried:
I downloaded the file as a .zip and then File->New->Import Module then navigated to the unzipped file. I believe I'm then supposed to add "compile 'com.romainpiel.shimmer:library:1.4.0#aar'" so I went to the gradle file to try to add it.
There are two: build.gradle (Module:app) and (Project:ProjectName).
I've tried adding to either and\or both and got this error: (Error:9,0) The project 'ProjectName' may be using a version of Gradle that does not contain the method. Did I do something wrong? Is it supposed to be this easy?
I would be extremely grateful to anyone who can point me in the right direction :)
You have three option
Using the GitHub Desktop you can clone the project and open it like a local project. Every change you make will be tracked. You can then commit and push using the GitHub Desktop. It's all UI and simple to use.
https://desktop.github.com
On Android Studio, when you open it, you'll see this, select GitHub and continue by adding your credentials.
You can then commit and push directly from that.
Using the terminal / command line.
If you are new, I recommend the first. It's simple to use and you get a hang of using it as it is the same steps with any project on any IDE you use.
Note: Downloading it as zip and then using it a bad idea because you're making it difficult on yourself because you can't keep track of changes and you'll have to re-upload everything every time. Which defeats the purpose of version control
If that GitHub account is not yours, then you'll have to fork the project, this way you'll have a separate version of the code on you GitHub on which you can modify. If it is yours then you're good.
Typically, you do not want to include external source code manually. This inhibits your build tool's (i.e. Gradle's) ability to manage that source code. It's very easy in Gradle to, say, set the version (like you have done by specifying "1.4.0") and then later remind yourself what version you have by merely looking at your build.gradle file. However, how would you go about doing that with raw source code? Typically developers do NOT put the version number of the source code in the actual source code - that's what they use their version control system (e.g. git) for, usually with tags.
Another aspect of Gradle is downloading and caching (and compiling) the external project for you. So that's nice.
With that said, you typically want to put that "compile..." line in your module's build.gradle file. Not the Project's build.gradle.
A module corresponds to your application (or library) that you are building or using. Some project's have multi-module configurations, where one module acts as a dependency for another (or several others). A Project in gradle is more of a 'meta' configuration that you can apply to all of your modules. The gradle docs recommend you focus on your module's configuration first and adjust the Project's configuration only if you have a specific need and reason to do so.
For the error you mentioned, you might have some unnecessary configurations in your build.gradle file, or the tool version numbers might not reflect what's on your system - if you copied and pasted from the internet, you might want to correct this by letting your IDE generate that file (the brute-force approach would be to create a new project entirely, and use its build.gradle files as a reference). Before you do that, you might want to check if your IDE provides any warnings inside that file.
You have to add the dependency to your module's gradle file.
There are two gradle file present in an android project. One is the project gradle and the second is the module gradle for each individual module.
You just have to copy the given
compile '......' in the dependency block.

How to prepare a closed sourced SDK module on Android Studio?

Background
I'm working on an app that has become very popular, so much that a part of it is supposed to become an SDK (which would be available for developers), and the app will split to 2 apps (both use the SDK).
According to what I know, there are multiple ways to create an SDK module (previously called "project" on Eclipse) :
Completely open sourced (Android library) - all sources and resources are open sourced and can be modified. An example might be Facebook's SDK and a lot of Github repos.
a single Jar file, which can be closed sourced.
The problem
Sadly, I can't make the SDK open sourced, and it should relatively be protected vs prying eyes (obfuscated etc...).
The issue here is, the SDK needs to use some resources of its own (drawables, strings,...), and so far (because I didn't have a lot of experience with creating SDKs) I've found 2 ways to handle resources for SDKs :
use reflection and/or "context.getResources().getIdentifier" . This is quite messy, as I lose the whole "R" usage of the code. Also, it has issues with "styleable" , as I've written here. It also makes it hard to find unused resources.
even worse ways: put resources in assets folder, put files in a wacky way inside the jar file, ...
Note that a part of the SDK includes custom views (for example, classes that extend from TextView), so even if I do split the SDk into 2 modules- resources and java files, both might have issues of dependencies (each uses the other one).
The question
Is it possible to somehow solve this issue?
Is it possible for the code part of the SDK to remain closed sourced, reach the "R" file as usual, and make it easy for both me and whoever use the SDK ?
How would I then generate the jar file as being obfuscated via Android Studio? and is it possible to prepare it to to be used via gradle afterwards?
Can I maybe make the Android-library of the SDK into an obfuscated jar file and not worry about the "R" file ? I ask this because this way I could enjoy both worlds: for our apps, it would remain open sourced, and for third party apps it would be closed sourced.
EDIT: seeing that this is supposed to be easy, I've tried it myself. I've created a totally new POC project which has an Android library module called "sdkmodule", and added this class to it:
public class SdkClass
{
public String doIt(Context context)
{
return context.getResources().getString(R.string.app_name);
}
}
Then, I've made the app's module to use this one, and I wrote this code in it:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SdkClass sdkClass=new SdkClass();
Log.d("AppLog","string from SDK:"+sdkClass.doIt(this));
Log.d("AppLog","string with same ID name from app:"+getResources().getString(R.string.app_name));
}
What I expected is that the first log would print the string that's in the SDK module, and the second to show the string of the current project, but instead I got an exception:
java.lang.NoClassDefFoundError: Failed resolution of: Lcom/example/user/sdkmodule/R$string;
On another try, I've got the same string that's used on the app itself (the one that uses the SDK module). And, on another run, the SDK produced the needed string as I've wanted.
How could it be? What should I do ?
In addition, I've tried to make a second activity in the SDK itself, and I've created a resource there that has the same resource name (used for a textView in its layout) as of the app itself, yet with a different value, yet when I've reached this activity, I've seen the one used by the app.
Here's the project, zipped (ignore the name of the folder, I wanted to try flavors too) :
https://drive.google.com/file/d/0B-PZZGk2vPohX25WUDNKTmotUTg/view?usp=sharing
The answer to your problem is to package and distribute your library as an AAR bundle
This format allows you to provide an obfuscated SDK jar and with its resources and the R mapping file.
This format is a standard and fully supported by maven-android-plugin (actually it's the replacement of the old APKLib format which supports only the distribution of source files).
Of course it's also supported by Gradle and Android Studio.
The Android Archive (AAR) format does what you want. It's like an Android-specific JAR file and contains compiled code, but includes its own resources and manifest. You can also include obfuscation as part of the build process. By default, the current version of Android Studio (1.2) and Gradle automatically build .AAR files for all library modules you create in your project.
You can change an app module into a library project that will publish an AAR file just by changing apply plugin: 'com.android.application' into apply plugin: 'com.android.library' in your module's Gradle file. The .AAR file will be placed in your MODULENAME/build/outputs/aar folder after each build. Some more information is available here.
Edit 1, after question updated:
The resources in your AAR get consolidated with the app module when the final APK gets compiled. The app resources will override the library's. This is probably by design, to allow people using a 3rd party library to customize its resources when creating their app, without having to rebuild the library. I think the easiest way to solve your resource conflict issue would just be to name your sdkmodule resources something more unique. Why not make the string key R.string.com_example_sdk_name or something?
No, the AAR libraries don't get obfuscated by default, but you can set up ProGuard in the Gradle build file for your AAR library to take care of this. Other tools are also available.

How to install two versions of an app

is there a simple way to have installed two version of the app on the same android Phone?
Im updating a old web app to a native android app. For testing purpose it would be great if we could have both installed at the same time on the same device.
It would be great to rename the update just temporary.
If I rename the package name in the AndroidManifest, then also R will be renamed and I have to reorganize the imports etc.
Is it possible to do a simple rename only somewhere in the AndroidManifest without changing a single line of code (even not with eclipse / android studio refactoring mechanism).
I have tried to mark my native app as library project and to include it in a new Android Project with another package name. But I will get trouble with this approach, for instance with actionbar sherlock:
Unable to execute dex: Multiple dex files define Lcom/actionbarsherlock/R$attr;
Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/actionbarsherlock/R$attr;
If you are using new gradle build system you could create new build type and then just add packageNameSuffixto it. This will add suffix to your package name but everything else stays the same.
buildTypes {
debug {
packageNameSuffix ".debug"
}
}
There is an easy way if you want to do it temporarily. You need to modify the AndroidManifest.xml file.
Find the line that looks like this:
manifest package="com.example.myapp"
and change it to something like:
"com.example.myapp.debug"
AFAIK there is no way other than changing the application package name, but it's fairly easy.
In Eclipse, right-click your project and select Android Tools > Rename Application Package.

Categories

Resources