Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support packaging proto files into android archives(aar) #414

Merged
merged 3 commits into from
Aug 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,11 @@ class ProtobufPlugin implements Plugin<Project> {
*/
private void addProtoTasks() {
if (Utils.isAndroidProject(project)) {
// Avoid packaging proto files into apk.
if (!project.android.hasProperty('libraryVariants')) {
project.android.packagingOptions.exclude("**/*.proto")
}

getNonTestVariants().each { variant ->
addTasksForVariant(variant, false)
}
Expand Down Expand Up @@ -284,6 +289,8 @@ class ProtobufPlugin implements Plugin<Project> {

variant.sourceSets.each {
setupExtractProtosTask(generateProtoTask, it.name)
// Add proto files to java resources, and package them into aar/classes.jar.
it.resources.srcDirs += generateProtoTask.sourceFiles
}

if (variant.hasProperty("compileConfiguration")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ class ProtobufAndroidPluginTest extends Specification {
File testProjectLiteStaging = ProtobufPluginTestHelper.projectBuilder('testProjectLite')
.copyDirs('testProjectLite')
.build()
File testProjectAndroidLibrary = ProtobufPluginTestHelper.projectBuilder('testProjectAndroidLibrary')
.copyDirs('testProjectAndroidLibrary')
.build()
File mainProjectDir = ProtobufPluginTestHelper.projectBuilder('testProjectAndroidMain')
.copySubProjects(testProjectStaging, testProjectLiteStaging, testProjectAndroidStaging)
.copySubProjects(testProjectStaging, testProjectLiteStaging, testProjectAndroidStaging,
testProjectAndroidLibrary)
.withAndroidPlugin(agpVersion)
.build()
when: "build is invoked"
Expand Down Expand Up @@ -62,8 +66,12 @@ class ProtobufAndroidPluginTest extends Specification {
File testProjectLiteStaging = ProtobufPluginTestHelper.projectBuilder('testProjectLite')
.copyDirs('testProjectLite')
.build()
File testProjectAndroidLibrary = ProtobufPluginTestHelper.projectBuilder('testProjectAndroidLibrary')
.copyDirs('testProjectAndroidLibrary')
.build()
File mainProjectDir = ProtobufPluginTestHelper.projectBuilder('testProjectAndroidMain')
.copySubProjects(testProjectStaging, testProjectLiteStaging, testProjectAndroidStaging)
.copySubProjects(testProjectStaging, testProjectLiteStaging, testProjectAndroidStaging,
testProjectAndroidLibrary)
.withAndroidPlugin(agpVersion)
.build()
and:
Expand Down Expand Up @@ -122,8 +130,12 @@ class ProtobufAndroidPluginTest extends Specification {
File testProjectLiteStaging = ProtobufPluginTestHelper.projectBuilder('testProjectLite')
.copyDirs('testProjectLite')
.build()
File testProjectAndroidLibraryStaging = ProtobufPluginTestHelper.projectBuilder('testProjectAndroidLibrary')
.copyDirs('testProjectAndroidLibrary')
.build()
File mainProjectDir = ProtobufPluginTestHelper.projectBuilder('testProjectAndroidMain')
.copySubProjects(testProjectStaging, testProjectLiteStaging, testProjectAndroidStaging)
.copySubProjects(testProjectStaging, testProjectLiteStaging, testProjectAndroidStaging,
testProjectAndroidLibraryStaging)
.withAndroidPlugin(agpVersion)
.withKotlin(kotlinVersion)
.build()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.grpc.helloaarexample;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import io.grpc.ManagedChannel;
import io.grpc.helloaarexample.HelloAar.HelloReply;
import io.grpc.helloaarexample.HelloAar.HelloRequest;
import io.grpc.helloworldexample.R;
import io.grpc.okhttp.OkHttpChannelBuilder;
import java.util.concurrent.TimeUnit;

public class HelloAarActivity extends ActionBarActivity {
private Button mSendButton;
private EditText mHostEdit;
private EditText mPortEdit;
private EditText mMessageEdit;
private TextView mResultText;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_helloworld);
mSendButton = (Button) findViewById(R.id.send_button);
mHostEdit = (EditText) findViewById(R.id.host_edit_text);
mPortEdit = (EditText) findViewById(R.id.port_edit_text);
mMessageEdit = (EditText) findViewById(R.id.message_edit_text);
mResultText = (TextView) findViewById(R.id.grpc_response_text);
}

public void sendMessage(View view) {
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(mHostEdit.getWindowToken(), 0);
mSendButton.setEnabled(false);
new GrpcTask().execute();
}

private class GrpcTask extends AsyncTask<Void, Void, String> {
private String mHost;
private String mMessage;
private int mPort;
private ManagedChannel mChannel;

@Override
protected void onPreExecute() {
mHost = mHostEdit.getText().toString();
mMessage = mMessageEdit.getText().toString();
String portStr = mPortEdit.getText().toString();
mPort = TextUtils.isEmpty(portStr) ? 0 : Integer.valueOf(portStr);
mResultText.setText("");
}

private String sayHello(ManagedChannel channel) {
GreeterGrpc.GreeterBlockingStub stub = GreeterGrpc.newBlockingStub(channel);
HelloRequest message = HelloRequest.newBuilder().setName(mMessage).build();
HelloReply reply = stub.sayHello(message);
return reply.getMessage();
}

@Override
protected String doInBackground(Void... nothing) {
try {
mChannel = OkHttpChannelBuilder.forAddress(mHost, mPort).build();
return sayHello(mChannel);
} catch (Exception e) {

return "Failed... : " + e.getMessage();
}
}

@Override
protected void onPostExecute(String result) {
try {
mChannel.shutdown().awaitTermination(1, TimeUnit.SECONDS);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
mResultText.setText(result);
mSendButton.setEnabled(true);
}
}
}
3 changes: 3 additions & 0 deletions testProjectAndroidBase/build_base.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ dependencies {
compile(project(':testProjectLite')) {
exclude module: "protobuf-lite"
}
compile(project(':testProjectAndroidLibrary')) {
exclude module: "protobuf-lite"
}
protobuf files('lib/protos.jar')
testCompile 'junit:junit:4.12'
}
Expand Down
7 changes: 7 additions & 0 deletions testProjectAndroidBase/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="io.grpc.helloaarexample.HelloAarActivity"
android:label="@string/app_name_aar" >
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
1 change: 1 addition & 0 deletions testProjectAndroidBase/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">GrpcHelloworldExample</string>
<string name="app_name_aar">GrpcHelloworldExampleForAar</string>
</resources>
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.grpc.helloaarexample

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.util.Log
import io.grpc.helloworldexample.R

class HelloAarActivityKotlin : AppCompatActivity() {
// From testProjectAndroidLibrary: src/main/proto/helloAar.proto
var request = HelloAar.HelloRequest.getDefaultInstance()

// From testProjectLite: src/nano/proto/messages.proto
var simpleRequest = io.grpc.testing.Messages.SimpleRequest.getDefaultInstance()

// From lib/protos.jar
var blobMessage = com.google.protobuf.gradle.test.External.BlobMessage.getDefaultInstance()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_helloworld)
}
}
95 changes: 95 additions & 0 deletions testProjectAndroidLibrary/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// This build is not a complete project, but is used to generate a project.
// See: ProtobufPluginTestHelper.groovy
buildscript {
repositories {
maven { url 'https://maven.google.com' }
maven { url "https://plugins.gradle.org/m2/" }
}
}

plugins {
id 'com.android.library'
id 'com.google.protobuf'
}

repositories {
maven { url 'https://maven.google.com' }
maven { url "https://plugins.gradle.org/m2/" }
}


android {
compileSdkVersion 26
buildToolsVersion "26.0.1"

defaultConfig {
minSdkVersion 7
targetSdkVersion 23
versionCode 1
versionName "1.0"
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
}

// https://github.com/square/okio/issues/58
lintOptions {
warning 'InvalidPackage'
abortOnError false
}

dexOptions {
javaMaxHeapSize "1g"
threadCount 1 // reduce predex thread count to limit memory usage
}
}

protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.0.0'
}
plugins {
grpc {
artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0-pre2'
}
javalite {
artifact = 'com.google.protobuf:protoc-gen-javalite:3.0.0'
}
}
generateProtoTasks {
all()*.plugins {
javalite { }
}
ofNonTest()*.plugins {
grpc {
// Options added to --grpc_out
option 'lite'
}
}
}
}

dependencies {
compile 'javax.annotation:javax.annotation-api:1.2'
compile 'com.google.protobuf:protobuf-lite:3.0.0'
compile 'io.grpc:grpc-core:1.0.0-pre2'
compile 'io.grpc:grpc-stub:1.0.0-pre2'
compile 'io.grpc:grpc-okhttp:1.0.0-pre2'
compile('io.grpc:grpc-protobuf-lite:1.0.0-pre2') {
// Otherwise Android compile will complain "Multiple dex files define ..."
exclude module: "protobuf-lite"
}
compile(project(':testProjectLite')) {
exclude module: "protobuf-lite"
}
testCompile 'junit:junit:4.12'
}
1 change: 1 addition & 0 deletions testProjectAndroidLibrary/settings.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rootProject.name = 'testProjectAndroidLibrary'
5 changes: 5 additions & 0 deletions testProjectAndroidLibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.dont.build.me" >
<application />
</manifest>
57 changes: 57 additions & 0 deletions testProjectAndroidLibrary/src/main/proto/helloAar.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

// Copyright 2020, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Message definitions to be used by integration test service definitions.
syntax = "proto3";

// From testProjectLite: src/main/proto
import "messages.proto";

option java_package = "io.grpc.helloaarexample";

package helloaar;

// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
string name = 1;
// Uses message type from messages.proto
grpc.testing.SimpleContext context = 2;
}

// The response message containing the greetings
message HelloReply {
string message = 1;
}